Steps to perform WCF Windows Activation Service hosting


Tarun Kumar Chatterjee
.Net – Technology Specialist
Published On :   09 Jun 2016
Visit Count
Today :  1    Total :   3362
Plan, Migrate, Secure, Report
SharePoint & Office 365 Tool. Simple & Easy to Use. 15-Day Trial!

Sharegate: Kick-Ass Tool
Think Your SharePoint & Office 365 Are Secure ? Find Out Now!


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

SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Categories

Migratiin Tools for SharePoint