Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Part 24 - Self hosting a wcf service in console application

Suggested Videos
Part 21 - Bindings in WCF
Part 22 - Configure WCF service endpoint dynamically in code
Part 23 - Hosting WCF service



In this video, we will discuss hosting a wcf service using a console application. Hosting a wcf service in any managed .net application is called as self hosting. Console applications, WPF applications, WinForms applications are all examples of managed .net applications.

Here are the steps to create and slef host a wcf service in a console application



Step 1: Create a new class library project with name = HelloService

Step 2: Delete class1.cs file that is auto-generated and a new WCF service with name = HelloService.

Step 3: Copy and paste the following code in IHelloService.cs file
using System.ServiceModel;
namespace HelloService
{
    [ServiceContract]
    public interface IHelloService
    {
        [OperationContract]
        string GetMessage(string name);
    }
}

Step 4: Copy and paste the following code in HelloService.cs file
namespace HelloService
{
    public class HelloService : IHelloService
    {
        public string GetMessage(string name)
        {
            return "Hello " + name;
        }
    }
}

Step 5: To host the WCF service, right click on HelloService solution in Solution explorer and add new Console Application with name = Host

Step 6: Right click on the "References" folder under "Host" project and add a reference to System.ServiceModel assembly and HelloService project.

Step 7: Copy and paste the following code in Program.cs file
using System;
using System.ServiceModel;

namespace Host
{
    class Program
    {
        public static void Main()
        {
            using (ServiceHost host = new ServiceHost(typeof(HelloService.HelloService)))
            {
                host.Open();
                Console.WriteLine("Host started @ " + DateTime.Now.ToString());
                Console.ReadLine();
            }
        }
    }
}

Step 8: Right click on "Host" project and add Application Configuration file. Copy and paste the following code in App.config file.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="mexBehavior">
                    <serviceMetadata httpGetEnabled="true" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <services>
            <service behaviorConfiguration="mexBehavior"
                     name="HelloService.HelloService">
                <endpoint address="HelloService" binding="netTcpBinding"
                    contract="HelloService.IHelloService" />
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:8080"/>
                        <add baseAddress="net.tcp://localhost:8090"/>
                    </baseAddresses>
                </host>
            </service>
        </services>
    </system.serviceModel>
</configuration>

Step 9: Right click on "Host" project in solution explorer and set it as startup project.

Step 10: Run the host by pressing CTRL + F5.

Step 11: Create a new windows forms application with name = WindowsClient

Step 12: Design the wia hreform as shown in the image below


Step 13: Right click on "References" folder in WindowsClient project and select add service reference option. Type "http://localhost:8080/" in the address textbox and click "Go" button. Type "HelloService" in the "Namespace" textbox and click "OK" button.

Step 14: Double click on the button control on the windows form to generate button Click event handler. Copy and paste the following code in Form1.cs file.
using System;
using System.Windows.Forms;

namespace WindowsClient
{
    public partial class Form1 : Form
    {
        HelloService.HelloServiceClient client;
        public Form1()
        {
            InitializeComponent();
            client = new HelloService.HelloServiceClient();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label2.Text = client.GetMessage(textBox1.Text);
        }
    }
}

Advantages of self hosting a wcf service in a console application
1. Very easy to setup. Specify the configuration in app.config file and with a few lines of code we have the service up and running.
2. Easy to debug as we don't have to attach a separate process that hosts the wcf service.
3. Supports all bindings and transport protocols.
4. Very flexible to control the lifetime of the services through the Open() and Close() methods of ServiceHost.

Disadvantages of self hosting a wcf service in a console application
1. The service is available for the clients only when the service host is running.
2. Self hosting does not support automatic message based activation that we get when hosted within IIS.
3. Custom code required.

In general, self-hosting is only suitable during the development and demonstration phase and not for hosting live wcf services.

wcf tutorial

2 comments:

  1. Its very nice to see these precious videos.
    Venkat, I have doubt regarding WCF service authorization.

    it would be appreciable if u can post some related doc/vid.

    ReplyDelete
  2. Can you give more data as to why you say this?

    "In general, self-hosting is only suitable during the development and demonstration phase and not for hosting live wcf services."

    ReplyDelete

If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.