Windows Communication Foundation 4.0 – Simplified Configuration Feature

What is WCF Simplified Configuration?

 

As per MSDN definition, WCF simplified configuration can be defined as: “Simplification of the WCF configuration section through support for default endpoints, binding and behavior configurations. These changes make it possible to host configuration-free services, greatly simplifying the developer experience for the most common WCF scenarios.”

 

A WCF service can be made up of  Business Logic written in a .Net programming language (C#.net/VB.net) and the configuration file which contains address, binding and/or behavior definitions for the service. While this configuration setting could be defined through code, it is a good practice to put these settings in configuration as it makes it easier for administrator to fine-tune WCF configuration without touching code.

 

With WCF3.x, if we try to host a WCF service without any endpoint definitions it would return an error. For example, when I tried with .Net 3.5 WCF service without any service definition, I received following error:

 

Service 'WcfService1.Service1' has zero application (non-infrastructure) endpoints. This might be because no configuration file was found for your application, or because no service element matching the service name could be found in the configuration file, or because no endpoints were defined in the service element.

When I tried same service with .Net 4.0 framework, it ran fine and I could see the service help page.

 

So what was the difference in above two experiments? Obviously it was .Net framework. Microsoft .Net Framework 4.0 provides a new WCF feature called WCF Simplified Configuration. So in WCF 4.0, one can create a WCF service without adding anything in WCF configuration. WCF infrastructure will take care to create a default endpoint (along with default binding and default behavior) for your service.

 

How it works?

When the service host application calls Open() method, it creates service description with the configuration settings already provided along with the service. If there are no configured endpoints, it calls AddDefaultEndpoints() public method which adds service endpoints for all base addresses in each contract found in the service host with the default binding.

 

For example, if your service has a single base address and implements only one service contract there will be one default endpoint added by the AddDefaultEndpoint() method.

I created a simple WCF service using Microsoft Visual Web Developer 2010 Express, it contains the following configuration in web.config file:

 

<configuration>

  <system.serviceModel>

    <behaviors>

      <serviceBehaviors>

        <behavior>

          <serviceMetadata httpGetEnabled="True" />

          <serviceDebug includeExceptionDetailInFaults="False" />

        </behavior>

      </serviceBehaviors>

    </behaviors>

  </system.serviceModel>

</configuration>

 

Please note it doesn’t contain any binding details and Service name attribute is also not set. I added the following method in my WCF service to print default binding added by Simplified WCF Configuration:

 

public string GetDefaultConfiguration()

{

    string ServiceDescription = string.Empty;

    foreach (ServiceEndpoint se OperationContext.Current.Host.Description.Endpoints)

    {

        ServiceDescription += "Address= " + se.Address + " Binding= " + se.Binding.Name + " Contract= " + se.Contract.Name;

    }

 

    return ServiceDescription;

}

 

When we test this service using WCFTestClient following are the results:

 

Address=

Binding= BasicHttpBinding

Contract= IService1

 

Since WCF service is hosted using Asp.net development server it has address with default binding set to basicHttpBinding and WCF 4.0 infrastructure creates a default endpoint using Address, Binding and Contract mentioned in the results above.

 

What may go wrong with WCF Simplified Configuration?

Suppose we have following WCF configuration settings:

 

<configuration>

  <system.serviceModel>

    <bindings>

      <basicHttpBinding>

        <binding name="BasicHttpBinding_IMetroLinxService"

        maxBufferSize="4000000" maxBufferPoolSize="4000000" maxReceivedMessageSize="4000000">

        </binding>

      </basicHttpBinding>

    </bindings>

  </system.serviceModel>

</configuration>

 

The configuration sets the maxReceivedMessageSize value to be 4000000 bytes. Now, since the service name attribute is missing from the configuration, WCF 4.0 will create its own default Binding and endpoint for the service. However, the default value for maxReceivedMessageSize attribute is 65,536 which will be the actual value for this service instead of 4000000 as mentioned in the configuration.

 

How to troubleshoot WCF Simplified Configuration related problems?

 

If we enable WCF tracing on a WCF service we get WCF traces similar to this:

 

<E2ETraceEvent xmlns="schemas.microsoft.com/2004/06/E2ETraceEvent">

  <System xmlns="schemas.microsoft.com/2004/06/windows/eventlog/system">

    <EventID>524312</EventID>

    <Type>3</Type>

    <SubType Name="Warning">0</SubType>

    <Level>4</Level>

    <TimeCreated SystemTime="2011-11-28T21:35:34.5395470Z" />

    <Source Name="System.ServiceModel" />

    <Correlation ActivityID="{9a6b8530-20e7-452c-ac8e-4d9032ea03da}" />

    <Execution ProcessName="WebDev.WebServer40" ProcessID="8968" ThreadID="6" />

    <Channel />

    <Computer>Test-PC</Computer>

  </System>

  <ApplicationData>

    <TraceData>

      <DataItem>

        <TraceRecord xmlns="schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Warning">

          <TraceIdentifier>msdn.microsoft.com/en-US/library/System.ServiceModel.EvaluationContextNotFound.aspx</TraceIdentifier>

          <Description>Configuration evaluation context not found.</Description>

          <AppDomain>e68d8b68-1-129669897343207830</AppDomain>

        </TraceRecord>

      </DataItem>

    </TraceData>

  </ApplicationData>

</E2ETraceEvent>

 

Configuration evaluation context not found description clearly says that the WCF service couldn’t find a configuration to attach with WCF service hence a default configuration (default endpoint) is applied.

Simply adding service attribute in <Service name> node in WCF configuration will attach the configuration defined in the WCF configuration file.

 

References:

 

 

Written by: 

Avil Avate

India Global Technical Support Center, Bangalore, India

Reviewed By:

Shamik Misra

India Global Technical Support Center, Bangalore, India