Developer Geeks Home

How to host WSHttpBinding and NetTcpBinding in a single WCF Service

RSS
 

  • Posted one year ago
  • Category: ASP .Net, SOA, WCF,
  • Audiences: Architect, Developer, System Analyst,

This article will demonstrate you how to host service endpoints with WSHttpBinding and NetTcpBinding in a single WCF Service.

Introduction

Let's jump to implementation.

Implementation

1. Open a new ConsoleApplication in VS .Net IDE. Rename it to "SelfHostWCF".

2. Add a reference to System.ServiceModel dll in the project.

3. Create a ServiceContract Interface and implement the Service class with OperactionContract.

   [ServiceContract]
    public interface IHelloWorldService
    {
        [OperationContract]
        string HelloWorld(string name);
    }

    //Service Class
    public class HelloWorldService : IHelloWorldService
    {
        public string HelloWorld(string name)
        {
            return string.Format("Hello, {0}", name);
        }
    }

4. Now, write the hosting code for this service:

Since we want to host the service with HTTP and TCP bindings, we would need to create respective Url for each of them.

Uri httpUrl = new Uri("http://localhost:8000/MyHelloWorldService");

Uri tcpUrl = new Uri("net.tcp://localhost:8090/MyHelloWorldService");

Use ServiceHost class to host the service with above declared HTTP and TCP urls.

ServiceHost servicehost = new ServiceHost(typeof(HelloWorldService), httpUrl, tcpUrl);

Add Http service endpoint and TCP service endpoint to the hosted service with specified Contract, Bindings, and Endpoint addresses:

//Add a http service endpoint
servicehost.AddServiceEndpoint(typeof(IHelloWorldService), new WSHttpBinding(), "");
//Add a tcp service endpoint
servicehost.AddServiceEndpoint(typeof(IHelloWorldService), new NetTcpBinding(), "");

Now, enable the metadata publishing and open the service host.

// Enable metadata publishing.   
ServiceMetadataBehavior metadata = new ServiceMetadataBehavior();
metadata.HttpGetEnabled = true;
metadata.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
servicehost.Description.Behaviors.Add(metadata);

// Open the ServiceHost to start listening for messages.
servicehost.Open();

Now, run the application and your service is hosted with HTTP and TCP endpoints:

Note: We don't need to provide service configurations for the endpoints in the Config files.

Following the full code for hosting this service:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace SelfHostWCF
{
    [ServiceContract]
    public interface IHelloWorldService
    {
        [OperationContract]
        string HelloWorld(string name);
    }

    //Service Class
    public class HelloWorldService : IHelloWorldService
    {
        public string HelloWorld(string name)
        {
            return string.Format("Hello, {0}", name);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Uri httpUrl = new Uri("http://localhost:8000/MyHelloWorldService");

            Uri tcpUrl = new Uri("net.tcp://localhost:8090/MyHelloWorldService");

            // Create the ServiceHost.   
            using (ServiceHost servicehost = new ServiceHost(typeof(HelloWorldService), httpUrl, tcpUrl))
            {
                //Add a http service endpoint
                servicehost.AddServiceEndpoint(typeof(IHelloWorldService), new WSHttpBinding(), "");
                //Add a tcp service endpoint
                servicehost.AddServiceEndpoint(typeof(IHelloWorldService), new NetTcpBinding(), "");
                // Enable metadata publishing.   
                ServiceMetadataBehavior metadata = new ServiceMetadataBehavior();
                metadata.HttpGetEnabled = true;
                metadata.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
                servicehost.Description.Behaviors.Add(metadata);

                // Open the ServiceHost to start listening for messages. Since   
                // no endpoints are explicitly configured, the runtime will create   
                // one endpoint per base address for each service contract implemented   
                // by the service.   
                servicehost.Open();

                Console.WriteLine("The service is ready at {0}", httpUrl);
                Console.WriteLine("Press <Enter> to stop the service.");
                Console.ReadLine();

                // Close the ServiceHost.   
                servicehost.Close();
            }
        }
    }
}

Now, Lets consume this service from a client application.

1. Open a new ConsoleApplication in VS .Net IDE. Rename it to "WCFClient".

2. Add a service reference to generate client proxy using HTTP endpoint url:

3. This will add HTTP and TCP endpoint binding configuarations to the App.Config file:

<client>
            <endpoint address="http://localhost:8000/MyHelloWorldService"
                binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IHelloWorldService"
                contract="ServiceReference1.IHelloWorldService" name="WSHttpBinding_IHelloWorldService">
                <identity>
                    <userPrincipalName value="WinManish\Manish" />
                </identity>
            </endpoint>
            <endpoint address="net.tcp://localhost:8090/MyHelloWorldService"
                binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IHelloWorldService"
                contract="ServiceReference1.IHelloWorldService" name="NetTcpBinding_IHelloWorldService">
                <identity>
                    <userPrincipalName value="WinManish\Manish" />
                </identity>
            </endpoint>
</client>

4. Now we can call the service from both the HTTP and TCP endpoints using these endpoint binding configurations.

ServiceReference1.HelloWorldServiceClient httpClient = new ServiceReference1.HelloWorldServiceClient("WSHttpBinding_IHelloWorldService");
            string retVal = httpClient.HelloWorld("HTTP Manish..");
            Console.WriteLine(retVal);

ServiceReference1.HelloWorldServiceClient tcpClient = new ServiceReference1.HelloWorldServiceClient("NetTcpBinding_IHelloWorldService");
            retVal = httpClient.HelloWorld("TCP Manish..");
            Console.WriteLine(retVal);

Following is the full code to consume these service endpoints:

using System;

namespace WCFClient
{
    class Program
    {
        static void Main(string[] args)
        {
            ServiceReference1.HelloWorldServiceClient httpClient = new ServiceReference1.HelloWorldServiceClient("WSHttpBinding_IHelloWorldService");
            string retVal = httpClient.HelloWorld("HTTP Manish..");
            Console.WriteLine(retVal);

            ServiceReference1.HelloWorldServiceClient tcpClient = new ServiceReference1.HelloWorldServiceClient("NetTcpBinding_IHelloWorldService");
            retVal = httpClient.HelloWorld("TCP Manish..");
            Console.WriteLine(retVal);

            Console.Read();
        }
    }
}

Also, attached is the Service Host and Client demo projects for you to download.

comments powered by Developer Geeks

My Recent Articles [86]