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 class
MyService : System.Web.Services.
WebService
{
[
WebMethod
]
[
OperationContract
]
public string
Hello(
string
name)
{
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 🙂