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

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


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.




Application Security Testing: An Integral Part of DevOps

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; [ServiceContract()] public interface IHelloWorldService { [OperationContract] 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"?> <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"> <system.serviceModel> <services> <service name="HelloWorldService" behaviorConfiguration="HelloWorldServiceBehavior"> <endpoint binding="netTcpBinding" bindingConfiguration="PortSharingBinding" contract="IHelloWorldService" /> <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" /> </service> </services> <bindings> <netTcpBinding> <binding name="PortSharingBinding" portSharingEnabled="true"> <security mode="None" /> </binding> </netTcpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior name="HelloWorldServiceBehavior"> <serviceMetadata /> <serviceDebug includeExceptionDetailInFaults="False"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration>

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/ WASHostedService/HelloWorldService.svc/mex

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"?> <configuration> <system.serviceModel> <bindings> <netTcpBinding> <binding name="NetTcpBinding_IHelloWorldService" -------- </binding> </netTcpBinding> </bindings> <client> <endpoint address= "net.tcp://thiru-pc/MyProjects/ DevX/WASHostedService/HelloWorldService.svc" binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IHelloWorldService" contract="IHelloWorldService" name="NetTcpBinding_IHelloWorldService" /> </client> </system.serviceModel> </configuration>

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(); MessageBox.Show(client.HelloWorld("Thiru")); }

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.

Comment and Contribute






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



We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date