Monday, November 26, 2012

WCF : A simple self-hosting WCF service

There are three ways of hosting WCF service. Hosting in IIS, WAS, and self-hosting. The previous post showed a simple WCF service that is hosted in IIS. Another popular hosting mechanism is self-hosting.
We can self-host a WCF service in any application, for example, in console application, windows forms, or windows service. And the coding is relatively simple.

(1) Run Visual Studio
(2) New Project - [Console Application]
(3) Add New Item - [WCF Service] item. Name it SimpleService.svc.

(4) Write a WCF interface in ISimpleService.cs.

namespace SimpleWcfService
{    
    using System.Runtime.Serialization;
    using System.ServiceModel;

    [ServiceContract]
    public interface ISimpleService
    {
        [OperationContract]
        string SayHello(string name);
    }
}

(5) Write a WCF service class that implements the interface.
// SimpleService.svc.cs
namespace SimpleWcfService
{
    public class SimpleService : ISimpleService
    {
        public string SayHello(string name)
        {
            string v = "Hello " + name;
            return v;
        }
    }
}

(6) Write a self-hosting code in Program.cs
Basically we create System.ServiceModel.ServiceHost instance and call Open() method; and call Close() method once we are done.
using System;
using System.ServiceModel;

namespace SimpleWcfHost
{
    class Program
    {
        static void Main(string[] args)
        {
            ServiceHost host = new ServiceHost(typeof(SimpleService));            
            host.Open();

            Console.WriteLine("WCF Service Started. Press ENTER to stop.");
            Console.ReadLine();

            host.Close();
        }
    }
}

(7) Edit App.config file. As a base address, we specified http://localhost:8899/ as below.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="">
                    <serviceMetadata httpGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="false" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <services>
            <service name="SimpleWcfHost.SimpleService">
                <endpoint address="" binding="wsHttpBinding" contract="SimpleWcfHost.ISimpleService">
                    <identity>
                        <dns value="localhost" />
                    </identity>
                </endpoint>
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:8899/" />
                    </baseAddresses>
                </host>
            </service>
        </services>
    </system.serviceModel>
</configuration>

(8) To check to see if the WCF service is running, open web browser and type http://localhost:8899/




(9) To test WCF service from WCF client, first run self-hosting EXE application. And then you can run WcfTestClient.EXE client application. Specify service URL (http://localhost:8899/) and call a method by clicking [Invoke].


No comments:

Post a Comment