Migrating ASMX to WCF

One question I was often asked last week was: “I have a bunch of ASMX services today, what is the best way for me to prepare for and/or migrate to Indigo”? (also asked as “can you tell me more about this ASMX migration thing?”). There are a number of ways to go about this, but Kirk and I came across a compelling approach that I will elaborate on here. Before we begin, if you need some background on the differences between ASMX 1.0, ASMX 2.0, and WCF, check out Aaron Skonnard’s MSDN article.

First we assume that you have a ASMX service implemented using CodeBehind. If you don’t, then your first step is to move your inline .asmx code into a .cs file 🙂

Now, let’s say you have the following ASMX service:

[WebService(Namespace=”http://kennyw.com/sampleservices/”)]
public class MyService : System.Web.Services.WebService
{
[WebMethod]
public string Hello(string name)
{
return string.Format(“Hello {0}.”, name);

}

}

Which is referenced from an .asmx file containing the reference directive such as the following:
<%@WebService Language=”C#” class=”MyService” %>

You can expose this ASMX service to WCF in 3 simple steps.

  1. Annotate your ASMX service with WCF attributes.
    Add [ServiceContract] to the classes you want to expose through WCF, and [OperationContract] to the methods. Our modified class looks like:

    [ServiceContract(Namespace=”http://kennyw.com/WCFservices/”)]
    [WebService(Namespace=”http://kennyw.com/sampleservices/”)]
    public class MyService : System.Web.Services.WebService
    {
    [WebMethod]
    [OperationContract]
    public string Hello(string name)
    {
    return string.Format(“Hello {0}.”, name);

    }

    }

  2. Create a .svc file in your virtual directory that contains the following declaration (which is very reminiscent of your .asmx file :)):
    <%@ServiceHost Language=”C#” Service=”MyService” %>
  3. Add a snippit to your web.config file (create one if you don’t already have one in your vdir) to add an HTTP binding for your service:
    <system.serviceModel>
      <services>
        <service type=”MyService”>
          <endpoint binding=”basicHttpBinding”
            contract=”MyService”
          />
        </service>
      </services>
    </system.serviceModel>

Viola! Now you can access your service file using both Indigo clients (using the .svc file) and ASMX clients (using the .asmx file). This will get you started on the migration path. You can add more functionality to other methods and expose those methods to new Indigo clients, and if you aren’t using any “HTTP-isms” (i.e. HttpContext.Current and friends) then you will also be able to add net.tcp and/or net.pipe bindings on Vista.

There are of course many more details to cover on this topic. For those going to VSLive! next week, Steve Maine will be covering this topic in much greater detail at the WCF for ASP.NET Developers session. Hopefully he will post some of his wise words following the talk on his blog 🙂

8 thoughts on “Migrating ASMX to WCF

  1. Pingback: Kirk Allen Evans' Blog

  2. Pingback: mattonsoftware.com : .NET Resources

  3. Debasish Pramanik

    I’m trying to understand WCF so the question may be stupid as I’m still not clear with the entire stuff but would like to ask you..Once I migrate my ASMX to WCF then can I change to binding to Remoting as sometime the deployment can be a single machine then to improve performance I would like to opt for this….

    Reply
  4. Pingback: Even a chimp can write code: Using WCF In An XBAP : Ashish Shetty's weblog

  5. Pingback: Juan Wajnerman » ASMX to WCF migration

  6. Kenny

    Debasish, there is no “remoting binding” out of the box for WCF. While you can write a custom binding to squirrel away a transport via Remoting, there are a number of restrictions to your objects if you want to conform to best practice Service-Oriented principles (such as avoiding MarshalByRef classes).

    Reply
  7. Pingback: Kirk Allen Evans's Blog : Phased Migration From ASMX to WCF

  8. Pingback: Good Links on WCF « Maverick

Leave a Reply

Your email address will not be published. Required fields are marked *