PERFORMANCE TUNING WCF

by Adactus Ltd 15. December 2011 00:35

More often we are deploying WCF services to be used by applications hosted in the same environment.  Traditionally we have opted to choose a binding mechanism and use it for external and internal applications connecting to our service. But wait…..are we really allowing our internal applications to perform as efficiently as possible?

The answer to this question is no.

This excellent article on WCF performance, from Terrance A. Snyder, shows the performance of various binding options in WCF.

By far the best channel is netTcpBinding for performance. Great! But netTcpBinding is a bit of a pig to get configured, so let’s demystify the steps involved in getting it setup.

Before we start, let’s get one thing clear.....Microsoft got configuration in WCF just about right. There, we said it! If you’re reading articles that start discussing significant code changes to support different WCF bindings, close your browser and go make a coffee (and don't open that site again!)

Still with us? Great…..let’s look at IIS and WCF configuration to support netTcpBinding.

There are several elements to supporting netTcpBinding in an IIS7 hosted application.

1.Windows Configuration

2.IIS Configuration

3.Service Configuration

4.Client Configuration

This following examples use a Windows 2008 R2 server, running IIS 7.

WINDOWS CONFIGURATION

  1. From the Start menu, choose Control Panel.
  2. Select Programs and Features.
  3. Click Turn Windows Components on or Off.
  4. Expand the Microsoft .NET Framework 3.5.1 node and check the Windows Communication Foundation Non-HTTP Activation feature.

CORRECTING IIS CONFIGURATION

If you install WCF Activation after IIS is installed or after you install .NET Framework 4.0 and above, you will need to refresh your IIS install by running the following command from a command prompt: 

aspnet_regiis -iru

 

IIS CONFIGURATION

The next stage is to configure your websites in IIS. First configure the bindings for the website. A net.tcp binding should have been created for the default website. You can add the required bindings to other websites manually using the IIS Manager, as shown below.

Alternatively, you can use Appcmd.exe, which is installed with the Internet Information Services 7.0 (IIS) management toolset. From an administrator-level command prompt, run the following command:

%windir%\system32\inetsrv\appcmd.exe set site "Default Web Site" -+bindings.[protocol='net.tcp',bindingInformation='808:*']

 

The net.tcp protocol needs to be added to the list of enabled protocols for the website. Using IIS Manager, select the website and click 'Advanced Settings'. Enter net.tcp as an additional enabled protocol. Each protocol should be separated with a comma.

 

Alternatively, you can use Appcmd.exe, which is installed with the Internet Information Services 7.0 (IIS) management toolset. From an administrator-level command prompt, run the following command:

%windir%\system32\inetsrv\appcmd.exe set app "Default Web Site/servicemodelsamples" /enabledProtocols:http,net.tcp

 

This completes the configuration of Windows and IIS to support net.tcp binding. 

WCF Service Configuration

The following steps outline the process to enable net.tcp binding for a WCF service. In addition we will outline a proven method for optimising the WCF net.tcp binding configurations, as outlined by this excellent article on WCF performance, from Terrance A. Snyder.

Service Definition

 

<services>
      <service name="NetTcpBindingExample.HelloWorldService">
        <endpoint binding="netTcpBinding" bindingConfiguration="max" contract="NetTcpBindingExample.IHelloWorldService"/>
        <endpoint address="mex" binding="wsHttpBinding" contract="IMetadataExchange" />
      </service>
</services>

 

Behaviour Definition

 

<behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>

        </behavior>
      </serviceBehaviors>
</behaviors>

 

Binding Definition

Option 1 - basicHttpBinding -- Fast!

 

<basicHttpBinding>

        <binding name="max" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
          <readerQuotas maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="2147483647" maxNameTableCharCount="2147483647" maxStringContentLength="2147483647"/>
          <security mode="None"/>
        </binding>
</basicHttpBinding>


 

Option 2 - customBinding - Faster!

 

<customBinding>
        <binding name="binaryHttpBinding">
          <binaryMessageEncoding maxReadPoolSize="2147483647" maxSessionSize="2147483647" maxWritePoolSize="2147483647" />
          <httpTransport maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" decompressionEnabled="true"/>
        </binding>
</customBinding>


 

Option 3 - netTcpBinding - Fastest!

 

<netTcpBinding>
        <binding name="max" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxConnections="2147483647" maxReceivedMessageSize="2147483647" listenBacklog="2147483647" transactionFlow="false">
          <readerQuotas maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="2147483647" maxNameTableCharCount="2147483647" maxStringContentLength="2147483647"/>
          <reliableSession enabled="false"/>
          <security mode="None"/>

        </binding>
</netTcpBinding>

 

 

That completes the WCF Service configuration - simple eh? Well actually, yes. You can choose between the different binding options above, but to get real performance for locally hosted services choose netTcpBinding everytime!

Client Configuration

The client configuration for the client application is very clean. A netTcpBinding is specified and the service endpoint is configured to use net.tcp://

 

<configuration>
    <system.serviceModel>

        <bindings>
            <netTcpBinding>
                   <binding name="max" maxBufferPoolSize="2147483647"
                                                    maxBufferSize="2147483647"
                                                    maxReceivedMessageSize="2147483647">
          <reliableSession enabled="false"/>
          <security mode="None"/>
        </binding>

      </netTcpBinding>
        </bindings>
        <client>
            <endpoint address="net.tcp://demo1.contoso.com:8080/HelloWorld.svc"
                      binding="netTcpBinding"
                      bindingConfiguration="max"
                      contract="HelloWorld.IHelloWorldService"
                      name="NetTcpBinding_IHelloWorldService" />
        </client>
    </system.serviceModel>

</configuration>


That completes the performance optimised netTcp configuration for a WCF service. To transition between services configured to use basic or web http binding only requires simple configuration change in Windows, IIS and the app.config or web.config files for the target services and clients. 

Simple, but effective WCF performance!

Tags: , , , , , ,

Performance | WCF

Add comment




  Country flag
biuquote
Loading


About Adactus Ltd

Adactus provides practical and bespoke software development and consulting services for web sites. As a Microsoft partner, Adactus can deliver customised web solutions, mobile development and general bespoke software development services across a wide range of eBusiness and eCommerce needs.  We also provide performance testing services for eCommerce development and other transaction-intensive web solutions.

Month List