Steps to perform WCF Windows Activation Service hosting

Tarun Kumar Chatterjee
 
Net – Technology Specialist
June 9, 2016
 
Rate this article
 
Views
5715

Hosting WCF in Activation service takes many advantages such as process recycling, isolation, idle time management and common configuration system. WAS hosted service can be created using following steps

· Enable WCF for non-http protocols

· Create WAS hosted service

· Enable different binding to the hosted service

Before Start creating the service we need to configure the system to support WAS. Following are the step to configure WAS.

Click Start –> Control Panel –> programs and Features and click ‘Turn Windows Components On or Off’ in left pane.

Expand ‘Microsoft .Net Framework 3.5.1’ and enable "Windows Communication Foundation HTTP Activation" and "Windows Communication Foundation Non- HTTP Activation".

clip_image002

Now we will be creating WAS Hosted Service

Start the Visual Studio and click File — >New — >Web Site. Select the ‘WCF Service Application’ click OK.

clip_image004

I have created a service named as MyService, which will have an Add method will accept two integer values as input and return the summation of passing parameters value. Interface implementation of the Service is shown below.

Add a WCF service with the following codes

clip_image006

Below is the WASHostedWCFService interface code:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Runtime.Serialization;
 using System.ServiceModel;
 using System.Text;
 
 namespace WASHostedWCFService
 {    
     [ServiceContract]
     public interface IMyService
     {
         [OperationContract]
         int Add(int num1, int num2);
     }
 }
 
 
 //Below is the interface implement code: 
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Runtime.Serialization;
 using System.ServiceModel;
 using System.Text;
 
 namespace WASHostedWCFService
 {
     public class MyService : IMyService
     {
         public int Add(int num1, int num2)
         {
             return num1 + num2;
         }
     }
 }
 

Here is my web.config code

 <?xml version="1.0"?>
 <configuration>
 
   <appSettings>
     <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
   </appSettings>
   <system.web>
     <compilation debug="true" targetFramework="4.5.1" />
     <httpRuntime targetFramework="4.5.1"/>
   </system.web>
   <system.serviceModel>
     <services>
       <service name="WASHostedWCFService.MyService" behaviorConfiguration="ServiceBehavior">
         <!-- Service Endpoints -->
         <endpoint binding="netTcpBinding" contract="WASHostedWCFService.IMyService" >
         </endpoint>
         <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
       </service>
     </services>
     
     <behaviors>
       <serviceBehaviors>
         <behavior name="ServiceBehavior">
           <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
           <serviceMetadata httpGetEnabled="true" />
           <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
           <serviceDebug includeExceptionDetailInFaults="false"/>
         </behavior>
       </serviceBehaviors>
     </behaviors>    
   </system.serviceModel>
   <system.webServer>
     <modules runAllManagedModulesForAllRequests="true"/>
     <!--
         To browse web app root directory during debugging, set the value below to true.
         Set to false before deployment to avoid disclosing web app folder information.
       -->
     <directoryBrowse enabled="true"/>
   </system.webServer>
 
 </configuration>
 

To enable different binding to the hosted service, Go to the Start menu -> Programs ->Accessories. Right click on the "Command Prompt" item, and select "Run as administrator" from the context menu.

Execute the following command

appcmd set app "Default Web Site/WASHostedWCFService" /enabledProtocols:http,net.tcp

Output will be shown below

clip_image008

Now the service is ready to use.

Now we will be creating the client application as shown below and add the reference ‘System.ServiceModel’, this is the core dll for WCF.

clip_image010

Next we can create the proxy class using service utility and add the proxy class to the client application. Creat the proxy class using Visual Studio Command prompt and execute the command

svcutil.exe net.tcp://localhost/WASHostedWCFService/MyService.svc

clip_image012

Add the generated proxy class and content of output.config file in App.config to WASHostedWCFClient application.

Within the Program.cs add the following code:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 
 namespace WASHostedWCFClient
 {
     class Program
     {
         static void Main(string[] args)
         {
             MyServiceClient objMyServiceClient = new MyServiceClient();
             Console.WriteLine(objMyServiceClient.Add(2, 3).ToString());
             Console.ReadLine();
         }
     }
 }
 
 

Now build and run the client application, it will call the service and return the summation as 5.

clip_image014

Happy Coding

Tarun Kumar Chatterjee

Category : .Net

Author Info

Tarun Kumar Chatterjee
 
Net – Technology Specialist
 
Rate this article
 
Tarun has been working in IT Industry for over 12+ years. He holds a B-tech degree. He is passionate about learning and sharing the tricks and tips in Azure, .Net ...read more
 

Leave a comment