Self Hosting WCF service(Part-17)

Hosting a WCF service in any dot net managed application like console application,WPF,Winforms etc is called self Hosting.

Let’s see an example. Create a new Visual studio project of type Class Library and name it as WCFSelfHostService.Delete the class1.cs and add WCF service to it and rename it to WCFSelfHostService.cs.

We will have a simple method and let’s add it in operation contract of IWCFSelfHostService.cs.

and in WCFSelfHostService.cs we will implement the interface.

Next we can delete the app.config file from solution and build the solution.
Add a console application to the project and add the service reference and System.ServiceModel to Host.
Then add an application configuration file app.config to your host.Right click app.config–>Edit WCF configuration and click on file and open app config.
Then click on create a new service and browse folder:
\visual studio 2013\Projects\WCFSelfHostService\WCFSelfHostService\bin\Debug\WCFSelfHostService.IWCFSelfHostService
Click on next and then it would show the contract and click next and then select the binding you want to use. In this example, we will use TCP binding.then Next and you will ask address and put WCFSelfHostService and click on finish.

In Address, we have given a relative address, so we need to create the base address.
We need to add 2 base address, one is for service end point and another for meta data to available.

Then the next important thing is to enable service behavior.for that click on Advanced–>service Behavior–>create a new service behavior–>Add–>serviceMetadata. Then click on serviceMetadata and Enable HTTPGetEnabled to true and change the name to mexBehavior.
Next step would be to associate the service with this behavior.
So lets go back to service and select the behaviorConfiguration as mexBehavior and close and save changes.Then look at app.config file.It would be looking like as given below.

Next step would be opening up the communication channel for the client to communicate.for that we need to open host in Program.cs file.

Then Set the Host as setup project and Hit CTRL+f5.The service host will be started.
To test the service, lets open another VS–>new–>Projet–>Windows Form Application–>Rename it to WCFSelfHostClient and Drag and drop 2 labels,one text box and one button and rename the button to Get Message.Remove the text for label2 and rename label 1 as Message that the user will give input.Add service reference to WCFSelfHostService.

Then double click the button and write a proxy class for WCFSelfHostService in Button click event.

Then run the client by hitting CTRL+F5. You will see the form. Give your input and you will see Hello + “Message that you have given”
For me it is Hello Techkatak.

Advantages of Self Hosting:

  • Easy to setup
  • Easy to debug since there is no separate process
  • Supports all binding and transport protocols

Disadvantages of SelfHosting:

  • Then service is available only when Host is running
  • Custom code is required to do self hosting
  • Does not support automatic messaged based activation like IIS
  • So this is good for Developemnt but not for live WCF service

© 2015, www.techkatak.com. All rights reserved.

One thought on “Self Hosting WCF service(Part-17)

Comments are closed.