Hosting WCF service example

Jan 16, 2013 00:00 · 406 words · 2 minute read Programming WCF

WCF service can be hosted in two ways.

1. Self-hosting

You can use the ServiceHost Class to host any .net 3.x application inside your project. In this case you can make a console application and add the reference of the WCF service and also System.ServiceModel to your project.

var serviceHost = new ServiceHost(typeof(Machine));

In the next step you can add endpoints to your service host instance

serviceHost.AddServiceEndpoint(typeof(IMachine), new BasicHttpBinding(),

serviceHost.AddServiceEndpoint(typeof(IMachine), new WSHttpBinding(), 

serviceHost.AddServiceEndpoint(typeof(IMachine), new NetTcpBinding(), 

You pass your service contract interface type along with your desire protocols. Each end point should have a unique URI address.

Each binding is suited for a specific type of communication. For instance, WebHttpBinding support REST-style communication while BasicHttpBinding is for SOAP.

Binding Configuration

Also it is possible to tailor a bindings to meet our needs.

var basicHttpBinding=new BasicHttpBinding();

serviceHost.AddServiceEndpoint(typeof(IMachine), basicHttpBinding,

Behavior configuration

It is also possible to add behavior to the service . For instance in order to set the meta exchange behavior we can come up with something like this.

var serviceMetadataBehavior=new ServiceMetadataBehavior {
	HttpGetEnabled = true,
	HttpGetUrl = new Uri("http://localhost:8733/ServiceMachine/meta")

Then you can open the service or abort it if you encounter to any exceptions.

try {
catch (Exception exception) {

2. Managed hosting

It is also possible to host your WCF service in IIS/ application without concern about ServiceHost class and let take care about our service. It is possible to use SvcConfigEditor.exe in order to maintain your config file.

	<service name="WCFService.Machine">
		<clear />
			<add baseAddress="http://localhost:8733/ServiceMachine/" />
		<endpoint address="basic" binding="basicHttpBinding" contract="WCFService.IMachine" />
			<endpoint address="ws" binding="wsHttpBinding" contract="WCFService.IMachine" />
		<endpoint address="net.tcp://localhost:8732/ServiceMachine/" binding="netTcpBinding contract="WCFService.IMachine" />

The logic behind it, is the same but the syntax is different. The base address is your main address and the address attribute in the endpoints represent the relative address.

Binding Configuration

Tailoring the binding we can use the binding configuration after the close tag as below.

		<binding name="customBindings">
			<security mode="Transport">
				<transport clientCredentialType="Basic"/>

Then map it to your desire basicHttpBindings

<endpoint address="basic" binding="basicHttpBinding" contract="WCFService.IMachine" bindingConfiguration="customBindings" />

Configure behavior

Similarly to the binding you should define the serviceBehavior tag .

	<behavior name="meta">
		<serviceMetadata httpGetEnabled="true" />
		<serviceDebug includeExceptionDetailInFaults="false" />

And then set it in behaviorConfiguration attribute.

<service behaviorConfiguration="meta" name="WCFService.Machine">


Feel free to download the source code of this wcf service example from my GitHub.