September 30, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Distributed Application Communication Using WCF

  • December 30, 2009
  • By Matt Goebel
  • Send Email »
  • More Articles »

Introduction

Windows Communication Foundation (WCF) is a subset of the .NET Framework that is designed to take interoperability in distributed application environments to the next level. With the emergence and acceptance of Web services and their accompanying protocols and standards, development of distributed applications has become a developer norm. WCF simplifies that development by introducing a service- oriented programming model that at its base provides asynchronous and untyped message-passing. Extending from the base are options and protocols designed to give the developer choice in transport and encoding methods among many other configurations.

Those familiar with .NET Remoting, .NET Web services and Enterprises Services will recognize and have a familiar development experience in WCF. In addition to the old tricks, WCF enables serialization capabilities that allow loose coupling and versioning of distributed application across various platforms. This allows development of each application in the environment to be performed more independently and with fewer maintenance issues. Integration with existing .NET Framework technologies such as COM+, Web Services Enhancements (WSE), Message Queuing and others is also provided within WCF.

Windows Communication Foundation comes with its own set of terms and expressions. Before beginning it is important to understand the following:


terms and expressions
Click here for larger image

The following link has additional WCF terms and definitions: http://msdn.microsoft.com/en- us/library/ms731079.aspx

Whether you are developing a Windows Communication Foundation application to communicate across different platforms, across the Internet, or just on the same server, there are several tasks required to build a WCF application. They are listed in order below:

  • Define the service contract
  • Implement the contract
  • Configure the service
  • Host the service
  • Consume the service via a client

The following walkthrough uses a console application to host the service and a WPF client application to consume the service. The WPF client application will retrieve a product list from the service, display it to the user and allow the user to delete individual products. To get started, create a new console application in Visual Studio using the Console Application template and name it WCFProductService.

This walkthrough will use a class called Product. Data will be created at runtime for purposes of the example but a more likely scenario is that you would communicate with a database to retrieve the data.

  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  
  namespace WCFProductService
  {
      public class Product
      {
          public Product()
          { }
  
          public int ProductID { get; set; }
          public string ProductName { get; set; }
          public string ProductDesc { get; set; }
          public int Inventory { get; set; }
  
          public List<Product> GenerateProductList()
          {
              List<Product> returnList = new List<Product>();
  
              returnList.Add(new Product() { ProductID = 1, ProductName = "Ball", ProductDesc = "White, Round", Inventory = 10 });
              returnList.Add(new Product() { ProductID = 2, ProductName = "Bat", ProductDesc = "Wood", Inventory = 7 });
              returnList.Add(new Product() { ProductID = 3, ProductName = "Glove", ProductDesc = "Brown, Leather", Inventory = 3 });
              returnList.Add(new Product() { ProductID = 4, ProductName = "Helmet", ProductDesc = "Head Protection", Inventory = 12 });
              returnList.Add(new Product() { ProductID = 5, ProductName = "Pads", ProductDesc = "Body Protection", Inventory = 12 });
              returnList.Add(new Product() { ProductID = 6, ProductName = "Jersey", ProductDesc = "Team Spirit", Inventory = 2 });
              returnList.Add(new Product() { ProductID = 7, ProductName = "Foam Finger", ProductDesc = "Awesome", Inventory = 23 });
              returnList.Add(new Product() { ProductID = 8, ProductName = "Tape", ProductDesc = "Injury prevention", Inventory = 102 });
              returnList.Add(new Product() { ProductID = 9, ProductName = "Club", ProductDesc = "9 Iron", Inventory = 11 });
              returnList.Add(new Product() { ProductID = 10, ProductName = "Bag", ProductDesc = "Holds Clubs", Inventory = 6 });
              
              return returnList;
          }
      }
  }

Product.cs

Defining the Service Contract

To create the service contract you will first have to add a reference to System.ServiceModel.dll using the Solution Explorer. Once the reference is added to the project you will need to add the System.ServiceModel namespace to the project by adding the following using statement.

using System.ServiceModel;

Program.cs

Next define a new interface called IProductManager and apply the ServiceContract attribute to the interface. For this example specify the Namespace value of the attribute to "http://CompanyName.ProductManager". It is best practice to specify the namespace as it prevents the default namespace value being added to the contract name. This interface will be used later when implementing the service contract.

In the IProductManager interface you will then declare a method for each operation you wish to expose via the contract. Add a method to retrieve the product list and one to delete a product. Add the OperationContract attribute to each method you want to expose. When finished your interface should look similar to the following.

  // ...
  
     [ServiceContract(Namespace="http://CompanyName.WCFProductService")]
     public interface IProductManager
     {
         [OperationContract]
         List GetAllProducts();
         [OperationContract]
         void DeleteProduct(int n1);
     }
  
  // ...

Program.cs

Implementing the Contract

After creating the contract via the interface above you will then need to implement the interface. For this create a class called ProductManagerService that implements the IProductManager interface. Then implement each method that was defined in the interface within the ProductManagerService class. Your code should look similar to the following when finished.

  // ...
  
     public class ProductManagerService : IProductManager
     {
         public List<Product> GetAllProducts()
         {
             if (MasterProductList == null)
                 MasterProductList = new List<Product>();
             if (MasterProductList.Count == 0)
                 MasterProductList = Product.GenerateProductList();
  
             return MasterProductList;
         }
  
         public void DeleteProduct(int n1)
         {
             if (MasterProductList == null)
             {
                 // throw and handle exception
             }
  
             var productToDelete = (from p in MasterProductList
                                   where p.ProductID == n1
                                   select p).FirstOrDefault();
  
             MasterProductList.Remove(productToDelete);
          }
      }
  // ...

Program.cs





Page 1 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel