Using Contract First development

Contract First development comes to us in .NET 4.5 WCF as the ability to create the service interface and data contract from a WSDL file. The WSDL file is generated in the svcutil.exe application with the /servicecontract flag.

This provides an excellent way to parallelize development since we can work on the backend service while it is being constructed, given that the service requirements and contract have been defined beforehand.

In this recipe, we will see how to use this new feature.

Getting ready

In order to use this recipe, you should have Visual Studio 2012 installed and a WSDL contract. For this recipe, we will use the dynamically generated web service we implemented in the first recipe.

How to do it...

In this recipe, we are going to generate a client for a WSDL file.

  1. Open the project of our first recipe and set the ASP.NET website that hosts the asynchronous web service. Navigate on the browser to the web service, appending ?wsdl to the address. You should get something like http://localhost:58997/AsyncService.svc?wsdl. Note that the port might change. It will generate an XML file with the WSDL definition and display it in our browser as follows:
    How to do it...
  2. Navigate to your Visual Studio tools folder and open the VS2012 Tools command prompt corresponding to your CPU.
  3. Navigate to where you want to generate the service code.
  4. Type the following command, noting that the port might change:
    svcutil /sc http://localhost:58997/AsyncService.svc?wsdl
  5. Note that we used the reduced form /sc of the /servicecontract flag on our command.
  6. We should get a notification that ASyncService.cs has been generated, as we can see in the following screenshot:
    How to do it...
  7. We could accomplish this with a WDSL file, with or without an XSD file.
  8. We will find our service contract in the file that was generated by our command prompt, which is AsyncServices.cs in our case.
  9. This file should contain the following code:
    //------------------------------------------------------------------------------
    // <auto-generated>
    //     This code was generated by a tool.
    //     Runtime Version:4.0.30319.17914
    //
    //     Changes to this file may cause incorrect behavior and will be lost if
    //     the code is regenerated.
    // </auto-generated>
    //------------------------------------------------------------------------------
    
     [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    [System.ServiceModel.ServiceContractAttribute(ConfigurationName="IAsyncService")]
    public interface IAsyncService
    {
        [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IAsyncService/DoWork", ReplyAction="http://tempuri.org/IAsyncService/DoWorkResponse")]
    Int DoWork();
    }
  10. If we had any data elements, they would also have been generated.
  11. This technique allows us to generate a client that works against a given service implementation.

How it works...

This recipe is quite straightforward; after getting the WSDL file for our web service, all it takes is for us to use the svcutil tool to generate the service interface and the data contract.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset