Create multiple endpoints using config file in WCF

 In the server side, you have to create a new windows form solution, this app will be the host of the service. Add a class file named "ServiceClass.cs"

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.ServiceModel;
  6. namespace WCFTestService
  7. {
  8.     [ServiceContract]
  9.     public interface IServiceClass
  10.     {
  11.         [OperationContract]
  12.         string GetText();
  13.         [OperationContract]
  14.         int MultiplyNumbers(int first, int second);
  15.     }
  16.     public class ServiceClass:IServiceClass
  17.     {
  18.         string IServiceClass.GetText()
  19.         {
  20.             return "Hello world!";
  21.         }
  22.         int IServiceClass.MultiplyNumbers(int first, int second)
  23.         {
  24.             return first * second;
  25.         }
  26.         int AddNumbers(int first, int second)
  27.         {
  28.             return first + second;
  29.         }
  30.     }
  31. }

Then you can create config file for the service, add a app.config to the project.

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <configuration>
  3.   <system.serviceModel>
  4.     <services>
  5.       <service name="WCFTestService.ServiceClass" behaviorConfiguration="returnFaults">
  6.         <host>
  7.           <baseAddresses>
  8.             <add baseAddress="net.tcp://localhost:8000/"/>
  9.             <add baseAddress="net.pipe://localhost/"/>
  10.             <add baseAddress="http://localhost:8082/"/>
  11.           </baseAddresses>
  12.         </host>
  13.         <endpoint contract="WCFTestService.IServiceClass"
  14.                   name="NetTcpBinding_IServiceClass"
  15.                   binding="netTcpBinding"
  16.                   address="TcpBinding" />
  17.         <endpoint contract="WCFTestService.IServiceClass"
  18.                   name="NetNamedPipeBinding_IServiceClass"
  19.                   binding="netNamedPipeBinding"
  20.                   address="NetNamedPipeBinding" />
  21.         <endpoint contract="WCFTestService.IServiceClass"
  22.                   name="BasicHttpBinding_IServiceClass"
  23.                   binding="basicHttpBinding"
  24.                   address="BasicHttpBinding" />
  25.         <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
  26.         <endpoint address="mex" binding="mexNamedPipeBinding" contract="IMetadataExchange" />
  27.         <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
  28.       </service>
  29.     </services>
  30.     <bindings>
  31.       <netTcpBinding>
  32.         <binding name="NetTcpBinding_IServiceClass"></binding>
  33.       </netTcpBinding>
  34.       <netNamedPipeBinding>
  35.         <binding name="NetNamedPipeBinding_IServiceClass"></binding>
  36.       </netNamedPipeBinding>
  37.     </bindings>
  38.     <behaviors>
  39.       <serviceBehaviors>
  40.         <behavior name="returnFaults">
  41.           <serviceMetadata httpGetEnabled="true"/>
  42.         </behavior>
  43.       </serviceBehaviors>
  44.     </behaviors>
  45.   </system.serviceModel>
  46. </configuration>

in main window's form_load event, you can create ServiceHost and then open it.

  1. ServiceHost sh = new ServiceHost(typeof(WCFTestService.ServiceClass));
  2.         sh.Open();

The service side is completed!

 

2. Create client side

create another application and then add service reference according to their addresses registered in server side

here we can add these 3 service references for the following addresses.

net.tcp://localhost:8000/

net.pipe://localhost/

http://localhost:8082/

 

then you can call the method from 3 ways of binding.

  1. //using tcp
  2. MyTCP.ServiceClassClient client = new WCFServiceTestClient.MyTCP.ServiceClassClient("NetTcpBinding_IServiceClass");
  3.                 textBox2.Text = client.MultiplyNumbers(15, 20).ToString();
  4. //using named pipe
  5.                 NamedPipe.ServiceClassClient client = new WCFServiceTestClient.NamedPipe.ServiceClassClient("NetNamedPipeBinding_IServiceClass1");
  6.                 textBox2.Text = client.MultiplyNumbers(150, 200).ToString();
  7. // using http
  8.  MyHttp.ServiceClassClient client1 = new WCFServiceTestClient.MyHttp.ServiceClassClient("BasicHttpBinding_IServiceClass");
  9.                 textBox2.Text = client1.MultiplyNumbers(98, 100).ToString();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值