Pages

Windows Communication Foundation - Contracts

Contracts are a key concept in WCF.  The WCF framework allows you to get them into a solution at the outset for "contract-first" style development.  A contract is an interface that your service will implement.  You should create these contracts first and then build the service to conform to them.  The service contract defines the operations that the WCF service will implement.

I'll be using the ADO.NET Entity Framework to work with my Data Model in this project.  I've already used the handy tools available in VS2010 to create a ProductsEntityModel project in my solution that contains all the connections and classes I will need to work with my test database: AdventureWorks.



I then created a WCF project.  VS automatically creates a couple of classes for you - IService.cs and Service.cs.  I updated the name of the interface to better reflect the contract that it will define.  Next I started to create the DataContract by using the attribute of the same name to define the ProductData class.  This class contains the DataMembers that will be passed to client applications and these are defined by a DataMember attribute.  Tagging a class with the DataContract attribute identifies it as a class that can be serialized and deserialized by WCF.


namespace Products
{
    // Data contract describing the details of a product passed to client applicaitons
    [DataContract]
    public class ProductData
    {
        [DataMember]
        public string Name;

        [DataMember]
        public string ProductNumber;

        [DataMember]
        public string Color;

        [DataMember]
        public decimal ListPrice;
    }
}

Next I will define the service contract as an interface in the same Products namespace using the ServiceContract attribute.  Every method that should be exposed will be tagged with the OperationContract attribute.


    // Service contract describing the operations provided by the WCF service
    [ServiceContract]
    public interface iProductsService
    {
        // Get the product number of every product
        [OperationContract]
        List ListProducts();

        // Get the details of a single product
        [OperationContract]
        ProductData GetProduct(string productNumber);

        // Get the current stock level for a product
        [OperationContract]
        int CurrentStockLevel(string productNumber);

        // Change the stock level for a product
        [OperationContract]
        bool ChangeStockLevel(string productNumber, short newStockLevel, string shelf, int bin);
    }


Now it is time to implement the service. I'll use the Service.cs class that was created when we made the ProductsService project.  Below you can see that I have defined a ProductsServiceImpl class within a Products namespace.  The ProductsServiceImpl class should inherit the interface that defines our contract. In this case, it is IProductsService.

namespace Products
{
    // WCF service that implements the service contract
    // This implementation provides minimal error checking and exception handling
    public class ProductsServiceImpl : iProductsService
    {
        
    }
}

Within this class, I will implement all of the required methods from the interface. This would include ListProducts, GetProduct, CurrentStockLevel, and ChangeStockLevel.  The next step would be to build a WCF client application that will interact with the service.  I'll save that for a future post.

No comments:

Post a Comment