WCF - ServiceContract 继承
发布日期:2021-06-29 03:54:09
浏览次数:2
分类:技术文章
本文共 6680 字,大约阅读时间需要 22 分钟。
ServiceContract 支持继承的好处是我们可以在不影响现有客户端的情况下自由扩展服务的功能。本文的目的是了解一下继承的一些细节问题。
(注:为了显示方便,以下演示代码中作了些删减。)演示原型:服务器端代码 [ServiceContract] public interface IMyService { [OperationContract] int Add(int a, int b); } public class MyServie : IMyService { public int Add(int a, int b) { Console.WriteLine(this.GetHashCode()); return a + b; } } public class WcfTest { public static void Test() { ServiceHost host = new ServiceHost(typeof(MyServie), new Uri("http://localhost:8080/MyService")); host.AddServiceEndpoint(typeof(IMyService), new BasicHttpBinding(), ""); ServiceMetadataBehavior metadata = new ServiceMetadataBehavior(); metadata.HttpGetEnabled = true; host.Description.Behaviors.Add(metadata); host.Open(); } }
客户端代码 //------------------------------------------------------------------------------ // <auto-generated> // 此代码由工具生成。 // 运行库版本:2.0.50727.42 // // 对此文件的更改可能会导致不正确的行为,并且如果 // 重新生成代码,这些更改将会丢失。 // </auto-generated> //------------------------------------------------------------------------------ namespace ConsoleApplication1.localhost { [GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")] [ServiceContractAttribute(ConfigurationName="ConsoleApplication1.localhost.IMyService")] public interface IMyService { [OperationContractAttribute(Action="http://.../IMyService/Add", ReplyAction="...")] int Add(int a, int b); } [GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")] public interface IMyServiceChannel : IMyService, IClientChannel { } [DebuggerStepThroughAttribute()] [GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")] public partial class MyServiceClient : ClientBase<IMyService>, IMyService { public int Add(int a, int b) { return base.Channel.Add(a, b); } } }
app.config <?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <bindings> <basicHttpBinding> <binding name="BasicHttpBinding_IMyService" closeTimeout="00:01:00" ... /> </basicHttpBinding> </bindings> <client> <endpoint address="http://localhost:8080/MyService" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IMyService" contract="IMyService" name="BasicHttpBinding_IMyService" /> </client> </system.serviceModel> </configuration>
接 下来,我们对 IMyService 进行升级,我们要创建一个继承接口 IMyService2,以便添加新的方法。自然 MyService 要实现的接口也有所变更。由于 "ServiceContractAttribute [AttributeUsage(Inherited = false,...)]",我们同样需要为继承接口添加 "[ServiceContract]"。 [ServiceContract] public interface IMyService { [OperationContract] int Add(int a, int b); } [ServiceContract] public interface IMyService2 : IMyService { [OperationContract] void Test(); } public class MyServie : IMyService2 { public int Add(int a, int b) { Console.WriteLine(this.GetHashCode()); return a + b; } public void Test() { Console.WriteLine("Test..."); } } public class WcfTest { public static void Test() { ServiceHost host = new ServiceHost(typeof(MyServie), new Uri("http://localhost:8080/MyService")); host.AddServiceEndpoint(typeof(IMyService), new BasicHttpBinding(), ""); //host.AddServiceEndpoint(typeof(IMyService2), new BasicHttpBinding(), "v2"); ServiceMetadataBehavior metadata = new ServiceMetadataBehavior(); metadata.HttpGetEnabled = true; host.Description.Behaviors.Add(metadata); host.Open(); } }
如 果我们没有为 IMyService2 添加一个新的 Endpoint,一切都还是老样子,重新创建的客户端代码和配置文件与最初没什么区别,仿佛 IMyservice2 未曾出现过。接下来我们继续,将上面代码中的注释符号去掉,我们为 IMyService2 增加了一个新的 Endpoint,重新创建的客户端代码会是什么样子呢?创建客户端时,我们依然使用 "" 而不是 ""。 //------------------------------------------------------------------------------ // <auto-generated> // 此代码由工具生成。 // 运行库版本:2.0.50727.42 // // 对此文件的更改可能会导致不正确的行为,并且如果 // 重新生成代码,这些更改将会丢失。 // </auto-generated> //------------------------------------------------------------------------------ namespace ConsoleApplication1.localhost { [GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")] [ServiceContractAttribute(ConfigurationName="ConsoleApplication1.localhost.IMyService")] public interface IMyService { [OperationContractAttribute(Action="http://.../IMyService/Add", ReplyAction="...")] int Add(int a, int b); } [GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")] public interface IMyServiceChannel : IMyService, IClientChannel { } [DebuggerStepThroughAttribute()] [GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")] public partial class MyServiceClient : ClientBase<IMyService>, IMyService { public int Add(int a, int b) { return base.Channel.Add(a, b); } } [GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")] [ServiceContractAttribute(ConfigurationName="ConsoleApplication1.localhost.IMyService2")] public interface IMyService2 { [OperationContractAttribute(Action="http://.../IMyService/Add", ReplyAction="...")] int Add(int a, int b); [OperationContractAttribute(Action="http://.../IMyService2/Test", ReplyAction="...")] void Test(); } [GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")] public interface IMyService2Channel : IMyService2, IClientChannel { } [DebuggerStepThroughAttribute()] [GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")] public partial class MyService2Client : ClientBase<IMyService2>, IMyService2 { public int Add(int a, int b) { return base.Channel.Add(a, b); } public void Test() { base.Channel.Test(); } } }
app.config <?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <bindings> <basicHttpBinding> <binding name="BasicHttpBinding_IMyService" closeTimeout="00:01:00" ... /> <binding name="BasicHttpBinding_IMyService2" closeTimeout="00:01:00" ... /> </basicHttpBinding> </bindings> <client> <endpoint address="http://localhost:8080/MyService" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IMyService" contract="IMyService" name="BasicHttpBinding_IMyService" /> <endpoint address="http://localhost:8080/MyService/v2" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IMyService2" contract="IMyService2" name="BasicHttpBinding_IMyService2" /> </client> </system.serviceModel> </configuration>
看 到了什么?没错,在原有的基础上增加了完整的 IMyService2 代理实现。这也就意味着原有客户端只需更新自动生成的代码就可以使用新的服务方法,当然不做调整的客户端依旧可以正常运行。但是有一点要注意,客户端生成 的新老接口和代理类型之间并没有继承关系,所以在客户端我们并不能使用 "多态" 代理。要是我们使用 "" 来创建客户端,会是什么结果?结果就是和上面的一样,也会自动生成新老接口和代理。在使用 "" 创建客户端代码前,记得调整 ServiceMetadataBehavior 的地址。 ServiceHost host = new ServiceHost(typeof(MyServie), new Uri("http://localhost:8080/MyService")); host.AddServiceEndpoint(typeof(IMyService), new BasicHttpBinding(), ""); host.AddServiceEndpoint(typeof(IMyService2), new BasicHttpBinding(), "v2"); ServiceMetadataBehavior metadata = new ServiceMetadataBehavior(); metadata.HttpGetUrl = new Uri("http://localhost:8080/MyService/v2"); metadata.HttpGetEnabled = true; host.Description.Behaviors.Add(metadata); host.Open();
直接使用 "Update Services Reference" 功能时,会出现如下异常。不知道是 Bug,还是我的系统有问题?
转载地址:https://blog.csdn.net/zengjibing/article/details/3812208 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
很好
[***.229.124.182]2024年04月06日 20时23分22秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Linux下启动rpc时提示Cannot register service: RPC: Unableto receive; errno = Connectionrefused的问题
2019-04-29
Google纪念遗传学之父孟德尔诞辰一百周年图标
2019-04-29
在Apache下配置多个虚拟主机站点
2019-04-29
Apache中目录权限访问控制配置
2019-04-29
Apache的认证和授权配置
2019-04-29
【学习笔记】Linux下CPU性能评估
2019-04-29
【学习笔记】Linux下内存性能评估
2019-04-29
【学习笔记】Linux下磁盘IO性能评估
2019-04-29
【学习笔记】Mysql中通过profiling定位query性能瓶颈
2019-04-29
2011年网上购书清单
2019-04-29
质量管理专家——戴明的14点说
2019-04-29
python
2019-04-29
网络协议
2019-04-29
进程和线程
2021-07-02
sql面试题
2021-07-02
linux基础与调优
2021-07-02
软件缺陷基础
2021-07-02
软件测试-面试13问
2021-07-02
记一次django项目的部署
2021-07-02
测试项目调研
2021-07-02