Host and consume a WCF service over TCP

In a previous post we have seen how to build and deploy a WCF service in IIS and later consume that service, via a console client application. This service though, was build under the default configurations, that is to use the basic settings for the service to function. The default binding configuration for WCF services is the basicHttpBinding, which allows a service to be called over the internet, using http requests. Sometimes though, the service isn’t supposed to serve clients over the internet but serve clients within an organization for example. At these cases there are several different configurations can be applied that would make the service response more efficient. We are going to show now, how to configure a WCF service to function under the TCP protocol. I am going to configure the project from the previous post mentioned.

First of all, we need to extend IIS functionality so it can handle and support TCP protocol requests. To do that, Windows Process Activation service (WAS) must be enabled. I am using Windows 7 and WAS by default isn’t even installed. To activate go to control panel and find “Turn Features On and Off”. Select Windows Process Activation service and it’s sub-features, select Microsoft .NET Framework 3.51 and its sub-features, and then finally click OK. Ensure that you have the correct version of ASP.NET installed by running the following command in the “Developer Command Prompt for VS2012” which is under the developer tools folder of the Visual Studio 2012 folder.

aspnet_regiis -iru 

Open Internet Information Services as administrator, right click the Default Web Site and select Edit Bindings. If WAS was installed correctly, you should see a net.tcp entry in the window like this.

tcp

Now you need to configure the website that we have hosted in the IIS in previous post to listen request over the TCP too. Do that by right click the website from IIS Default Web Site, choose from Actions menu Advance Settings and add “net.tcp” to the “Enabled Protocols” entry. I had named the website “AdventureWorksService”.  The enabled protocols entry should look like this now.

http, net.tcp

Now the WCF service hosted in IIS can listen both HTTP and TCP requests (in different ports, TCP listens right now at :808 port). Back in the client project, note that in the App.config there is only one endpoint defined, which specifies the HTTP address of the hosted service. Also, in the Program.cs file, when we create the proxy, the default constractor is used. This is because only one endpoint is defined. Now, we are going to add another endpoint entry for TCP requests and also we ‘ll change the proxy contractor in the main method, passing as parameter the name of the new endpoint. That way, the requests will be sent over the TCP. The App.config file should change as follow:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
<system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="BasicHttpBinding_AdventureWorksService" />
   </basicHttpBinding>
</bindings>
<client>
  <endpoint address="http://localhost/AdventureWorksService/Service.svc"
   binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_AdventureWorksService"
   contract="AdventureWorksService.AdventureWorksService" name="BasicHttpBinding_AdventureWorksService" />
    <endpoint address="net.tcp://localhost/AdventureWorksService/Service.svc"
     binding="netTcpBinding" contract="AdventureWorksService.AdventureWorksService"
     name="NetTcpBinding_AdventureWorksService" />
</client>
</system.serviceModel>
</configuration>

Change the main method in the Program.cs file like this.


static void Main(string[] args)
{
// Create a proxy object and connect to the service
AdventureWorksService.AdventureWorksServiceClient proxy = new AdventureWorksService.AdventureWorksServiceClient("NetTcpBinding_AdventureWorksService");

// Obtain a list of all products
Console.WriteLine("Test 1: List all products");
string[] productNumbers = proxy.ListProducts();
foreach (string productNumber in productNumbers)
 {
    Console.WriteLine("Number: {0}", productNumber);
 }
Console.WriteLine();
Console.WriteLine("Test 2: Display the details of a product");
ProductData product = proxy.GetProduct("SA-M198");
Console.WriteLine("Number: {0}", product.ProductNumber);
Console.WriteLine("Name: {0}", product.Name);
Console.WriteLine("Color: {0}", product.Color);
Console.WriteLine("Price: {0}", product.ListPrice);
Console.WriteLine();
// Disconnect from the service
proxy.Close();
Console.WriteLine("Press ENTER to finish");
Console.ReadLine();
}

Build and run the program. You just consumed your service over TCP!

Advertisements


Categories: WCF

3 replies

  1. Note – be careful up whitespace in the “Enabled Protocols” in IIS. If there is whitespace between them, I have found it to fail. In your example, you specify “http, net.tcp”. It should be, “http,net.tcp”

Trackbacks

  1. Throwing and catching Strongly Types SOAP Faults in WCF | chsakell's Blog
  2. Secure a TCP – WCF service at the Message level | chsakell's Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Diary Of A Programmer

Because every day is worth noting

Chara Plessa

The purpose of this blog is to broaden my education, promote experimentation and enhance my professional development. Albert Einstein once said that “If you can’t explain it simply, you don’t understand it well enough” and I strongly believe him!

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Kumikoro

A Front End Developer's Blog

Muhammad Hassan

Full Stack Developer | ASP.NET | MVC | WebAPI | Advanced Javascript | AngularJS | Angular2 | C# | ES6 | SQL | TypeScript | HTML5 | NodeJS, MS candidate @LUMS, Grad & EX-Adjunct Faculty @NUCES-FAST, seasonal blogger & open-source contributor. Seattle, WA.

Software Engineering

Web development

IEvangelist

.NET, ASP.NET, C#, MVC, TypeScript, AngularJS

leastprivilege.com

Dominick Baier on Identity & Access Control

Happy DotNetting

In Love with Technology

Knoldus

Knols of experience to your advantage

knowshnet

Search - Read - Request - Share

Rahul's space

Learn, Share and Grow with me !

Dhananjay Kumar

Developer Evangelist @Infragistics | MVP @Microsoft |

SQL Authority with Pinal Dave

SQL Server Performance Tuning Expert

Conficient Blog

Random bits of tech from @conficient

Code! Code! Code!

SOLID & KISS

Code Wala

Designing and coding

Microsoft Mentalist

A way to start with Microsoft Technologies

Tony Sneed's Blog

A glimpse into the lives of Tony & Zuzana Sneed

%d bloggers like this: