Standards for Technology in Automotive Retail

 
 Home -  News Feed 

7.4. WCF Walkthrough

Procedure 7.1. Steps to constructing a WCF service

  1. Service Contract

    The service contract outlines the operations that will be available on the service. The service contract is defined by decorating the service class or interface with the ServiceContract attribute. Arguments to the attribute should include the name of the configuration which links it to the settings in the configuration file, as well as the SessionMode argument enabling session-ful conversations. Allowing session in the service contract is required for Reliable Messaging since the conversation must maintain state over many requests. In the following example, the configuration name is set to starTransportPortTypes and the SessionMode is set to Allowed in the ServiceContractAttribute to enable session-ful reliable messaging conversations.

    The svcutil.exe utility which comes with Visual Studio 2008 will create the service contract and supporting types for you automatically based on the STAR WSDL and XSD schema files. However, the contract will have to be modified to remove the Action qualifiers and other attributes which interfere with the WS-Addressing implementation. Failure to remove these will result in the service showing no operations available.

    One can use Visual Studio 2008’s WCF service project type to quickly create a skeleton WCF project.

    Figure 7.1. You can select WCF as a project type. Visual Studio will generate a skeleton WCF project.

    You can select WCF as a project type. Visual Studio will generate a skeleton WCF project.

    The example contract below (modified from the standard svcutil output) should work for any basic STAR service in WCF. The types ProcessMessageRequest, ProcessMessageResponse, PutMessageRequest, and etc. can be used as-is from the svcutil.exe output.

    [System.ServiceModel.ServiceContractAttribute(
        ConfigurationName="starTransportPortTypes", 
        SessionMode=System.ServiceModel.SessionMode.Allowed)]
    public interface starTransportPortTypes
    {
        
        [System.ServiceModel.OperationContractAttribute()]
        [System.ServiceModel.XmlSerializerFormatAttribute()]
        ProcessMessageResponse ProcessMessage(ProcessMessageRequest request);
        
        [System.ServiceModel.OperationContractAttribute()]
        [System.ServiceModel.XmlSerializerFormatAttribute()]
        PutMessageResponse PutMessage(PutMessageRequest request);
        
        [System.ServiceModel.OperationContractAttribute()]
        [System.ServiceModel.XmlSerializerFormatAttribute()]
        PullMessageResponse PullMessage(PullMessageRequest request);
    }				
    				

    To use this contract, simply open up the file IService1.cs that was auto-generated by Visual Studio and replace its contract with the one above.

  2. Service Configuration

    The service configuration sets up the behaviors available in the web service and configures the endpoints that will be available. Note that the endpoint address MUST match the address that the public will use to access the site for the default WS-Addressing to work properly. The “mex” endpoint provides the mechanism by which the WSDL can be retrieved with the “?WSDL” query string. Removing this endpoint prevents retrieval of the WSDL in this fashion.

    The “WCF Service Configuration Editor” tool in Visual Studio 2008 (SvcConfigEditor.exe) is an easy way to set up the configuration file. It can also be used to enable diagnostics for tracing messages and viewing SOAP headers to examine the operation of the service.

    Figure 7.2. Service configuration

    Service configuration

    To use the configuration above, simply open the file called web.config that was auto-generated by Visual Studio 2008 and replace its code with the code above.

    Figure 7.3. Select web.config

    Select web.config

  3. Service Implementation

    With the service contract defined, the implementation must be created. Note the use of the ServiceBehavior class attribute, which allows incoming request contexts to retain a constant session while the Reliable Messaging conversation takes place. The code below can replace the default code that was auto-generated in the file Service1.svc.

    [System.ServiceModel.ServiceContractAttribute(
        ConfigurationName="starTransportPortTypes", 
        SessionMode=System.ServiceModel.SessionMode.Allowed)]
    public interface starTransportPortTypes
    {
        
        [System.ServiceModel.OperationContractAttribute()]
        [System.ServiceModel.XmlSerializerFormatAttribute()]
        ProcessMessageResponse ProcessMessage(ProcessMessageRequest request);
        
        [System.ServiceModel.OperationContractAttribute()]
        [System.ServiceModel.XmlSerializerFormatAttribute()]
        PutMessageResponse PutMessage(PutMessageRequest request);
        
        [System.ServiceModel.OperationContractAttribute()]
        [System.ServiceModel.XmlSerializerFormatAttribute()]
        PullMessageResponse PullMessage(PullMessageRequest request);
    }
    				

    Figure 7.4. Select Service file

    Select Service file

  4. Client Setup

    To create a client which will talk to our service, we create a service proxy with svcutil.exe or the tool of your choice. Just like we have on the server side, the client must configure the endpoint behaviors in the configuration file and enable reliable sessions.

    Figure 7.5. Example client service proxy

    Example client service proxy