Tuesday, February 19, 2008

Convert existing asmx .net web service to WCF service in .net 3.0/3.5

If you are thinking what if i already have an existing web service .asmx which are exposed to existing clients already but still want to use the new technology infrastructure then you can do it really sweet. (why you would do that...must check new features of WCF on msdn:))

So if you have existing web service like this - http://mycompany/myWebservice.asmx
Steps to have clients use .asmx extension but behind scenes use powerful WCF here they are -
1> Decorate web service class name and web method with ServiceContract and OperationContract atrributes. For this you should add reference System.ServiceModel.dll assembly that is part of .net 3.5 framework. You can download .net 3.5 framework from to have WCF services run on your machines just fine.

Once you install you should found this installed in GAC.

here's how it looks-

using System.ServiceModel;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

[ServiceContract(Namespace="http://tempuri.org")]
public class myWebService : System.Web.Services.WebService
{
[WebMethod]
[OperationContract]
public string HelloWorld(string name)
{
return string.Format("Hello, {0}",name);
}
}

2> add this system.serviceModel section into your existing web.config of the asmx web service -




3> you can run the web service now and it should compile and run without issues with asmx extension but one more thing you would have to do to tell .net runtime to use WCF service runtime is add buildprovider under Compilation section.



4> Last but not the least modify your markup code .asmx to have something like this -% @ServiceHost language = "c#" Service="yourNamespace.ClassnameOftheService" %

This shuold help you up and running WCF service still keeping the existing asmx extension!

If you are done ...great! After i completed all the above steps i encountered pretty wierd errors saying -
Service 'myWebservice' has zero application (non-infrastructure) endpoints. This might be because no configuration file was found for your application, or because no service element matching the service name could be found in the configuration file, or because no endpoints were defined in the service element.

Reason - that was because my web.config had wrong endpoint service name reference.
service name="myWebService" behaviorConfiguration="returnFaults"

endpoint binding="basicHttpBinding" contract="myWebService"

Rectifying that helped...

If you still continue to get error you may also want to verify that you have your config file as web.config instead of app.config.

Hope that helps!

Here are few links for further information and references used here -
http://blogs.msdn.com/wenlong/archive/2007/09/18/how-to-use-asmx-extension-to-handle-wcf-requests.aspx

http://www.topxml.com/rbnews/WSCF/WCF/re-44738_Phased-Migration-From-ASMX-to-WCF.aspx

For more on WCF visit previous posts here - http://archdipesh.blogspot.com/search/label/WCF

Cheers - Dipesh

No comments: