For converting an asmx service to WCF we need to take following steps.
- Create a .wsdl file from current asmx service, you can do this by typing the service url?wsdl in IE and then save the file as .wsdl extension
- Go to Visual Studio Command Prompt
- Go to the directory where you saved wsdl file and run svcutil.exe with wsdl file name as argument e.g. svcutil.exe HelloService.wsdl
- The svcutil will generate two files .cs file and .config file. The .cs file will contain your service contract.
- Create a new WCF Service Application in your solution, This template creates a service1.svc in your project, remove all files associated with Service1
- Add a new WCF Service by using add item and name it as your own service e.g. Queue
- Delete the interface file created in above step.
- Copy your .cs file generated by svcutil to your WCF service application folder and include it in your project.
- Change the file name to indicate it as an interface e.g. IHelloService.cs. You need to change the class name also and its references in the class.
- In your generated interface file, remove , ReplyAction = “*” string for each operation.
- Implement the above interface in your implementation class
- Build your project, and you should be good to go.
- You can now implement your methods by copying your old code to new one or by writing it new.
- In order to test, browse your service in browser or add a service reference to a test project, and it should generate similar interface at your end.
This approach will choose appropriate serializer depending on the types defined in your asmx service.
Notes:
- Your WCF web service class no longer inherits from the System.Web.Services.WebService class so remove it.
- Change the System.Web.Services.WebService attribute on the web service class to theSystem.ServiceModel.ServiceContract attribute.
- Change the System.Web.Services.WebMethod attribute on each web service method to theSystem.ServiceModel.OperationContract attribute.
- Substitute the .ASMX file with a new .SVC file with the following header:
<% @ServiceHost Service="MyNamespace.MyService" %>
- Modify the application configuration file to create a WCF endpoint that clients will use to send their requests to:
<system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="MetadataEnabled"> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="true" /> </behavior> </serviceBehaviors> </behaviors> <services> <service name="MyNamespace.MyService" behaviorConfiguration="MetadataEnabled"> <endpoint name="HttpEndpoint" address="" binding="wsHttpBinding" contract="MyNamespace.IMyService" /> <endpoint name="HttpMetadata" address="contract" binding="mexHttpBinding" contract="IMetadataExchange" /> <host> <baseAddresses> <add baseAddress="http://localhost/myservice" /> </baseAddresses> </host> </service> </services> </system.serviceModel>
- Decorate all classes that are exchanged by the web service methods as parameters or return values with the System.RuntimeSerialization.DataContract attribute to allow them to be serialized on the wire.
- Decorate each field of the data classes with theSystem.RuntimeSerialization.DataMember attribute to include it in the serialized message.