Video tutorial on Hosting WCF Service with netTcpBinding in Windows Service

WCF Service

IService1.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;

namespace NetTcpServicetoHostinWindowsServices

{

[ServiceContract]

public interface IService1

{

[OperationContract]

string GetData(int value);

}

}

Service1.svc.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;

namespace NetTcpServicetoHostinWindowsServices

{

public class Service1 : IService1

{

public string GetData(int value)

{

return string.Format("You entered: {0}", value);

}

}

}

App.Config

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<system.web>

<compilation debug="true" />

</system.web>

<system.serviceModel>

<services>

<service name="NetTcpServicetoHostinWindowsServices.Service1" behaviorConfiguration ="MyBehavior">

<host>

<baseAddresses>

<add baseAddress = "net.tcp://localhost:9999/Service1/" />

</baseAddresses>

</host>

<endpoint name ="NetTcpEndPoint"

address =""

binding="netTcpBinding"

contract="NetTcpServicetoHostinWindowsServices.IService1">

</endpoint>

<endpoint name ="NetTcpMetadataPoint"

address="mex"

binding="mexTcpBinding"

contract="IMetadataExchange"/>

</service>

</services>

<behaviors>

<serviceBehaviors>

<behavior name ="MyBehavior">

<serviceMetadata httpGetEnabled="False"/>

<serviceDebug includeExceptionDetailInFaults="False" />

</behavior>

</serviceBehaviors>

</behaviors>

</system.serviceModel>

</configuration>

Windows Service

Service1.cs

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Diagnostics;

using System.Linq;

using System.ServiceProcess;

using System.Text;

using System.ServiceModel;

using NetTcpServicetoHostinWindowsServices;

namespace HostingWindowsService

{

public partial class Service1 : ServiceBase

{

internal static ServiceHost myHost = null;

BackgroundWorker worker;

public Service1()

{

InitializeComponent();

}

protected override void OnStart(string[] args)

{

worker = new BackgroundWorker();

worker.DoWork += new DoWorkEventHandler(worker_DoWork);

}

void worker_DoWork(object sender, DoWorkEventArgs e)

{

if (myHost != null)

{

myHost.Close();

}

myHost = new ServiceHost(typeof(NetTcpServicetoHostinWindowsServices.Service1));

myHost.Open();

}

protected override void OnStop()

{

if (myHost != null)

{

myHost.Close();

myHost = null;

}

}

}

}

Client Application

Program.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using ConsoleClient.ServiceReference1;

namespace ConsoleClient

{

class Program

{

static void Main(string[] args)

{

Service1Client proxy = new Service1Client();

Console.WriteLine(proxy.GetData(9));

Console.Read();

}

}

}

12 responses to “Video tutorial on Hosting WCF Service with netTcpBinding in Windows Service”

  1. Great Tutorial Dhananjay… Hope to see more with regards to WCF REST in .NET 4.0.. I think this is the only blog which has this type of tutorial.

    Thanks!!

  2. Good tutorial but do me a fav 🙂 please buy a new keyboard or move the mic further away from it.. i mean wow, you beat the crap out of that keyboard by the sounds of it 🙂 hehe

  3. it not installing when i try to install it(the Transaction failed rollback has performed)

  4. it showing error when i try to start service…

  5. Awesome..Explained each step in detail.Though I am getting sone faults while consuming my service.

    Thanks

  6. Good Video..
    If your getting error at the time of stating service, then right click on service > Properties>Log on tab> select Local System Account. Press Ok. Try again.
    Hope this should solve the issue.

  7. Nice video, but you actually connected the WcfHost instead of the service you installed. The WcfHost was launched by Visual Studio when you added the reference. I’m unable to connect to the actual service once installed. Any ideas?

  8. Also, you forgot to add worker.RunWorkerAsync() in your OnStart(). Forgetting this allows the service to run, but the background thread is never entered and the service is never started.

    Great video and got me up and running. Thanks!

  9. Good video, but please brush up your presentation skills.

  10. Is this method of hosting also called as WAS hosting????
    Please let me knw….

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com