WCF - 泛型
发布日期:2021-06-29 03:54:13
浏览次数:2
分类:技术文章
本文共 2320 字,大约阅读时间需要 7 分钟。
在 《Programming WCF Services》中有这么一段话 "You cannot define WCF contracts that rely on generic type parameters. Generics are specific to .NET, and using them would violate the service-oriented nature of WCF. ",意思是说 "我们不能定义依赖泛型参数的 WCF 契约,因为这违反了面向服务的基本原则……"。
其实我们是可以使用泛型的,只不过在添加宿主和端点时,需要提供封闭构造参数。看下面的例子。 [DataContract] public class Data<T> { [DataMember] public T X; } [ServiceContract] public interface IMyService<T> { [OperationContract] void Test(Data<T> d); } public class MyServie<T> : IMyService<T> { public void Test(Data<T> d) { Console.WriteLine(d.X); } } public class WcfTest { public static void Test() { ServiceHost host = new ServiceHost(typeof(MyServie<int>), new Uri("http://localhost:8080/MyService")); host.AddServiceEndpoint(typeof(IMyService<int>), new BasicHttpBinding(), ""); ServiceMetadataBehavior metadata = new ServiceMetadataBehavior(); metadata.HttpGetEnabled = true; host.Description.Behaviors.Add(metadata); host.Open(); } }
可以是可以,不过生成的客户端代理类型名字有够怪的。 (当然,我们可以使用 Name 强行指定一个 "好看" 的名字。) //------------------------------------------------------------------------------ // <auto-generated> // 此代码由工具生成。 // 运行库版本:2.0.50727.42 // // 对此文件的更改可能会导致不正确的行为,并且如果 // 重新生成代码,这些更改将会丢失。 // </auto-generated> //------------------------------------------------------------------------------ namespace ConsoleApplication1.localhost { [GeneratedCodeAttribute("System.Runtime.Serialization", "3.0.0.0")] [DataContractAttribute(Namespace = "...")] [SerializableAttribute()] public partial class DataOfint : object, IExtensibleDataObject { } [GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")] [ServiceContractAttribute(ConfigurationName = "ConsoleApplication1.localhost.IMyServiceOf_Int32")] public interface IMyServiceOf_Int32 { [OperationContractAttribute(Action = "http://.../IMyServiceOf_Int32/Test", ReplyAction = "...")] void Test(DataOfint d); } [GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")] public interface IMyServiceOf_Int32Channel : IMyServiceOf_Int32, IClientChannel { } [DebuggerStepThroughAttribute()] [GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")] public partial class MyServiceOf_Int32Client : ClientBase<IMyServiceOf_Int32>, IMyServiceOf_Int32 { public void Test(DataOfint d) { base.Channel.Test(d); } } }
不过,既然是开发 SOA,那么还是遵循标准为好。所以还是建议大家,不要在服务公开接口中使用泛型。至于内部实现该怎么着就怎么着好了。 转载地址:https://blog.csdn.net/zengjibing/article/details/3812508 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
第一次来,支持一个
[***.219.124.196]2024年04月02日 20时14分49秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
记一次django项目的部署
2019-04-29
测试项目调研
2019-04-29
接手软件测试新项目的流程
2019-04-29
jmeter-性能测试2-脚本录制开发
2019-04-29
jmeter-性能测试3-参数化
2019-04-29
期货基础知识
2019-04-29
期权基础
2019-04-29
jmeter-性能测试6-性能基础扫盲
2019-04-29
pytest+allure生成测试报告
2019-04-29
接口测试用例
2019-04-29
vnc安装配置
2019-04-29
Allure 测试报告:allure.title 去掉后方的参数化显示
2019-04-29
appium起源-环境配置
2019-04-29
appium初识-firstdemo
2019-04-29
allure报告的定制化
2019-04-29
Python爬虫实战:批量下载网站图片
2019-04-29
Python 使用 PyQt5 开发的关机小工具分享
2019-04-29
利用Python爬取微博数据生成词云图片实例代码
2019-04-29
对Python3 解析html的几种操作方式小结
2019-04-29
Python基于opencv调用摄像头获取个人图片的实现方法
2019-04-29