Quantcast
Viewing all articles
Browse latest Browse all 10

WCF Hosting and Types

WCF service cannot exist on its own; it has to be hosted in windows process called as host process. Single host process can host multiple servers and same service type can be hosted in multiple host process. As we discussed there are mainly four different way of hosting the WCF service.

1.      IIS hosting

2.      Self hosting

3.      Windows Activation Service

4.      Windows Service

Multiple hosting and protocols supported by WCF.Microsoft has introduced the WCF concept in order to make distributed application development and deployment simple.

Hosting Environment

Supported protocol

Windows console and form application

HTTP,net.tcp,net.pipe,net.msmq

Windows service application (formerly known as NT services)

HTTP,net.tcp,net.pipe,net.msmq

Web server IIS6

http, wshttp

Web server IIS7 - Windows Process Activation Service (WAS)

HTTP,net.tcp,net.pipe,net.msmq

 A summary of hosting options and supported features.

Feature

Self-Hosting

IIS Hosting

WAS Hosting

Executable Process/ App Domain

Yes

Yes

Yes

Configuration

App.config

Web.config

Web.config

Activation

Manual at startup

Message-based

Message-based

Idle-Time Management

No

Yes

Yes

Health Monitoring

No

Yes

Yes

Process Recycling

No

Yes

Yes

Management Tools

No

Yes

Yes

The main advantage of hosting service in IIS is that, it will automatically launch the host process when it gets the first client request. It uses the features of IIS such as process recycling, idle shutdown, process health monitoring and message based activation. The main disadvantage of using IIS is that, it will support only HTTP protocol.

Let as do some hands on, to create service and host in IIS

Step 1: Start the Visual Studio 2008 and click File->New->Web Site. Select the 'WCF Service' and Location as http. This will directly host the service in IIS and click OK.

Image may be NSFW.
Clik here to view.

Step 2: I have created sample HelloWorld service, which will accept name as input and return with 'Hello' and name. Interface and implementation of the Service is shown below.

IMyService.cs
[ServiceContract]

public interface IMyService

{

    [OperationContract]

    string HelloWorld(string name);  

}

MyService.cs
public class MyService : IMyService

{

    #region IMyService Members

    public string HelloWorld(string name)

    {

        return "Hello " + name;

    }

    #endregion

}

Step 3: Service file (.svc) contains name of the service and code behind file name. This file is used to know about the service.

MyService.svc

<.%@ ServiceHost Language="C#" Debug="true" Service="MyService" CodeBehind="~/App_Code/MyService.cs"%.>

Step 4: Server side configurations are mentioned in the config file. Here I have mention only one end point which is configured to 'wsHttpBinding', we can also have multiple end point with differnet binding. Since we are going to hosted in IIS. We have to use only http binding. We will come to know more on endpoints and its configuration in later tutorial. Web.Config

<.system.serviceModel.>
  <
.services.>
   <
.service behaviorConfiguration="ServiceBehavior" name="MyService".>
            <
.endpoint address="http://localhost/IISHostedService/MyService.svc" binding="wsHttpBinding" contract="IMyService".>
            <
.identity.>
            <
.dns value="localhost"/.>
            <
./identity.>
            <
./endpoint.>

            <.endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/.>
   <
./service.>
<
./services.>

 <.behaviors.>
  <
.serviceBehaviors.>
    <
.behavior name="ServiceBehavior".>
            <
.!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above 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.>

Note: You need to mention the service file name, along with the Address mention in the config file. IIS Screen shot

Image may be NSFW.
Clik here to view.

This screen will appear when we run the application.

Image may be NSFW.
Clik here to view.

Step 5: Now we successfully hosted the service in IIS. Next we have to consume this service in client application. Before creating the client application, we need to create the proxy for the service. This proxy is used by the client application, to interact with service. To create the proxy, run the Visual Studio 2008 command prompt. Using service utility we can create the proxy class and its configuration information.

svcutil  http://localhost/IISHostedService/MyService.svc

Image may be NSFW.
Clik here to view.

After executing this command we will find two file generated in the default location.

·         MyService.cs - Proxy class for the WCF service

·         output.config - Configuration information about the service.

Step 6: Now we will start creating the Console application using Visual Studio 2008(Client application).

Image may be NSFW.
Clik here to view.

Step 7: Add the reference 'System.ServiceModel'; this is the core dll for WCF.

Image may be NSFW.
Clik here to view.

Step 8: Create the object for the proxy class and call the HelloWorld method.

static void Main(string[] args)
{

                        //Creating Proxy for the MyService

                        MyServiceClient client = new MyServiceClient();

                        Console.WriteLine("Client calling the service...");

                        Console.WriteLine(client.HelloWorld("Ram"));

                        Console.Read();

}

Step 9: If we run the application we will find the output as shown below.

Image may be NSFW.
Clik here to view.

WCF Hosting (Self Hosting)
In web service, we can host the service only in IIS, but WCF provides the user to host the service in any application (e.g. console application, Windows form etc.). Very interestingly developer is responsible for providing and managing the life cycle of the host process. Service can also be in-pro i.e. client and service in the same process. Now let's us create the WCF service which is hosted in Console application. We will also look in to creating proxy using 'ClientBase' abstract class.

Note: Host process must be running before the client calls the service, which typically means you have to prelaunch it.

Step 1: First let's start create the Service contract and it implementation. Create a class Library and name it as MyCalculatorService. This is simple service which returns addition of two numbers.

Image may be NSFW.
Clik here to view.

Step 2: Add the System.ServiceModel reference to the project.

Image may be NSFW.
Clik here to view.

Step 3: Create an ISimpleCalculator interface, Add ServiceContract and OperationContract attribute to the class and function as shown below. You will know more information about these contracts in later session. These contracts will expose method to outside world for using this service.

IMyCalculatorService.cs
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel;

namespace MyCalculatorService
{

    [ServiceContract()]

    public interface ISimpleCalculator

    {

        [OperationContract()]

        int Add(int num1, int num2);

    }

}

Step 4: MyCalculatorService is the implementation class for IMyCalculatorService interface as shown below.

MyCalculatorService.cs
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace MyCalculatorService
{

    class SimpleCalculator : ISimpleCalculator

    {

        public int Add(int num1, int num2)

        {

            return num1 + num2;

        }

    }

}

Step 5: Now we are ready with service. Let's go for implementing the hosting process. Create a new console application and name it as 'MyCalculatorServiceHost'. Now take the reference of dll you created in last steps.

Image may be NSFW.
Clik here to view.

Step 6: ServiceHost is the core class use to host the WCF service. It will accept implemented contract class and base address as contractor parameter. You can register multiple base addresses separated by commas, but address should not use same transport schema.

Uri httpUrl = new Uri("http://localhost:8090/MyService/SimpleCalculator");

Uri tcpUrl = new Uri("net.tcp://localhost:8090/MyService/SimpleCalculator");

ServiceHost host = new ServiceHost(typeof(MyCalculatorService.SimpleCalculator), httpUrl, tcpUrl);

Multiple end points can be added to the Service using AddServiceEndpoint() method. Host.Open() will run the service, so that it can be used by any client.

Step 7: Below code show the implementation of the host process.

using System;
using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel;

using System.ServiceModel.Description;

namespace MyCalculatorServiceHost
{

    class Program

    {

        static void Main(string[] args)

        {

            //Create a URI to serve as the base address

            Uri httpUrl = new Uri("http://localhost:8090/MyService/SimpleCalculator");

            //Create ServiceHost
            ServiceHost host = new ServiceHost(typeof(MyCalculatorService.SimpleCalculator), httpUrl);

            //Add a service endpoint
            host.AddServiceEndpoint(typeof(MyCalculatorService.ISimpleCalculator) , new WSHttpBinding(), "");

            //Enable metadata exchange
            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();

            smb.HttpGetEnabled = true;

            host.Description.Behaviors.Add(smb);

            //Start the Service
            host.Open();

            Console.WriteLine("Service is host at " + DateTime.Now.ToString());

            Console.WriteLine("Host is running... Press <
.Enter.> key to stop");
            Console.ReadLine();

        }

    }

}

Step 8: Service is hosted; now we need to implement the proxy class for the client. There are different ways of creating the proxy:

1.      Using SvcUtil.exe, we can create the proxy class and configuration file with end points.

2.      Adding Service reference to the client application.

3.      Implementing ClientBase<.T.> class

Of these three methods, Implementing ClientBase<.T.> is the best practice. If you are using rest two method, we need to create proxy class every time when we make changes in Service implementation. But this is not the case for ClientBase<.T.>. It will create the proxy only at runtime and so it will take care of everything.

MyCalculatorServiceProxy.cs
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel;

using MyCalculatorService;

namespace MyCalculatorServiceProxy
{

    public class MyCalculatorServiceProxy :

        //WCF create proxy for ISimpleCalculator using ClientBase

        ClientBase<
.ISimpleCalculator.>,
        ISimpleCalculator

    {

        public int Add(int num1, int num2)

        {

            //Call base to do funtion

            return base.Channel.Add(num1, num2);

        }

    }

}

Step 9: In the client side, we can create the instance for the proxy class and call the method as shown below. Add proxy assembly as reference to the project.

using System;
using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel;

namespace MyCalculatorServiceClient
{

    class Program

    {

        static void Main(string[] args)

        {

            MyCalculatorServiceProxy.MyCalculatorServiceProxy proxy ;

            proxy= new MyCalculatorServiceProxy.MyCalculatorServiceProxy();

            Console.WriteLine("Client is running at " + DateTime.Now.ToString());

            Console.WriteLine("Sum of two numbers... 5+5 ="+proxy.Add(5,5));

            Console.ReadLine();

        }

    }

}

Step 10: End point (same as service) information should be added to the configuration file of the client application.

<.?xml version="1.0" encoding="utf-8" ?.>
<
.configuration.>
  <
.system.serviceModel.>
    <
.client.>
      <
.endpoint address ="http://localhost:8090/MyService/SimpleCalculator" binding ="wsHttpBinding" contract ="MyCalculatorService.ISimpleCalculator".>
      <
./endpoint.>
    <
./client.>
  <
./system.serviceModel.>
<
./configuration.>

Step 11: Before running the client application, you need to run the service. Output of the client application is shown below.

Image may be NSFW.
Clik here to view.

This self host shows advantage such as in-Pro hosting, programmatic access and it can be used when there need singleton service. I hope you have enjoyed the Self hosting session, now let go for hosting using Windows Activation service.

Windows Activation Service
Windows Activation service is a system service available with Windows vista and windows server 2008. It is available with IIS 7.0 and it is more powerful compared to IIS 6.0 because it supports Http, TCP and named pipes were IIS 6.0 supports only Http. It can be installed and configured separately.

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

1.      Enable WCF for non-http protocols

2.      Create WAS hosted service

3.      Enable different binding to the hosted service

Enable WCF for non-http protocols
Before Start creating the service we need to configure the system to support WAS. Following are the step to configure WAS.

1.      Click Start -> Control Panel -> programs and Features and click 'Turn Windows Components On or Off' in left pane.

2.      Expand 'Microsoft .Net Framework 3.0' and enable "Windows Communication Foundation HTTP Activation" and "Windows Communication Foundation Non- HTTP Activation".

3.      Next we need to add Binding to the Default Web site. As an example, we will bind the default web site to the TCP protocol. Go to the Start menu -> Programs ->Accessories. Right click on the "Command Prompt" item, and select "Run as administrator" from the context menu.

4.      Execute the following command

5.      C:\Windows\system32\inetsrv> appcmd.exe set site "Default Web Site" -+bindings.[protocol='net.tcp', bindingInformation='808:*']

That command adds the net.tcp site binding to the default web site by modifying the applicationHost.config file located in the "C:\Windows\system32\inetsrv\config" directory. Similarly we can add different protocols to the Default Web site.



Viewing all articles
Browse latest Browse all 10

Trending Articles