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.
- 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 classMyService : System.Web.Services.WebService
{[WebMethod]
[OperationContract]
public stringHello(stringname)
{return string.Format(”Hello {0}.”, name);}
}
- 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” %> - 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 ![]()
February 1st, 2006 at 6:00 am
Migrating ASMX to Indigo: The Double-Decorator Pattern…
Kenny has a blog post about our discovery of the Double-Decorator pattern for services.
We were fortunate……
May 6th, 2006 at 11:50 pm
[…] […]
April 5th, 2007 at 6:22 am
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….
May 15th, 2007 at 12:31 pm
[…] see this article. If you already have ASMX services for which you were using an ASMX client, here’s how to migrate ASMX to WCF. I should add, you are already able to access a WCF service using an ASMX client.Labels: WCF, […]
May 31st, 2007 at 8:48 am
[…] Wolf describes in a post how to reuse an implementation of an ASMX Web Service, “double-decorating” the classes […]
May 31st, 2007 at 10:07 am
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).
September 24th, 2007 at 1:26 pm
[…] existing clients. Is this possible? Turns out there is a pretty cool way to handle this. Remember Kenny’s double-decorator post? You can take your existing ASMX and overlay WCF attributes on the WebServiceAttribute decorated […]
February 12th, 2008 at 9:40 pm
[…] Migrating ASMX to WCF […]