RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Hosting WCF Services in Windows Activation Service : Page 2

You're probably used to hosting WCF services with IIS, but Vista and IIS 7 provide a much more powerful and lighter-weight option that supports additional protocols besides HTTP.

Creating the Service
To start, add a service named HelloWorldService.svc to the WASHostedService project. The newly added HelloWorldService.svc contains the following lines of code.

   <%@ ServiceHost Language="C#" 
     Debug="true" Service="HelloWorldService"
     CodeBehind="~/App_Code/HelloWorldService.cs" %>
Note that the HelloWorldService.svc has a code-behind file named HelloWorldService.cs that Visual Studio places in the App_Code directory. Open up the HelloWorldService.cs file and modify its code as follows:

   using System;
   using System.ServiceModel;
   public interface IHelloWorldService
      string HelloWorld(string input);
   public class HelloWorldService : IHelloWorldService
      public string HelloWorld(string input)
         return "Hello: " + input;
The preceding code decorates the interface IHelloWorldService definition with the [ServiceContract] attribute to indicate that this interface contains the service operation definitions. To expose the individual methods in the interface as service operations, you specify the [OperationContract] attribute. After defining the service contract in an interface, you can then implement the methods inside the actual class—HelloWorldService in this case.

After creating the service, you need to specify the service behavior in the configuration file. Configuring the service behavior in an external configuration file lets you customize the service behavior without having to modify and recompile the service code. To host the HelloWorldService in WAS, modify the Web.config file in the service project as follows:

   <?xml version="1.0"?>
         <service name="HelloWorldService"
          <endpoint binding="netTcpBinding" 
            contract="IHelloWorldService" />
            <endpoint address="mex" binding="mexTcpBinding"
               contract="IMetadataExchange" />
           <binding name="PortSharingBinding" 
             <security mode="None" />
           <behavior name="HelloWorldServiceBehavior">
             <serviceMetadata />
Through its attributes, the <service> element under <system.ServiceModel>\<services> specifies the name of the service as well as the name of the behavior configuration that you want to use to control the service's behavior. The <service> element also contains another child element named <endpoint> where you specify the address, contract, and binding for the service. Note that the contract attribute is set to the name of the interface—IHelloWorldService. To expose the service through TCP binding, set the binding attribute to netTcpBinding. Finally, the separate <netTcpBinding> element defines specific characteristics of the TCP binding, such as security.

Creating the Client
To test the HelloWorld service, create a new Visual C# Windows Forms application and name it WASHostedServiceClient.

Figure 2. Svcutil.exe Output: The Svcutil.exe utility downloads a service's WSDL file using that and related service metadata to create a client side proxy and configuration file.
Next, you'll need to create a proxy for the WCF service described earlier in this article. You'll also need to create a configuration file containing the settings required to connect to the service. You can accomplish both tasks through the Service Model Metadata Utility (svcutil.exe), which builds a proxy and corresponding configuration file based on a published service's metadata. For example, here's the command to run svcutil.exe to create a proxy and configuration file for the HelloWorldService:

   svcutil.exe net.tcp://localhost/MyProjects/DevX/
Figure 2 shows the output produced by the preceding command.

The output from Figure 2 has two files:
  1. A WCF proxy (a C# class file in this case) that translates method calls to messages dispatched to the service.
  2. An output.config file (with the settings based on the service configuration) that clients can use to communicate with the service.
Now add the created proxy file to the WASHostedServiceClient project. Also rename the output.config file created with svcutil to App.config and add that to the WASHostedServiceClient project as well. The App.config file contains a number of configuration entries related to the invocation behavior of the client. The following code highlights the important sections of the App.config file:

   <?xml version="1.0" encoding="utf-8"?>
           <binding name="NetTcpBinding_IHelloWorldService" 
         <endpoint address=
           name="NetTcpBinding_IHelloWorldService" />
The <system.ServiceModel> element is the root of the service client configuration section. Inside that are the <bindings> and <client> elements where you specify the binding details and endpoint details respectively.

At this point, you are ready to consume the service by writing code against the proxy. To do that, add a command button named btnHelloWorld to the form and modify its Click event-handling code as follows:

   private void btnHelloWorld_Click(object sender, EventArgs e)
      HelloWorldServiceClient client = new HelloWorldServiceClient();
The implementation simply invokes the HelloWorld() method of the HelloWorldService using the proxy class (generated through the svcutil utility) from the previous section and displays the results of the invocation via a message box.

You've seen the steps involved in using TCP binding to host the service in WAS. The next section discusses how to use Named Pipe binding to host a service in WAS.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date