.NET 4.0 WCF Default Bindings

One of the really nice things about WCF in general, is that we can offload a ton of hard coded remoting details to a host *.config file.  On the same token, one of the worse things about WCF is building the hosting side *.config file....at least if you are not using WCF 4.0.  As you might have noticed while working with WCF, many services need the same basic settings: same binding (HTTP, TCP, etc), support for MEX, etc.

Establish these basic details time and time again can be annoying to say the least.  However, under .NET 4.0, WCF has some built in default binding endpoints, including a default usage of MEX. If you were to open the machine.config file for a .NET 4.0 installation, you will find a new section which specifies default WCF binding classes to use, if you specify an simple base address:

<system.serviceModel>

...

  <protocolMapping>

    <add scheme="http" binding="basicHttpBinding"/>

    <add scheme="net.tcp" binding="netTcpBinding"/>

    <add scheme="net.pipe" binding="netNamedPipeBinding"/>

    <add scheme="net.msmq" binding="netMsmqBinding"/>

  </protocolMapping>

    ...

</system.serviceModel>

Given this, you could now expose an HTTP and TCP endpoint using nothing more than the following host configuration file:

<configuration>

  <system.serviceModel>

    <services>

      <service name="MagicEightBallServiceLib.MagicEightBallService" >

        <host>

          <baseAddresses>

            <add baseAddress="http://localhost:8080/MagicEightBallService"/>

            <add baseAddress="net.tcp://localhost:8099/MagicEightBallService"/>

          </baseAddresses>

        </host>

      </service>

    </services>        

  </system.serviceModel>

</configuration>

That's it!  If you need to change any of the default properties of these default bindings, simply define an unnamed WCF binding in your <bindings> section.  For example:

<configuration>

  <system.serviceModel>

    <services>

      <service name="MagicEightBallServiceLib.MagicEightBallService" >

        <host>

          <baseAddresses>

            <add baseAddress="http://localhost:8080/MagicEightBallService"/>

            <add baseAddress="net.tcp://localhost:8099/MagicEightBallService"/>

          </baseAddresses>

        </host>

      </service>

    </services>

 

    <bindings>

      <basicHttpBinding>

        <binding openTimeout = "00:30:00" />

      </basicHttpBinding>

      <netTcpBinding>

        <binding closeTimeout="00:15:00"/>

      </netTcpBinding>

    </bindings>

 

  </system.serviceModel>

</configuration>

 Finally, if you need to enable MEX, you no longer need to manually build an endpoint for IMetadataExchange and establish a complex custom behavior.  The following is all you need:

<configuration>

  <system.serviceModel>

    <services>

      <service name="MagicEightBallServiceLib.MagicEightBallService" >

        <host>

          <baseAddresses>

            <add baseAddress="http://localhost:8080/MagicEightBallService"/>

            <add baseAddress="net.tcp://localhost:8099/MagicEightBallService"/>

          </baseAddresses>

        </host>

      </service>

    </services>

 

    <bindings>

      <basicHttpBinding>

        <binding openTimeout = "00:30:00" />

      </basicHttpBinding>

      <netTcpBinding>

        <binding closeTimeout="00:15:00"/>

      </netTcpBinding>

    </bindings>

 

    <behaviors>

      <serviceBehaviors>

        <behavior>

          <!-- To get default MEX, don't name your <serviceMetadata> element-->

          <serviceMetadata httpGetEnabled="true"/>

        </behavior>

      </serviceBehaviors>

    </behaviors>

 

  </system.serviceModel>

</configuration>

As I am sure you would agree, these are awesome simplifications.  While WCF has always been a simple programming model, the details of host configuration was quite painful.  Looks like things are much, much better with 4.0

Happy coding!

 


Posted by: Andrew Troelsen
Posted on: 2/8/2010 at 12:40 PM
Tags:
Categories: .NET
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Subscribe to this BlogRSS comment feed
Comments are closed
Contact Us 651-994-8558 1-800-866-9884
Home | Training | Curriculum | Course Finder | Schedule | Enroll | Twin Cities Java User Group | Consulting | Foundation | Jobs | About Us | Our Story | Press Room | Instructors | President | Map & Directions | Sitemap

Java Training | JSF / Struts / Spring / Hibernate Training | Java Power Tools Training | .NET 4.0 & Visual Studio 2010 Training | .NET 3.5 and Visual Studio 2008 Training | .NET 2.0 and Visual Studio 2003 Training | Prism / MVVM / MEF Training | Microsoft Web Development Training | Cloud Computing Training | Ajax / Web Services / XML Training | Groovy and Grails Training | SQL Server 2008 Training | SQL Server 2005 Training | Mobile Development Training | SharePoint 2010 Training | SharePoint 2007 Training | Agile, Process, Analysis & Design Training | Arch/Design Patterns Training | Microsoft Official Curriculum Training | Web Development Training | Ruby Training | Rational Application Developer (RAD) Training | WebSphere Application Server Training | WebSphere Portal Training | WebLogic Training | Boot Camp Training | Project Management Training | C++ Training | Metro / WinRT / Windows 8 Development Training | Retired

Intertech delivers training on-site and virtually serving cities including Phoenix, AZ | San Francisco, CA | Los Angeles, CA | San Diego, CA | San Jose, CA | Washington, DC | Chicago, IL | Orlando, FL | Boston, MA | Duluth, MN | Minneapolis St. Paul, MN | Rochester, MN | Raleigh-Durham, NC | New York, NY | Philadelphia, PA | Austin, TX | Dallas, TX | Houston, TX | Seattle, WA.