Configuring WCF Service with netTcpBinding

Apart from other bindings supported by WCF, I found netTcpBinding as more complex to configure than its other peers and less (rather scattered) information about the same over internet. I thought of putting it all together from the very first step at one place in order to make it easier for everyone including myself to use it as reference in future. So lets get started.

Remember : netTcpBinding hosting on IIS is only supported by IIS7 and later versions. I’m using Microsoft Visual Studio 2010 and IIS7.5 for this demo.

Step 1:

By default, IIS only supports HTTP or HTTPS. In order to let IIS support netTcpBinding, we first need to enable/activate WCF non-HTTP activation. To do that, Go to control panel > Program and Features > Turn Windows features on or off and then look for ‘Microsoft .NET Framework 3.5.1’ . You will see something like below:

Make sure you select both options. This will enable IIS to support HTTP, HTTPS, net.tcp, net.pipe, net.msmq, msmq.formatname.

Remember : After windows finish enabling these features, it will unregisters ASP.NET from the machine.

Step 2:

As this has unregisters the ASP.NET from the machine. So, next step is to register ASP.NET with IIS. To do this,go to Visual Studio Command Prompt and then type ‘aspnet_regiis -i’. This will register the ASP.NET with the IIS and you will be able to host your web application again.

Step 3:

Now next step is to check if you have required windows service running. Go to Administrative tools > Services and check the following service if they running, if not start them.

These three service ensures that IIS is able to listen to TCP request and also enable the port sharing.

Step 4:

As you machine is now configured to support netTcp, next step is to configure IIS to enable a website to use net.tcp port which is 808 by default. You can also change it any other port you want. Make sure you don’t configure net.tcp to port 80 because it is being used by HTTP and you cannot have two protocols running on the same port. Alternative, either change HTTP port to something else other than 80 (only if you want to use net.tcp on port 80, BUT it is not a recommended approach).

Now time to configure IIS. Open IIS and then go to your WebSite (‘Default Web Site’) and right click on it and then go to ‘Edit Bindings’. You will see the following:

Did you notice that you got whole list of protocols other than HTTP and HTTPS. This is because we have enable WCF non-HTTP activation in step 1.

Now select net.tcp from the drop down and you will have something like following:

In the ‘Binding information’ text box, you need to specify which port you want to use for netTCP connection. As I mentioned earlier, 808 is default but you can have any other port as well. Format is ‘port:ipaddress’ so I entered ‘808:*’ and then click ok.

Now you website is configured to listen TCP connection request on port 808. Remember if you have multiple website where you need to setup TCP then you have to do the same for all the websites.

Step 5:

Next step is to enable net.tcp protocol for your application in the IIS. To do that right click on your application in the IIS and then click on ‘Advanced Settings’ and look for property ‘Enabled Protocols’ and there enter ‘net.tcp’. List of enabled protocol is a comma-separated list so you can enable multiple protocols by separating them with a comma.

Now your website is fully configured to use netTcp binding. Next step is to configure your project to have WCF service on TCP port 808.

Step 6:

Open Visual Studio Project where you have your WCF service and then right click on the project and then go to project ‘Properties’. You should have something like below. Select ‘Web’ tab from left and change servers to be local IIS web server instead of Visual Studio Development Server and change Project URL and point it to your IIS application.

The reason we have changed Server to be Local IIS Web Server instead of Visual Studio Development Server is that you can configure IIS Web Server to support net.tcp but you cannot configure the same for Visual Studio Development Server. This is because, Visual Studio Development Server only supports HTTP.

Now the next step is to configure your web.config of the project to support netTcpBinding for the WCF Service.

Step 7:

Our last step is to configure web.config to use netTcpBinding for the WCF Service. Following the sample of web.config on how it should for a WCF service using netTcpBinding.

Now there are few points to note:

– ‘httpGetEnabled’ attribute of ‘serviceMetadata’ must be set to ‘false’. This will allow to discover WCF service metadata over non-HTTP protocol.

– Note we are using ‘mexTcpBinding’ for the mex endpoint. You can also use ‘mexHttpBinding’ but then you have to set ‘httpGetEnabled’ attribute to ‘True’ and you should be able to locate your WCF Service using http address instead of net.tcp.

–  Its important that you specify the baseAddress for your endpoint. This will help IIS to let find your WCF Service. It will then know where to look for.

Step 8:

Next and final step is try and find your WCF Service in the browser.

Please note: you will only be able to discover service metadata in browser if you have set httpGetEnabled to True and have used mexHttpBinding instead of mexTcpBinding in the web.config. This is because, web browser only supports HTTP and HTTPs. Oterwise it will only be discoverable from within visual studio itself (see screenshot one after the below).

And now try using Add Service Reference option where you want to add the reference to this WCF Service.

Thats it and your ready to go.

Following are the errors you can encounter if you skipped or didn’t do any step properly.

Issue 1:

Resolution:

– Check if you have correctly enabled the protocols for the application in the IIS (Step 5).

– Check if you have mentioned proper baseAddress in the web.config (Step 7).

Issue 2:

Resolution:

– Perform Step 2.

Advertisements
Tagged , ,

28 thoughts on “Configuring WCF Service with netTcpBinding

  1. Great post. I hope MS makes this a slightly more automatic process in the future..

  2. Someone says:

    Hello,

    I did everything as described on this page, but when I am trying to access the .svc file through HTTP using my browser, I get the following error:
    Metadata publishing for this service is currently disabled.
    Then it suggests me to enable httpGetEnabled and use mexHttpBinding. There is a lot of controversial information about this elsewhere on the internet.
    How did you make mexTcpBinding work via HTTP? What may be needed for it to work?

    • Rohit Gupta says:

      Hi,

      It is because you have httpGetEnabled attribute set to false in the web.config. mexTcpBinding is used to discover wcf service metadata on the tcp protocol BUT not on the Http. If you want to discover the metadata on the Http then you have to use the mexHttpBinding instead of mexTcpBinding. The way you do it is below:

      With this you also need to set the httpGetEnabled to true like below:

      After setting this, you will be able to discover your web service over Http but it will still communicate over Tcp protocol.

      Remember, browser only supports HTTP and HTTPs. Thats why you weren’t able to discover your wcf service over HTTP because you had httpGetEnable to false and also the mexTcpBinding.

      Let me know if that works for you.

      • Some1 says:

        Hello,

        I’m setting httpGetEnabled attribute to false because this is how your post above and this picture suggested to do: https://rohitguptablog.files.wordpress.com/2011/06/webconfig.png
        I did try setting httpGetEnabled to true, but if I set it to true, then I still was not able to get the same page that you’re getting on this picture: https://rohitguptablog.files.wordpress.com/2011/06/svcutil.png
        If I have httpGetEnabled=”true” then the message I’m getting on this page tells svcutil.exe http://………?wsdl
        So, there must be something else, that you’re doing not like written in the post above, or there must be some error in my IIS/WAS/etc settings.

      • Rohit Gupta says:

        Hi,

        Sorry about that, I did miss some vital information for that. I’ve updated the post, please check step 7 and step 8 again. I’ve included the missing information.

        httpGetEnable and mexTcpBinding/mexHttpBinding are inter related. So for discovery over TCP you use the following:
        httpGetEnabled=false and mexTcpBinding

        For discovery over Http (or in web browser), use the following settings
        httpGetEnabled =true and mexHttpBinding

        Hope this will solve your problem.

  3. Someone says:

    Hi,

    Sorry, this still does not explain your screen shot, where it says:
    svcutil.exe net.tcp://…………………../Service.svc/mex
    If you are using http protocol to access the metadata, why it says net.tcp in your browser?
    When I tried with httpGetEnabled=”true”, it said something like svcutil.exe http://…………………../Service.svc?wsdl
    I also have tried executing svcutil.exe net.tcp://…………………../Service.svc/mex having in my configuration mexTcpBinding enabled, it gave errors and did not generate proxy/client.
    What part have you actually update in the post? I couldn’t see any changes from the previous version…

    Thanks.

    • Rohit Gupta says:

      Hi,

      Section 7 and 8 are changed from previous, they now elaborate little more about the screenshot.

      I think you’re confusing yourself between two things, one is service itself and other is its metadata. I would recommend start afresh from step in the post, it won’t take more than half and hour. Alternatively, send me your test solution on ro DOT gupta AT hotmail DOT com

      I can have a look and see where the actual problem is.

  4. PGus says:

    Hi,

    I’ve come across your blog while trying to host my WCF4 service on both HTTP and TCP endpoints. I’ve gotten my service to work on HTTP using file-less activation. However, when I try including service configuration, I get errors.

    Have you tried above with file-less activation? Can you throw some light on how this might be done?

    Thanks!

    • Rohit Gupta says:

      Hi,

      I haven’t tried the above using file-less activation. I need to have a look whether it is possible or not, considering the fact tcp.net protocol has its limitations. If you find something, please do share.

      Thanks

  5. Abhinav Bhatnagar says:

    Hi Rohit,

    Sorry, I followed every step . Finally my problem is same as ypu were discussing with person named ‘Someone’. I am still not able to retrieve metadata. I want to use it thru net.tcp binding. For that i kept my httpGetEnabled=false and binding=”mexTcpBinding”..

    But Still its not working….Service is not exposing metadata…even step 8 is not able to work out properly….i cant get i through service reference as well..If you dnt mind can i send you my solution to check where i am wrong..

  6. Abhinav Bhatnagar says:

    Well Now i am able to generate the metadata…

    svcutil.exe net.tcp://abhi-pc/WCFWithTCP/Service1.svc/mex

    But when i add it to service reference of my client. it gives me error..Pls help me to resolve that..

    ” Metadata contains a reference that cannot be resolved: ‘net.tcp://10.20.70.22/Service1.svc/mex’.
    The message could not be dispatched because the service at the endpoint address ‘net.tcp://10.20.70.22/Service1.svc/mex’ is unavailable for the protocol of the address.
    If the service is defined in the current solution, try building the solution and adding the service reference again.”

    • Rohit Gupta says:

      Hi Abhinav,

      Would you be able to send me the solution. i can certainly have a look and see where the problem is. Please send your solution at RO dot GUPTA at HOTMAIL dot COM

      I will try to help you out as much as possible.

      Thanks!

      • Abhinav Bhatnagar says:

        Hey Rohit,

        I got the solution..

        I resolved the issue by repairing the service model installation on the server with:

        C:\Windows\Microsoft.NET\Framework64\v4.0.30319>ServiceModelReg.exe -r

        With this i could add my service reference

  7. Abhinav Bhatnagar says:

    Hi Rohit,Thanks for the reply !! I have sent you solution.Mean while can you pls guide me about the service-Client security..

    As i am new to WCF, I have 1 WCF Service having four service contract gives me functionality of Addition,Multiplication,Division and Subtract respectively and if expose 4 different endpoints at service(E1 FOR ADD,E2-SUB….So on…)

    Now question is at client i want my Client1 can consume only Add feature using E1 but cannot consume Multiplication thru E2.

    What i think for this….
    a) if i instantiate the my Service Contract which is interface thru the class which implements it , will give me only those operation contracts which are defined in that contract only…this is simple OOPs concept.

    b.) But at client side after adding the service reference WCF by default provides the ContractClients….which would provide the details of all Service contract which is even not related to client..as proxy class contains all the information…Can we restrict the client at service…After adding Service reference is it possible to get only those information for web config which is required for that client….

    In short i am looking for client-Service security based SOA

  8. Arsh says:

    Well it worked for me, I have an hosting account and those guys are not able to configure it properly and I can’t do it since I do not have access to those settings.

    I cannot afford to have a VPS account since it’s very expensive.

    Thanks for the help.

  9. Nizam Deen says:

    Very nice post

  10. enpee says:

    Hi Rohit,
    nice explanation. I have a strange problem , surfing the sol I reached here and seems to me that you are expert.
    Can you please me. I’m having similar scenario. Trying to do the tcp binding in IIS 7.
    I’m having an interesting prob.
    The wcf service work fine , when build in .netframework 3.5 and in win 2008 servr having IIS 7 , the apppool is in v2.0.
    but it shows error The message could not be dispatched because the service at the endpoint address ‘net.tcp://machine.local:portnumber/Servicename.svc’ is unavailable for the protocol of the address.
    when I’m building it in .netframewk 4 and app-pool is V4.
    can you help or guess why it is having the issue with .netframework 4.0

    • enpee says:

      I tried doing everything including repairing the service model installation on the server with:
      C:\Windows\Microsoft.NET\Framework64\v4.0.30319>ServiceModelReg.exe -r
      and other things avaiable in internet regarding tcipbinding. my config file is almost the same as yours.

  11. Ashu says:

    Hi Rohit,

    I did the following setting in web.config – could u please assist me where exactly i am making mistake –
    Web.config –

    Output in IE –
    ————————————————————————-

    Error Summary
    HTTP Error 500.19 – Internal Server Error

    The requested page cannot be accessed because the related configuration data for the page is invalid.

    Detailed Error Information

    Module

    IIS Web Core

    Notification

    BeginRequest

    Handler

    Not yet determined

    Error Code

    0x80070005

    Config Error

    Cannot read configuration file due to insufficient permissions

    Config File

    \\?\C:\Users\test\Documents\Visual Studio 2012\Projects\WebClient.Web\WebClient.Web\web.config

    Requested URL

    http://localhost:80/WebClient.Web/Service.svc

    Physical Path

    C:\Users\test\Documents\Visual Studio 2012\Projects\WebClient.Web\WebClient.Web\Service.svc

    Logon Method

    Not yet determined

    Logon User

    Not yet determined

    Config Source -1:
    0:

    Links and More Information This error occurs when there is a problem reading the configuration file for the Web server or Web application. In some cases, the event logs may contain more information about what caused this error.
    View more information »

    • Rohit Gupta says:

      Hi Ashu,

      It looks like a permission to read issue. Check if your web.config file has read permission for the IIS_Users. If not, try assigning the permission and you should be able to get it working.

  12. Valentin says:

    Thanks a lot for the tutorial. Great stuff.

  13. Viyoma Sachdeva says:

    very nice description

  14. Naveen Kushwaha says:

    thanks a lot….
    this is really very helpful post….

  15. TARUN says:

    My 3 days search ends at your blog..thanks a lot bro for putting this together….

  16. Pavel Dvorak says:

    Thanks for the extremely helpful article, got my service tcp endpoint working follows the steps and discussion, and some experimentation.

  17. Appreciation to my father who informed me regarding this website, this website is really remarkable.

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

%d bloggers like this: