Thursday, October 25, 2012

netTcpBinding in WCF Services

I have wrote some posts about WCF and today I am going to write about configuring netTcpBinding in WCF as it needs some little extra work. WCF or Windows Communication Foundation is one my favorite areas in the .NET Framework and bindings is one of the things which I like the most in WCF.

Before starting off, there is a small check list to be checked. First is, you can’t configure netTcpBinding, if your service is running in the Visual Studio Development Server. The reason for this is Visual Studio Development Server only supports HTTP. And to configure netTcpBinding, we need IIS 7. This will not work on previous versions of IIS. 

Next you have to make sure “Windows Communication Foundation Non-HTTP Activation” is available. Since I am using Windows Server 2008R2 operating system, it’s on the .NET Framework 3.5.1 Features under Features in Server Manager.
image
WCF Activation
Final item in the check list is, you have to make sure following service is started.
image
Net.Tcp Listener Adapter
Now I have created a WCF Service Application and I have changed the project properties as follows.
image
Use Local IIS Web Server
Then you can click on "Create Virtual Directory" button or if you run the project for the first time without clicking it, you will be asked before Visual Studio creating a directory for you. After creating the virtual directory you can see it under “Default Web Site” in the IIS.
image
Default Web Site in IIS
Now you have to do two configurations in IIS. Right click on “Default Web Site” and click on "Edit Bindings". Check whether net.tcp type is there and if not add it.
image
Default Web Site : Site Bindings
Now right click on created Virtual Directory and select "Advanced Settings" under Manage Applications. In Enabled Protocols add net.tcp.
Untitled
Advanced Settings of created virtual directory
Now you are done with the IIS configurations. Now modify the web.config as follows.
<system.serviceModel>
    <behaviors>      
        <serviceBehaviors>
            <behavior> 
                <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
                <serviceMetadata httpGetEnabled="false"/>
                <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
                <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>

    <services>
        <service name="WcfApplication.Service1">
            <endpoint address="" contract="WcfApplication.IService1" binding="netTcpBinding"></endpoint>
            <endpoint address="mex" contract="IMetadataExchange" binding="mexTcpBinding"></endpoint>

            <host>
                <baseAddresses>
                    <add baseAddress="http://localhost/WcfApplication/Service.svc" />
                </baseAddresses>
            </host>
        </service>
    </services>

    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>


And that's it. In here please note that I have put httpGetEnabled to "false". Now right click on the Service and view it in your browser. You will get something like this.
Untitled
Created Service
Now you can test this service by adding a service reference from another project or by using WCF Test Client which is located at,
"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\WcfTestClient.exe".
In here I am using the WCF Test Client.
image
WCF Test Client
image
WCF Test Client : Testing the service
That’s all. Hope this helps.

Happy Coding.

Regards,
Jaliya

1 comment: