Replacing an Existing ASMX Service with WCF

I have an existing ASMX service that I'm replacing with a WCF service. The replacement service works, but some of the clients point to a page with a service.asmx address. IIS gives me an error if I name my WCF service service.asmx. How do I get this to work?

Sometimes big tasks get stuck on the tiniest of things. In this case, the task of porting a web service is getting stuck on what to call the resulting service file. Note that in most cases porting a service is not actually that hard either, although you will have to spend some quality time with the service contract fixing up namespaces and operation names to match what the old system automatically generated. That was a digression from the task at hand. Let's look at what's going wrong here and how to fix it.

Service files are a text-based way to define the behavior that occurs when a resource with a particular address is requested. What actually goes on is that the resource file gets compiled to generate some code, and it's the file extension that decides what compiler gets used. A compiler is just an instance of System.Web.Compilation.BuildProvider that can spit out code on demand. You don't have to worry about any of this because we've already written a BuildProvider for WCF services. The class that does this is System.ServiceModel.Activation.ServiceBuildProvider although it may be hard to find any information about this class because it's not public. However, you also don't need to worry about that because for our purposes all you need to know is the type name.

Now, the web.config file of your service controls the mapping of file extensions to a BuildProvider. Here's an example of changing the default mapping of *.asmx to invoke a WCF service.

 <system.web>
 <compilation>
  <buildProviders>
   <remove extension=".asmx"/>
   <add extension=".asmx" type="System.ServiceModel.Activation.ServiceBuildProvider, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  </buildProviders>
 </compilation>
</system.web>

In case you're curious, here are the type names of a few of the other build providers that you normally have installed. There are more than a dozen of these on the typical system.

-
.aspx - System.Web.Compilation.PageBuildProvider

-
.asmx - System.Web.Compilation.WebServiceBuildProvider

-
.ashx - System.Web.Compilation.WebHandlerBuildProvider

-
.resx - System.Web.Compilation.ResXBuildProvider

Next time: Customizing a Metadata Resolver