How to Create HTTP and TCP Binding in One Service Application

Step 1: Create a class library to create a WCF contract.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.ServiceModel;  
  7. using System.Runtime.Serialization;  
  8. namespace MyServiceLibrary   
  9. {  
  10.     [ServiceContract]  
  11.     interface IMyService  
  12.     {  
  13.         [OperationContract]  
  14.         int GetResult(int num1, int num2);  
  15.     }  
  16.     public class MyService: IMyService  
  17.     {  
  18.         public int GetResult(int num1, int num2)  
  19.         {  
  20.             return num1 + num2;  
  21.         }  
  22.     }  
  23. }   
Step 2: Create a console application to host and configure WCF service HTTP and TCP.
 
Step 3: Open app.config to configure HTTP and TCP binding,
  1. <system.serviceModel>  
  2.     <behaviors>  
  3.         <serviceBehaviors>  
  4.             <behavior name="b1">  
  5.                 <serviceMetadata httpGetEnabled="true" />  
  6.             </behavior>  
  7.         </serviceBehaviors>  
  8.     </behaviors>  
  9.     <services>  
  10.         <service name="MyServiceLibrary.MyService" behaviorConfiguration="b1">  
  11.             <endpoint address="" binding="basicHttpBinding" contract="MyServiceLibrary.IMyService"></endpoint>  
  12.             <endpoint address="" binding="netTcpBinding" contract="MyServiceLibrary.IMyService"></endpoint>  
  13.             <endpoint address="mex1" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>  
  14.             <endpoint address="mex2" binding="mexTcpBinding" contract="IMetadataExchange"></endpoint>  
  15.             <host>  
  16.                 <baseAddresses>  
  17.                     <add baseAddress="http://localhost:1111/MyService" />  
  18.                     <add baseAddress="net.tcp://localhost:1234/MyService" />  
  19.                 </baseAddresses>  
  20.             </host>  
  21.         </service>  
  22.     </services>  
  23. </system.serviceModel>  
Step 4: Open program.cs file to host WCF service. Write code in Main.
  1. ServiceHost host = new ServiceHost(typeof(MyServiceLibrary.MyService));  
  2. if (host != null)  
  3. {  
  4.     host.Open();  
  5.     Console.WriteLine("Service Started............");  
  6.     Console.ReadLine();  
  7.     host.Close();  
  8. }   
Step 5: Create client application as ConsoleApplication to consume WCF service.
 
Step 6: Add a service reference as HTTP or TCP,
  • http://localhost:1111/MyService
  • net.tcp://localhost:1234/MyService  
Step 7: Open program.cs to consume and execute service method. Write code in Main.
  1. ServiceReference1.MyServiceClient ob1 = new ServiceReference1.MyServiceClient();  
  2. Console.WriteLine(ob1.GetResult(10,20));  
Step 8: Execute ConsoleApplication. 

Your client application will through an error.

An endpoint configuration section for contract 'ServiceReference1.IMyService' could not be loaded because more than one endpoint configuration for that contract was found. Please indicate the preferred endpoint configuration section by name.

Solution
  1. Open web.config file from client application and copy binding name under binding section and add this binding name as a string parameter for ServiceReferences. Like this:
    1. ServiceReference1.MyServiceClient ob1 = new ServiceReference1.MyServiceClient("BasicHttpBinding_IMyService");  
  2. Open web.config file from client application and remove one of the bindings.
Step 9: Test your client application.
Ebook Download
View all
Learn
View all