Web application developer

WCF service with TCP connection

wcf tcp

Written by: willem | Nov 5 2014 3:42 PM

In this fourth post of my series about creating and using a WCF service, I'll show you how to create a WCF service that accepts TCP connections. TCP should be faster than using the service over HTTP since there is less overhead, but a complete performance analysis is beyond the scope of this article.

This article is one in a series of posts about WCF:

This post starts where the previous post ends. So if you want to start from here and don’t want to do read all the other posts, make sure you download the project here and configure IIS using the pictures in the previous post.

If you didn’t follow the previous blog strictly, you might not have installed the TCP software for WCF. Check this in “turn windows features on or off” and install it if necessary.

003

To allow TCP connections in IIS, start up the IIS manager by clicking start and typing “internet information Services (IIS) Manager”. Next click enter and the IIS manager starts.

Select “wcf_service”, in the left pane under sites, en click bindings on the right pane.

001

Click add.

002

Select net.tcp and add “8735:*”. This is for defining the port number.

004

Click ok, and the close.

Now, again in the IIS manager under wcf_service, click “Advanced Settings” in the right pane.

005

Under “enabled protocols” add net.tcp if it is not there yet.

006

Click ok and close the IIS manager.

Next, open the web.config file in the published folder. This is the folder to which the wcf_service in IIS manager refers. Be careful not to open the config of the Visual Studio project accidentally.

Add a binding for tcp under system.serviceModel –> bindings.

	<netTcpBinding>
              <binding name="InsecTcp">
                  <security mode="None"></security>
              </binding>
	</netTcpBinding>

Now add an endpoint for out tcp connection, this should be under services –> service.

	<endpoint address="net.tcp://localhost:8735/messageservice.messageservice.svc" binding="netTcpBinding" bindingConfiguration="InsecTcp" contract="MessageService.IMessageService"></endpoint>

Now open the project in visual studio. Since we are only using the client application you can unload MessageService project (but not necessary).

Change the button_click method in MainWindow.xaml.cs into this:

private void Button_Click(object sender, RoutedEventArgs e)
{
     EndpointAddress endpoint = new EndpointAddress("net.tcp://localhost:8735/MessageService.MessageService.svc");
     NetTcpBinding binding = new NetTcpBinding();
     binding.Security.Mode = SecurityMode.None;
     ChannelFactory<IMessageService> factory = new ChannelFactory<IMessageService>(binding, endpoint);
     IMessageService service = factory.CreateChannel();
     MessageService.Message message = new MessageService.Message();
     message.Reverse = true;
     message.StringValue = InputTextBox.Text;
     OutputTextBox.Text = service.ProcessMessage(message);
}

Here only three changes are made:

  • The endpoint address is changed (including the protocol!).
  • The binding class is changed to NetTcpBinding.
  • The tcp security mode in set to none since we don’t use security here.

Now run the client project and send some text to process!

007

Download the updated project here.

No Comments

Add a Comment

About Me

I'm a web application developer specializing in asp.net. I have skills in asp.net, C#, html5, javascript, asp.net mvc, design patterns and more.

more about me

Random background module

This module for Orchard CMS lets you upload a list of images. For every page it chooses one randomly and sets it a the background image.

Go to project page on CodePlex
Download module

The Monack Framework

This open source framework makes it easy to build and develiver custom web applications. Unfortunately I can't spent much time on it so it is still work in progress. 

More about MonackFr
Project page at CodePlex

Orchard

Since I decided to create this website in Orchard and didn't know anything about
it when I started, I also post my experiences with this CMS.