Horizontal Menu Bar

Follow C# Tutorials

Thursday, 9 February 2012

C#.Net How To: Consume a WCF service hosted as Window Services

A WCF service can be hosted in IIS, Managed Windows Services, Windows Activation Service (WAS) and Self-hosting. I have already explained how to Host WCF service in IIS  and how to host a WCF service in managed Windows Service in my previous post. Let’s learn here how to consume a WCF service hosted in Windows Service in Visual Studio 2010 using console application.

To consume the WCF service I am considering an example given in my previous post i.e how to host a WCF service in managed Windows Service. The service will accept a name at console and will return a welcome message like “Welcome to http://www.a1ashiish-csharp.blogspot.in, Mr. Ashish Ramteke”.

Let’s start - 

1) Your WCF service must be ready and running windows service. You can check the status of your WCF service using either Service Control Manager or Task Manager. Open Task Manager and goto Services tab to check the status of your service whether it is running or stopped. If it is stopped then right click on the service and then select Start Service. 
Task manager windows service
WCF Service - Task Manager showing the status of WCF Windows Service.
 2) Once your service is started, you can access it by the base address (URL) provided in the configuration file i.e app.config of your WCF service. In this post, I am assuming the base address (URL) of my previous post how to host a WCF service in managed Windows Service i.e “http://localhost:8000/WCFWindowsServiceHosting/Welcome.

3)   Now create a console application that will consume the WCF service.
Let’s name this console consumer application as WCFWindowsServiceConsumeApp.
Create console application
WCF Service - Creating a consumer console application to consume the WCF Windows Service.

4)   Add your WCF service reference to this consumer application. To add the WCF service reference, right click on project WCFWindowsServiceConsumeApp in Solution Explorer, and select Add Service Reference.
Adding wcf service reference
WCF Service - WCF Service - Adding Service Reference to consumer application.

5)   Enter WCF service URL in Address: field and click on Go button. If the service is running then service(s) found at address message appears and your service appears in Services: panel.     
Default service reference screen
WCF Service -Service Reference Window.

Name the service reference as WCFWindowsServiceReference and click on OK button.

6)   The service reference appears in solution explorer.
wcf windows service in explorer
WCF Service - WCF windows service Reference appearing in Solution Explorer.

7)   Add following lines of code to program.cs file –

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WCFWindoxServiceConsumeApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var welcomeClient = new WCFWindowsServiceReference.WelcomeClient();
            Console.Write("Enter your name : ");
            Console.WriteLine(welcomeClient.WelcomeMessage(Console.ReadLine()));
            Console.ReadLine();
        }
    }
}

8)   Your WCF service consumer console application is ready. Execute the application to see the result.
wcf windows service output
WCF Service - WCF Windows service consumer application output.
I hope you enjoyed the article. Could you please share the article on your social media? Thank you!

Following few articles might be of your interest -

1) How to self host the WCF service?
2) How to consume WCF service in ASP.net?
3) How to host WCF service in IIS?
4) How to enable WCF tracking and MessageLogging?
 

Sharing is Caring »»

Follow C# Tutorials