Monday, November 21, 2011

WCF 4.0 File-less Activation

So far till framework 3.5 we have used the service activation using the .SVC files, with Web Application. If you look closely to this pattern, some of the issues we face are

  • SVC file makes the Service tightly coupled with the activation process
  • Adds extra maintenance effort.
  • Every time we deploy our application we may need to change the config files with the updated Server URL’s.
  • If we want to rename our Service we have to physically change the file names.

WCF 4.0 had added lots of new features on 3.5 framework, you can get more details of these features here: What’s New in WCF 4.

In this post I am going to cover just the one very interesting feature of WCF 4.0 or 4.5 to solve the above mentioned issue, that is the File Less Activation, which says we do not require to keep any physical .SVC files anymore in our web application. Now you might be wondering how it is possible to add a reference or create a proxy to my service or invoke my Service, right ?

In WCF 4.0, you can define virtual service activation endpoints that map to your service types in Web.config. This makes it possible to activate WCF services without having to maintain physical .svc files.

The following example shows how to configure an activation endpoint.

    <add relativeAddress="CustomerService.svc" 

Now let us take a step back and see how our service was looking when we create using a default template for “WCF Service Application” Or “WCF Service Library”


By default your WCF Service Application create a Service1.svc files, follow the steps below to enable your Service with file-less activation.

1. Safely delete both the files Service1.svc and Service1.svc.cs.

2. Now add a class file which you want to create as Service, e.g FileService.cs, etc.

3. Implement the contract, IService1.cs in your FileService.cs,

4. Now coming back to the configuration file, add the relativeAddress with the desired SVC file name here and assign the service attribute with the namespace of your Service.

This will look similar to the line of code mentioned in the code snippet above.

So now with all the changes implemented given in the steps above your complete web.config file will be somewhat similar to the one below.

1: <?xml version="1.0"?>

   2: <configuration>
   3:   <system.web>
   4:     <compilation debug="true" targetFramework="4.0" />
   5:   </system.web>
   6:   <system.serviceModel>
   7:     <behaviors>
   8:       <serviceBehaviors>
   9:         <behavior>
  10:           <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
  11:           <serviceMetadata httpGetEnabled="true"/>
  12:           <!-- 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 -->
  13:           <serviceDebug includeExceptionDetailInFaults="false"/>
  14:         </behavior>
  15:       </serviceBehaviors>
  16:     </behaviors>
  18:     <serviceHostingEnvironment>
  19:       <serviceActivations>
  20:         <add relativeAddress="CustomerService.svc" 
  21:              service="WcfService1.CustomerService"/>
  22:       </serviceActivations>
  23:     </serviceHostingEnvironment>
  25:     <!--<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />-->
  26:   </system.serviceModel>
  27:  <system.webServer>
  28:     <modules runAllManagedModulesForAllRequests="true"/>
  29:   </system.webServer>
  31: </configuration>

Leaving all the configuration as default provided by your VS2010, we are just going to replace the <serviceHostingEnvironment> given in Line 18 above with codes required for file-less activation. Now lets hit F5 or Run, ideally this will display all the files contained in your Service directory in the browser windows with the Svc file missing. but don’t worry since we are creating the virtual service endpoints so there is no physical files to display here. But you can append the existing URL with  your desired “ServiceName + .SVC” file extension at the end of your browser URL “http://localhost:3035/CustomerService.svc


And that’s it, our service is now ready for hosting without any physical SVC file, you can deploy this Service application in IIS too or refer in any other applications without any change in config files, you just need to browse the service application and type the SVC service file name defined in your web.config file at the end of the URL in your browser window or Add Service Reference windows respectively.

The example given in this article gives you the basic idea of file-less activation. In actual scenarios you may have much bigger config files. To handle your complex configuration files you just need small changes to configure file-less activation, For example if you have defined single or multiple endpoints in your configurations then you just need to remove the address attribute from the endpoint tag and add the ServiceHostingEnvironment attribute mentioned above. This also works if you have multiple Service defined in the same application.

Few points to Note (Source: MSDN)

  • When using configuration-based activation, inline code in .svc files is not supported.
  • The relativeAddress attribute must be set to a relative address such as “<sub-directory>/service.svc” or “~/<sub-directory/service.svc”.
  • A configuration exception is thrown if you register a relative address that does not have a known extension associated with WCF.
  • The relative address specified is relative to the root of the virtual application.
  • Due to the hierarchical model of configuration, the registered relative addresses at machine and site level are inherited by virtual applications.
  • Registrations in a configuration file take precedence over settings in a .svc, .xamlx, .xoml, or other file.
  • Any ‘\’ (backslashes) in a URI sent to IIS/WAS are automatically converted to a ‘/’ (forward slash). If a relative address is added that contains a ‘\’ and you send IIS a URI that uses the relative address, the backslash is converted to a forward slash and IIS cannot match it to the relative address. IIS sends out trace information that indicates that there are no matches found.