jump to navigation

Self-hosting WCF Services March 4, 2012

Posted by fofo in C#, Visual Studio 2008, Visual Studio 2010, VS 2010, WCF, WCF Service.

In this post I will show you how to host a WCF Service in your own applications.This is referred as self hosting, meaning the hosting of a WCF Service in a Console application or a Windows Forms application.  This is the third post in a series of posts on the various options we have when it comes to hosting WCF Services.If you want to know how to host the WCF Service in IIS have a look in this post.If you want to find out how to host a WCF Service in a windows service click here.

I will use a simple a WCF service that just completes some basic math tasks.The main focus in this post is not to implement a very complicated WCF Service. The main focus is demonstrating how to host the WCF Service in a console application.

Let’s build the WCF Service first.

1) Launch Visual Studio as an Administrator. From the available templates use “WCF Service Library”. Choose a suitable name for that project.I have named mine WCFCalcService. I will use C# as the development language.

2) Rename the IService.cs and Service.cs to ICalculator.cs and Calculator.cs

3) I will not be deleting anything from the existing code in those files. I will add some code in the ICalculator.cs

public interface ICalculator

double Add(double a1, double a2);

double Multiply(double a1, double a2);

string GetData(int value);

CompositeType GetDataUsingDataContract(CompositeType composite);

// TODO: Add your service operations here

// Use a data contract as illustrated in the sample below to add composite types to service operations
public class CompositeType
bool boolValue = true;
string stringValue = "Hello ";

public bool BoolValue
get { return boolValue; }
set { boolValue = value; }

public string StringValue
get { return stringValue; }
set { stringValue = value; }

4) Now we need to implement the methods we wrote in the ICalculator.cs file.

public class Calculator : ICalculator

        public double Add(double n1, double n2)
            return n1 + n2;

        public double Multiply(double n1, double n2)
            return n1 * n2;

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

        public CompositeType GetDataUsingDataContract(CompositeType composite)
            if (composite == null)
                throw new ArgumentNullException("composite");
            if (composite.BoolValue)
                composite.StringValue += "Suffix";
            return composite;

5) Build and run your application.Visual Studio will launch the WCF Test Client  so we can test the service.The service will be hosted in the WCF Service Host application that VS launches automatically.Test that the service works as expected.

6) I am going to add a new project in my solution, a Console application and in there I will be hosting my service.I have named this new project CalcServiceHost.I am going to add a few references to this project.The first one will be to the System.ServiceModel and the second one will be to the WCF Service WCFCalcService. Then I am going to create a new instance of a class called ServiceHost inside the Program.cs file and specify the type of service (Calculator) that I want to host. Then I will open the host and by opening the host I will be able to read the configurations about our service from the config file and build the runtime to support all the endpoints that has found.

The whole code follows

static void Main(string[] args)

ServiceHost host = new ServiceHost(typeof(Calculator));


Console.WriteLine("Service is up and running");




7) Now we need to add a configuration file (app.config) to my Console project so I can configure my endpoints.The whole code for the app.config file follows

<?xml version="1.0" encoding="utf-8" ?>
<behavior name="NewBehavior0">
<serviceMetadata httpGetEnabled="true" />
<service behaviorConfiguration="NewBehavior0" name="WCFCalcService.Calculator">
<endpoint address="main" binding="basicHttpBinding" bindingConfiguration=""
contract="WCFCalcService.ICalculator" />
<add baseAddress="http://localhost:8080/calcservice" />

You can type the code in the app.config file. There is full Intellisense support. You can also use the WCF configuration tool to achieve the same thing.I am not doing anything difficult. I set a base address for the service and configure an endpoint.I also enable the service’s metadata.

Hit CTRL+F5 and you will see the hosting console application with the message “Service is up and running”.If you launch your favorite browser and type

http://localhost:8080/calcservice&#8221; in the URL address, you will be able to browse to the WSDL definition of the service.

8) Now we can test the WCF service from the test client.Open a Visual Studio command prompt (Administrator) window and type

wcftestclient “http://localhost:8080/calcservice&#8221; and hit Enter. It is going to talk to our service and find its metadata.It is going to launch the familiar test client so we test the service.Test the WCF Service to see if it works as expected.Have a look at the picture below.

9) We see that the WCF service works as expected. If you close the Console application that hosts the WCF service, nothing will work.

Leave a comment if you want the source code.

Hope its helps!!!!



1. Dot Net Rules : Self-hosting WCF Services - March 4, 2012

[…] post in a series of posts on the various options we have when it comes to hosting WCF Services. (read more) Share Posted: Κυριακή, 4 Μαρτίου 2012 6:19 μμ από το μέλος […]

2. How to configure WAS for TCP endpoints in IIS « DOT NET RULES - March 5, 2012

[…] and expose TCP endpoints. Recently I have posted 3 posts on WCF hosting options. You can find them here , here and […]

3. The Best Web Hosting Sites - July 5, 2013

My brother suggested I might like this web site.
He was totally right. This post actually made my day.
You can not imagine just how much time I had spent for this info!

4. Varun - November 10, 2013

please give me source code

5. Http://Bluehostdiscount11.Webs.Com - January 3, 2014

Fantastic blog! Do you have any tips and hints for aspiring writers?

I’m hoping to start my own blog soon but I’m a little lost on everything.
Would you propose starting with a free platform like WordPress
or go for a paid option? There are so many choices out there that I’m totally confused ..
Any tips? Thank you!

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 )

Google+ photo

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

Twitter picture

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

Facebook photo

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


Connecting to %s

%d bloggers like this: