Ron Jacobs

Windows Workflow Foundation

WCF How To–Configuration Based Activation

WCF How To–Configuration Based Activation

  • Comments 2

I was there when this feature was born.  It’s a long story but basically the idea is that you should be able to have a WCF service (or Workflow Service for that matter) without a SVC file.

What is an SVC file?

Simply put, an SVC file is configuration that lives in a file that gives IIS something to serve (after all it is a web server).

The .svc extension is handled by WCF so when a request comes into IIS with a URI that ends with .svc, WCF gets to handle it.  You can find the details in your machine level web.config (just search for .svc).Here is an example of the markup you will find in an SVC file.  To see this, just right click on a SVC file in Visual Studio and select View Markup

<@ ServiceHost Language="C#" Debug="true" Service="WebApplication1.Service1" CodeBehind="Service1.svc.cs" %>

As you can see, this configuration gives WCF the information it needs to build (if necessary) and host your service.  Yes you can actually put code in an SVC file but I don’t recommend it.  Usually all the code is in the code behind file.

Why Get Rid of It?

Most people don’t mind .svc files.  After all they are small 1 line text files.  But if you have thousands of services and hundreds of web servers there is a certain amount of overhead involved in managing all these 1 line text files.  The good news is that now with .NET 4 you can eliminate them if you want to.

How Do I Get Rid of It?

Configuration Based Activation is the name of the feature that allows you to eliminate the .svc file.  I’m going to walk you through step by step how you can eliminate an .svc file.

Task 1 – Create The Starting Project

  1. Start Visual Studio 2010
  2. Create a new ASP.NET Empty Web Application
  3. Add a new WCF Service to your application named Service1 – this will create an interface IService1.cs, Service1.svc and codebehind Service.svc.cs file.

Task 2 – View the Service Page

  1. Right click on the SVC file and select View in Browser
  2. You will see the service page with a link to the WSDL

Task 3 – Add the Configuration Activation Entries

  1. Open web.config
  2. Locate the <serviceHostingEnvironment> element and change it to add the following configuration providing the name of the service class and the relative address that we want to use for it
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" >
  <serviceActivations>
    <add service="WebApplication1.Service1" relativeAddress ="Service1.svc"/>
  </serviceActivations>
</serviceHostingEnvironment>

Task - Remove the SVC file

  1. Open the code behind file Service1.svc.cs
  2. Save it as Service1.cs (this will cause VS to cease to think of it as code behind)
  3. Delete Service1.svc (this will also delete the code behind Service1.svc.cs)
  4. Press F5 to run your application and navigate to the Service1.svc page as you did before. It works even though there is no .svc file.
  • Oh Yeah!!! Straight from Visual Studio 2010 Training Kit.  Thanks.

  • @Magesh - this is in the VS2010 Training Kit?  I know it's not in the What's new in WCF labs because I wrote that one.  At any rate, the kit is super valuable

Page 1 of 1 (2 items)