Windows Communication Foundation - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

WCF Self Hosting

WCF Self Hosting

shape Description

WCF Service can be created in any Managed Application or Self-Hosting. This option enables to common scenarios which are running inside Console Applications. Hosting a service in managed applications is typically useful during the application development phase.

shape Step-1

How to create a Self-Hosting of WCF Service is explained here with the simple console application.First simple WCF Service is needed for self-hosting. So first implement a simple WCF Service by the class library (.dll) with the name as HelloService like below screen.

shape Step-2

After creating a project,one default class called as class.cs1 is generated. Remove that class and add a WCF Service application like below screen. Right click on the Project -> Add button -> New item option in the popup then below screen appears. Select the WCF application.

shape Step-3

WCF Application generates by default two files, those are HelloService.cs and IHelloService.cs. ISelfHost file contains service contract and HelloService is used to implement the methods which we had written in ISelfHost.cs file. Write a code in ISelfHost like below: [csharp]using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace WCFSelfHost { // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "ISelfHost" in both code and config file together. [ServiceContract] public interface ISelfHost { [OperationContract] string HelloSelfHost(string name); } }[/csharp] Write a code in WCFSelfHost.cs like below: [csharp] using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace WCFSelfHost { // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "SelfHost" in both code and config file together. public class SelfHost : ISelfHost { public string HelloSelfHost(string name) { return "This is Self Host service : " + name; } } } [/csharp] Host WCF service by using console application. Here WCF Service is self-hosted. For that, add console application to this solution. Right click on Solution Explorer and select the New Project like below screen.

shape Step-4

Select console application and change the name as "Host"  like below screen.

shape Step-5

For this project add some references like System.Service model assembly that will give all the features of WCF and give one more reference to use WCF Service (HelloService) project itself. Below screen shows how to add the reference. All the assembly references can be seen. In that, select System.ServiceModel assembly only. Below screen shows how to add reference.

shape Step-6

After adding all the references, look for App.config file in  the "Host" project. Here, set all the endpoint addresses, metadata etc like below code. Even though there is a config file in 'HelloService" project, but this project is just a class library so no use of that file and can delete it. [csharp]<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="maxBehavior"> <serviceMetadata httpGetEnabled="true" /> </behavior> </serviceBehaviors> </behaviors> <services> <service behaviorConfiguration="maxBehavior" name="WCFSelfHost.SelfHost"> <endpoint address="WCFSelfHost" binding="netTcpBinding" bindingConfiguration="" contract="WCFSelfHost.ISelfHost" /> <host> <baseAddresses> <add baseAddress="http://localhost:8080" /> <add baseAddress="net.tcp://localhost:8090" /> </baseAddresses> </host> </service> </services> </system.serviceModel> </configuration>[/csharp] After setting the all the addresses, behaviors, and proxies, then write a code to open up the communication channel. Write a code in the program.cs file which is host project like below. [csharp] using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using WCFSelfHost; namespace Hosting { class Program { static void Main(string[] args) { using (ServiceHost host = new ServiceHost(typeof(WCFSelfHost.SelfHost))) { host.Open(); Console.WriteLine("Host Started @ " + DateTime.Now.ToString()); Console.ReadLine(); } } } } [/csharp]

shape Step-7

After completing all the above process, just build the "WCFSelfHost" project and set "Hosting" project as a startup project because WCFSelftHost project is a class library (.dllfile). Then one can run the solution and get the below screen. The service is hosted successfully. When pressed on any key after getting the above screen, service will give service created date and time.