日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

WCF热带鱼书学习手记 - Service Contract Overload

發布時間:2025/3/13 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WCF热带鱼书学习手记 - Service Contract Overload 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

重載是面向對象編程里面比較常見的一個問題,如下: interface ICalculator {int Add(int a, int b);double Add(double a, double b); } 通過不同的參數列表,給出不一樣的函數簽名。但是在WCF通過interface公開服務契約的時候,有這樣一個問題。直接在這2個Add方法上添加[OperationContract]會導致異常發生。 我們來創建一個例子,服務端代碼: interface ICalculator {[OperationContract]int Add(int a, int b);[OperationContract]double Add(double a, double b); }........static void Main(string[] args) {try{ServiceHost host = new ServiceHost(typeof(Calculator));Binding wsHttpBinding = new WSHttpBinding();host.AddServiceEndpoint(typeof(ICalculator), wsHttpBinding, new Uri("http://localhost:8086/Calculator/"));host.Open();}catch (Exception ex){Console.WriteLine(ex.ToString());}Console.Read(); } 在啟動self-host的時候遇到如下的異常信息: System.InvalidOperationException: Cannot have two operations in the same contract with the same name, methods Add and Add in type Server.ICalculator violate this rule. You can change the name of one of the operations by changing the method name or by using the Name property of OperationContractAttribute.
原因在于,基于wsdl的方法是不支持重載的,方法名是唯一的標識。WCF提供了一個解決方法,就是給[OperationContract]一個name屬性。 [ServiceContract] interface ICalculator {[OperationContract(Name="AddInt")]int Add(int a, int b);[OperationContract(Name="AddDouble")]double Add(double a, double b); } 重新啟動self-host以后,異常不再出現。從客戶端角度來看,如果以channel的方式調用: static void Main(string[] args) {Binding binding = new WSHttpBinding();EndpointAddress address = new EndpointAddress("http://localhost:8086/Calculator/");ICalculator proxy = ChannelFactory<ICalculator>.CreateChannel(binding, address);using (proxy as IDisposable){Console.WriteLine(proxy.Add(1, 2));Console.WriteLine(proxy.Add(1.0, 2.0));}Console.Read(); } 因為是引用了ICalculator接口,客戶端也支持了面向對象語言的重載特性。但如果使用的自動生成的proxy類,會生成類似如下的類定義: [ServiceContract] interface ICalculator {[OperationContract]int AddInt(int a, int b);[OperationContract]double AddDouble(double a, double b); }public partial class CalculatorClient : ClientBase<ICalculator>, ICalculator {public int AddInt(int a, int b){return Channel.AddInt(a, b);}public int AddDouble(double a, double b){return Channel.AddDouble(a, b);} } ..... 為了讓代理類也支持重載特性,可以手工的修改代理: [ServiceContract] interface ICalculator {[OperationContract(Name="AddInt")]int Add(int a, int b);[OperationContract(Name="AddDouble")]double Add(double a, double b); }public partial class CalculatorClient : ClientBase<ICalculator>, ICalculator {public int Add(int a, int b){return Channel.Add(a, b);}public int Add(double a, double b){return Channel.Add(a, b);} } ..... 然后我們在調用代理類的時候,可以同使用channel一樣,使用重載特性啦。。。。 CalculatorClient proxy = new CalculatorClient (); Console.WriteLine(proxy.Add(1, 2)); Console.WriteLine(proxy.Add(1.0, 2.0)); proxy.Close();

?

轉載于:https://www.cnblogs.com/milkman-nuaa/archive/2009/12/23/1630311.html

總結

以上是生活随笔為你收集整理的WCF热带鱼书学习手记 - Service Contract Overload的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。