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;

    public interface ISimpleService
        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));            

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


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

(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].

1 comment:

  1. This is a good article & good site.Thank you for sharing this article. It is help us following categorize:
    healthcare, e commerce, programming, multi platform,inventory management, cloud-based solutions, it consulting, retail, manufacturing, CRM, technology means, digital supply chain management, Delivering high-quality service for your business applications,
    Solutions for all Industries,
    Getting your applications talking is the key to better business processes,
    Rapid web services solutions for real business problems,
    Web-based Corporate Document Management System,
    Outsourcing Solution,
    Financial and Operations Business Intelligence Solution,

    Our address:
    2002 Timberloch Place, Suite 200
    The Woodlands, TX 77380