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"
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.ServiceModel;
- namespace WCFTestService
- {
- [ServiceContract]
- public interface IServiceClass
- {
- [OperationContract]
- string GetText();
- [OperationContract]
- int MultiplyNumbers(int first, int second);
- }
- public class ServiceClass:IServiceClass
- {
- string IServiceClass.GetText()
- {
- return "Hello world!";
- }
- int IServiceClass.MultiplyNumbers(int first, int second)
- {
- return first * second;
- }
- int AddNumbers(int first, int second)
- {
- return first + second;
- }
- }
- }
Then you can create config file for the service, add a app.config to the project.
- <?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <system.serviceModel>
- <services>
- <service name="WCFTestService.ServiceClass" behaviorConfiguration="returnFaults">
- <host>
- <baseAddresses>
- <add baseAddress="net.tcp://localhost:8000/"/>
- <add baseAddress="net.pipe://localhost/"/>
- <add baseAddress="http://localhost:8082/"/>
- </baseAddresses>
- </host>
- <endpoint contract="WCFTestService.IServiceClass"
- name="NetTcpBinding_IServiceClass"
- binding="netTcpBinding"
- address="TcpBinding" />
- <endpoint contract="WCFTestService.IServiceClass"
- name="NetNamedPipeBinding_IServiceClass"
- binding="netNamedPipeBinding"
- address="NetNamedPipeBinding" />
- <endpoint contract="WCFTestService.IServiceClass"
- name="BasicHttpBinding_IServiceClass"
- binding="basicHttpBinding"
- address="BasicHttpBinding" />
- <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
- <endpoint address="mex" binding="mexNamedPipeBinding" contract="IMetadataExchange" />
- <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
- </service>
- </services>
- <bindings>
- <netTcpBinding>
- <binding name="NetTcpBinding_IServiceClass"></binding>
- </netTcpBinding>
- <netNamedPipeBinding>
- <binding name="NetNamedPipeBinding_IServiceClass"></binding>
- </netNamedPipeBinding>
- </bindings>
- <behaviors>
- <serviceBehaviors>
- <behavior name="returnFaults">
- <serviceMetadata httpGetEnabled="true"/>
- </behavior>
- </serviceBehaviors>
- </behaviors>
- </system.serviceModel>
- </configuration>
in main window's form_load event, you can create ServiceHost and then open it.
- ServiceHost sh = new ServiceHost(typeof(WCFTestService.ServiceClass));
- 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/
then you can call the method from 3 ways of binding.
- //using tcp
- MyTCP.ServiceClassClient client = new WCFServiceTestClient.MyTCP.ServiceClassClient("NetTcpBinding_IServiceClass");
- textBox2.Text = client.MultiplyNumbers(15, 20).ToString();
- //using named pipe
- NamedPipe.ServiceClassClient client = new WCFServiceTestClient.NamedPipe.ServiceClassClient("NetNamedPipeBinding_IServiceClass1");
- textBox2.Text = client.MultiplyNumbers(150, 200).ToString();
- // using http
- MyHttp.ServiceClassClient client1 = new WCFServiceTestClient.MyHttp.ServiceClassClient("BasicHttpBinding_IServiceClass");
- textBox2.Text = client1.MultiplyNumbers(98, 100).ToString();