October 31, 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 »

Configuring the Service

After the service contract has been implemented you are now ready to configure the service. Configuring the service involves determining and then defining how the service will be exposed, the location where it will be found, the type of security and message encoding used to send and receive messages. Windows Communication Foundation provides an abundance of options when performing configuration.

There are two basic ways to set the configuration for the service. The first option is to use inline code which the walkthrough uses for configuration. The second option uses configuration files and takes full advantage of the .NET Framework's configuration technology. When the WCF service is hosted in IIS for example the configuration would be part of the Web.config file.

For those looking to gain efficiency through reuse, WCF includes common configurations in the form of bindings that allow you to quickly set up the more basic features. Bindings can be used either inline or through XML elements in configuration files. In the walkthrough, WCFProductService uses the BasicHttpBinding binding in code to configure the service. The BasicHttpBinding does not provide security by default so therefore should only be used in secured network environments or with added security. A full list of WCF provided bindings and details to make a determination on which binding would work best for your situation can be found at http://msdn.microsoft.com/en- us/library/ms730879.aspx.

Within the Main method add a BasicHttpBinding object with the following properties.

  BasicHttpBinding binding = new BasicHttpBinding();
  binding.MessageEncoding = WSMessageEncoding.Text;
  binding.Security.Mode = BasicHttpSecurityMode.None; // default

Program.cs

There are many additional properties on the object that configure various different aspects of the service. Take some time to get familiar with the options as not all need to be configured but are useful to know when determining the best configuration for your service.

The next configuration step is for the endpoints. Endpoints are where WCF services are 'found' and where you go to gather information about the WCF service. The contract, configuration information and the address come together to comprise an endpoint. The contract and configuration has already been coded leaving the address, or endpoint location, to be implemented.

In the Main method create a new Uri object for the service's base address.

  Uri address = new Uri("http://localhost:8000/WCFProductService");

Program.cs

The last configuration required is to make the service discoverable through metadata. In the System.ServiceModel.Description namespace there is a class called ServiceMetadataBehavior that allows you to configure your WCF service to publish metadata about itself when requested. For those familiar with ASP.NET Web services this would be similar to discovery and generating the WSDL and adding a Web Reference to your project. Instead now you are making a service discoverable so a Web Service proxy can be created on your client as you will see later in this article.

To enable metadata discovery first add a using reference to System.ServiceModel.Description. Next create a new ServiceMetadataBehavior object in the Main method and set its HttpGetEnabled property to true. In the next step you will add the behavior to the ServiceHost object.

  using System.ServiceModel.Description;
  
  // ...
  
  ServiceMetadataBehavior smdb = new ServiceMetadataBehavior();
  smdb.HttpGetEnabled = true;

Program.cs

Note that by default metadata publishing is disabled due to security concerns with overexposing your service's surface to attack. Alternatives exist that do not require you to publish the service's metadata. Using the ServiceModel Metadata Utility Tool (discussed later) you are still able to generate metadata and client code directly from your service assemblies.

Hosting the Service

Now that the service has been defined, implemented and configured it is time to create the service host object to host the service and expose the service via an endpoint. The first step is to create a new ServiceHost object to host the WCF service. Use the base address Uri object created in the previous step and for the implementation of the service contract use ProductManagerService.

Next add a service endpoint to the ServiceHost object. For parameters pass in the interface that is to be exposed (IProductManager), the previously created binding object and the endpoint's address. Note that the address used here is relative to the base address used by the ServiceHost object. Then, before starting the service, add the ServiceMetadataBehavior object created in the previous step to the ServiceHost object's Description.Behaviors collection.

The Open() method of the ServiceHost object tells the service to start waiting for incoming messages and the Close() method stops the service. When finished your Main method in Program.cs should look similar to the following.

 // ...
          
  static void Main(string[] args)
  {
      BasicHttpBinding binding = new BasicHttpBinding();
      binding.MessageEncoding = WSMessageEncoding.Text;
      binding.Security.Mode = BasicHttpSecurityMode.None; // default
          
      Uri address = new Uri("http://localhost:8000/WCFProductService");
          
      ServiceHost productServiceHost = new 
        ServiceHost(typeof(ProductManagerService), address);
          
      productServiceHost.AddServiceEndpoint(typeof(IProductManager),
                  binding, "ProductManager");
          
      ServiceMetadataBehavior smdb = new ServiceMetadataBehavior();
      smdb.HttpGetEnabled = true;
          
      try
      {
        productServiceHost.Description.Behaviors.Add(smdb);                                   
                          
      productServiceHost.Open();
      Console.WriteLine("ProductManager Service is open!");
             Console.ReadLine();
          
             productServiceHost.Close();
      }
      catch (CommunicationException ce)
      {
      // handle/log exception, clean up ServiceHost...
             productServiceHost.Abort();
      }
  }
          
// ...
        

Program.cs

Before moving on, take a minute to familiarize yourself with all the different options available in creating the ServiceHost object and adding the endpoint. One of the great benefits of Windows Communication Foundation is how many options you have and how configurable it is. At the same time it can also be a source of overwhelming confusion if you have not taken the time to understand all of what is available.

To test that the service is working run the console application from Visual Studio (for Vista and Windows 7 users make sure you are running VS as an administrator) and open IE to the service's debug page located at http:// localhost:8000/WCFProductService.

Note: If you are running Windows 7 you may need to run the following command from an administrator command prompt to view the page in IE.

netsh http add urlacl url=http://+:8000/WCFProductService user=[username]

Administrative Command Prompt command





Page 2 of 3



Comment and Contribute

 


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

 

 


Sitemap | Contact Us

Rocket Fuel