Self-Hosting of WCF Service With Console Application

WCF Services

A WCF service keeps data in either XML or JSON format that can be processed by any language. In other words if you create a Service application then it can communicate with any application.

Now I will explain how to create a simple WCF Service and how to host it in a console application.

Step 1: Open Visual Studio 2010.

Step 2: Now click on "New Project" -> "Windows" -> "Class Library" and add the name of Class library; I used "Hello".

Class Library

Step 3: Now delete Class1.cs

Step 4: Now go to the Solution Explorer click on "Add" > "New Item".

New Item

Step 5: Add the WCF Service and add the name of the service.

name of service

Step 6: And now declare the method in the interface as in the following:

  1. [OperationContract]  
  2. string getmessage(string name); 

And implement it in the .cs file as in the following:

  1. public string getmessage(string name)  
  2. {  
  3.     return "Hello" + name;  

And build the project.

Step 7: Now delete the app.config file by first right-clicking on the Solution Explorer as in the following:

Solution Explorer
 
Step 8: Now we want to host this WCF Service in a console application. For that we need to add the console application from "Add" > "New Project" and select the Console Application and add the name of application just like I gave HelloService.

New Project

Step 9: Now first we need to add the reference for the ServiceModel. To add the services in the console application click on the console application and go to "References" > "Add Reference" and then add the reference of serviceModel.

serviceModel
 
And add the reference of service also.

add the reference of service

And now add the App.config in the console application. For adding it first right-click on the console application and go to "Add" > "New Item" > "Application Configuration File".

Step 10: Now we need to configure the service in the app.config file so for that go to "Tools" > "WCF Service Configuration Editor". Then go to "File" > "Open" > "Config File" and open the config file of this project where it is present and then click on Create a New Service.

Create a New Service

And then open the DLL of the service and then open the service reference.
 
open the service reference

And click on the next button 2 times and then for communication mode we choose TCP binding and then again click "Next >".
 
TCP binding

And now it shows an address that is the address of the end point that can be a relative address and it specifies the base address. You can give the base address that you want, just as I used hello.
 
base address

And then click on "Next" then it will show all the details about the service and then click on Finish.

Step 11: Now we have added a Relative address to add the Base Address of the service so to add the base address first go to host and then we need 2 base addresses, one is for the service end point and the second one is for the metadata to be available.
 


And now after click on new provide the base address of the service just like I used http://localhost:8080.

base address of service

And now again add the address of the metadata available. Click on new again and provide the address, I used net.tcp://localhost:8090.
 
base address of service

Step 12: Now to enable the metadata information we need to set the service behaviour. First expand Advanced and then Service Behaviour and here click on New Service Behaviour Configuration.

Service Behaviour Configuration

And then click on Add and the service Metadata.

service Metadata

And then here go to the service Metadata and set the property HttpGetEnabled to true.

HttpGetEnabled

Now we need to associate the behaviour with service so for that:

behaviour with service

And now close this and it will ask to save the changes, click Yes and then you will see the app.config file as:
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <configuration>  
  3. <system.serviceModel>  
  4. <behaviors>  
  5. <serviceBehaviors>  
  6. <behavior name="NewBehavior0">  
  7. <serviceMetadata httpGetEnabled="true" />  
  8. </behavior>  
  9. </serviceBehaviors>  
  10. </behaviors>  
  11. <services>  
  12. <service behaviorConfiguration="NewBehavior0" name="Hello.Service1">  
  13. <endpoint address="hello" binding="netTcpBinding" bindingConfiguration=""  
  14. contract="Hello.IService1" />  
  15. <host>  
  16. <baseAddresses>  
  17. <add baseAddress="http://localhost:8080" />  
  18. <add baseAddress="net.tcp://localhost:8090" />  
  19. </baseAddresses>  
  20. </host>  
  21. </service>  
  22. </services>  
  23. </system.serviceModel>  
  24. </configuration> 

Step 13: Now add the code inside in the console application for the host.

  1. static void Main(string[] args)  
  2. {  
  3.     //herre the name of the service which we need to host  
  4.     using (ServiceHost host = new ServiceHost(typeof(Hello.Service1)))  
  5.     {  
  6.         //read the WCF service  
  7.         host.Open();  
  8.         Console.WriteLine(DateTime.Now.ToString());  
  9.         host.Close();  
  10.         Console.ReadLine();  
  11.    }  

Output

Output
The advantages of self-hosting in WCF are:
  1. Very easy to setup with app.config file.
  2. For debugging we don't need to attach the separate process, it is easy to debug.
  3. It supports all bindings.
  4. Very flexible to control the lifetime of the service using the open and close methods of the service.

The disadvantages of self-hosting in WCF are:

  1. The service is available only for the client.
  2. We need to start the WCF service manually and it doesn't support automatic message-based activation.
  3. It needs custom code.

Up Next
    Ebook Download
    View all
    Learn
    View all