Partager via


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

Comments

  • Anonymous
    January 12, 2007
    I'm making multiple calls to a service and all of the other calls sit waiting until the first call completes.

  • Anonymous
    January 12, 2007
    如何将已有的WebService升级到WCFReplacinganExistingASMXServicewithWCFhttp://blogs.msdn.com/drnick/arch...

  • Anonymous
    January 14, 2007
    This is good information. What if I have multiple ASMX files that I'm slowly migrating to WCF? Is there a way to make this work without overriding the build provider in the web.config?

  • Anonymous
    January 14, 2007
    Segnalo un post che trovo molto interessante, relativo a come sostituire servizi ASMX con WCF, semplicemente...

  • Anonymous
    January 14, 2007
    Mike- The granularity of controlling this is at the vdir/subdir level.  If you can migrate applications over one directory at a time, then you can make the build provider method work.  Otherwise, you'll probably need to have a shim service so that you can forward the calls.

  • Anonymous
    October 02, 2007
    IIS uses the file extension to register behaviors that take place when a particular address is requested.