多个协定”示例演示如何在一个服务上实现多个协定,以及如何配置终结点以便与实现的每个协定进行通信
1.服务端代码如下(服务实现了两个协定,增加了黄色所示代码):
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 //创建一个ServiceHost 6 using (ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService))) 7 { 8 // Open the ServiceHost to create listeners 9 serviceHost.Open(); 10 Console.WriteLine("服务已经开启!"); 11 Console.WriteLine("按回车键结束服务!"); 12 Console.WriteLine(); 13 Console.ReadLine(); 14 } 15 } 16 17 } 18 [ServiceContract]//定义服务协定完成 19 public interface ICalculator 20 { 21 [OperationContract] 22 double Add(double n1, double n2); 23 [OperationContract] 24 double Subtract(double n1, double n2); 25 [OperationContract] 26 double Multiply(double n1, double n2); 27 [OperationContract] 28 double Divide(double n1, double n2); 29 } 30 31 [ServiceContract] 32 public interface ICalculatorSession 33 { 34 [OperationContract] 35 string test(string s); 36 } 37 38 public class CalculatorService : ICalculator, ICalculatorSession 39 { 40 public double Add(double n1, double n2) 41 { 42 return n1 + n2; 43 } 44 45 public double Subtract(double n1, double n2) 46 { 47 return n1 - n2; 48 } 49 50 public double Multiply(double n1, double n2) 51 { 52 return n1 * n2; 53 } 54 55 public double Divide(double n1, double n2) 56 { 57 return n1 / n2; 58 } 59 60 public string test(string s) 61 { 62 return s; 63 } 64 }
2.服务端的配置(增加了黄色所示的终结点):
1 <system.serviceModel> 2 <services> 3 <service name="Microsoft.ServiceModel.Samples.CalculatorService" behaviorConfiguration="returnFaults"> 4 <endpoint address="http://localhost:8000/GIX4" binding ="customBinding" 5 bindingConfiguration="compactBindingConfig" contract="Microsoft.ServiceModel.Samples.ICalculator"/> 6 <endpoint address="basic" binding="basicHttpBinding" contract="Microsoft.ServiceModel.Samples.ICalculator"></endpoint> 7 <endpoint address="secure" binding="wsHttpBinding" contract="Microsoft.ServiceModel.Samples.ICalculator"></endpoint> 8 <endpoint address="session" binding="wsHttpBinding" contract="Microsoft.ServiceModel.Samples.ICalculatorSession"></endpoint> 9 <host> 10 <baseAddresses> 11 <add baseAddress="http://localhost:8000/GIX4"/> 12 </baseAddresses> 13 </host> 14 </service> 15 </services> 16 <bindings> 17 <customBinding> 18 <binding name="compactBindingConfig" receiveTimeout="00:20:00" sendTimeout="00:30:00"> 19 <binaryMessageEncoding> 20 <readerQuotas maxDepth="64" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="4096" maxNameTableCharCount="16384"/> 21 </binaryMessageEncoding> 22 <httpTransport maxReceivedMessageSize="2147483647" /> 23 </binding> 24 </customBinding> 25 </bindings> 26 <behaviors> 27 <serviceBehaviors> 28 <behavior name="returnFaults"> 29 <serviceDebug includeExceptionDetailInFaults="true"/> 30 <!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false 并删除上面的元数据终节点--> 31 <serviceMetadata httpGetEnabled="true"/> 32 </behavior> 33 </serviceBehaviors> 34 </behaviors> 35 </system.serviceModel>
3.客户端更新以下服务,然后对配置做适当的修改,如下黄色所示:
1 <system.serviceModel> 2 <bindings> 3 <basicHttpBinding> 4 <binding name="BasicHttpBinding_ICalculator" /> 5 </basicHttpBinding> 6 <customBinding> 7 <binding name="CustomBinding_ICalculator"> 8 <binaryMessageEncoding /> 9 <httpTransport /> 10 </binding> 11 </customBinding> 12 <wsHttpBinding> 13 <binding name="WSHttpBinding_ICalculator" /> 14 <binding name="WSHttpBinding_ICalculatorSession" /> 15 </wsHttpBinding> 16 </bindings> 17 <client> 18 <endpoint address="http://localhost:8000/GIX4" binding="customBinding" 19 bindingConfiguration="CustomBinding_ICalculator" contract="Calculator.Service.ICalculator" 20 name="CustomBinding_ICalculator" /> 21 <endpoint address="http://localhost:8000/GIX4/basic" binding="basicHttpBinding" 22 contract="Calculator.Service.ICalculator" name="basic" /> 23 <endpoint address="http://localhost:8000/GIX4/secure" binding="wsHttpBinding" 24 contract="Calculator.Service.ICalculator" name="secure"> 25 <identity> 26 <userPrincipalName value="chenlh@huawei" /> 27 </identity> 28 </endpoint> 29 <endpoint address="http://localhost:8000/GIX4/session" binding="wsHttpBinding" 30 contract="Calculator.Service.ICalculatorSession" name="session"> 31 <identity> 32 <userPrincipalName value="chenlh@huawei" /> 33 </identity> 34 </endpoint> 35 </client> 36 </system.serviceModel>
4.客户端调用:
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 CalculatorClient client = new CalculatorClient("secure"); 6 double n1 = 5.6; 7 double n2 = 7.3; 8 double result; 9 10 result = client.Add(n2,n1); 11 Console.WriteLine("执行加法后的结果为:{0}", result.ToString()); 12 13 result = client.Subtract(n2, n1); 14 Console.WriteLine("执行减法后的结果为:{0}", result.ToString()); 15 16 result = client.Multiply(n1, n2); 17 Console.WriteLine("执行乘法后的结果为:{0}", result.ToString()); 18 19 result = client.Divide(n1, n2); 20 Console.WriteLine("执行除法后的结果为:{0}", result.ToString()); 21 22 CalculatorSessionClient clientSeesion = new CalculatorSessionClient(); 23 string s = clientSeesion.test("你好我做一个测试!"); 24 Console.WriteLine(s); 25 26 Console.ReadLine(); 27 28 } 29 }