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.
- Your WCF web service class no longer inherits from the
System.Web.Services.WebServiceclass so remove it.
- Change the
System.Web.Services.WebServiceattribute on the web service class to the
- Change the
System.Web.Services.WebMethodattribute on each web service method to the
- 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:
Reference books, you may like:Programming WCF Services: Design and Build Maintainable Service-Oriented Systems
WCF Multi-Layer Services Development with Entity Framework, 4th Edition
Learning WCF: A Hands-on Guide
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
<add baseAddress="http://localhost/myservice" />
- Decorate all classes that are exchanged by the web service methods as parameters or return values with the
System.RuntimeSerialization.DataContractattribute to allow them to be serialized on the wire.
- Decorate each field of the data classes with the
System.RuntimeSerialization.DataMemberattribute to include it in the serialized message.
© 2015, admin. All rights reserved.