Web service to wcf conversion

Life is 10% what happens to us and 90% how we react to it. If you don't build your dream, someone else will hire you to help them build theirs.

Web service to wcf conversion

For converting an asmx service to WCF we need to take following steps.

  1. 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
  2. Go to Visual Studio Command Prompt
  3. 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
  4. The svcutil will generate two files .cs file and .config file. The .cs file will contain your service contract.
  5. Create a new WCF Service Application in your solution, This template creates a service1.svc in your project, remove all files associated with Service1
  6. Add a new WCF Service by using add item and name it as your own service e.g. Queue
  7. Delete the interface file created in above step.
  8. Copy your .cs file generated by svcutil to your WCF service application folder and include it in your project.
  9. 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.
  10. In your generated interface file, remove , ReplyAction = “*” string for each operation.
  11. Implement the above interface in your implementation class
  12. Build your project, and you should be good to go.
  13. You can now implement your methods by copying your old code to new one or by writing it new.
  14. 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.