Feb
9
2012

C#.Net How To: Consume WCF service hosted as Windows service

In this WCF tutorial Let’s learn to consume WCF service hosted as Windows Service in Visual Studio 2010 using console application. In last article we learned what is windows service and how to host wcf service in windows service?

A WCF service can be hosted in IIS, Windows Services, Windows Activation Service (WAS) and It also supports Self-hosting. In one of my previous article I have explained how WCF service can be hosted in IIS and in windows services.

To consume the WCF service I am considering an example from previous post i.e how to host a WCF service in managed Windows Service. The service will accept a string (name) at console and will return a welcome message. For example if entered string is “Mr. Ashish Ramteke” then the output will be “Welcome to http://www.a1ashiish-csharp.blogspot.in, Mr. Ashish Ramteke.

Steps to consume WCF service hosted as Windows Service

  1. To consume WCF service hosted as Windows Service, 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. For example 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 start it. To start the windows service right click on the service name and then select Start Service.
    WCF hosted in Windows Service
    WCF hosted in 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) from 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.
    WCF Service - Create Console Application
    WCF Service – Create Console Application
  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.
    WCF Service - Adding WCF reference
    WCF Service – Adding WCF reference
  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.
    WCF Service - WCF Web methods
    WCF Service – WCF Web methods

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

  6. You can verify the service reference in solution explorer.
    WCF Service - WCF Reference
    WCF Service – WCF Reference
  7. Add following lines of code to program.cs file to consume wcf service hosted as windows service
    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. Save all the files. Your WCF service consumer console application is ready. You can now execute the application to see the result as below -
    Consume WCF service hosted as Windows Service
    WCF hosted as Windows Service consumed in Console.

In this wcf tutorial we learned to consume WCF Service hosted as windows service.

I hope you enjoyed the article. If you find this article helpful, then could you please share the article on your social media?

Following few articles might be of your interest -

About the Author: Ashish Ramteke

Ashish Ramteke is a Software Professional, Part time blogger and founder of C# Tutorials, a popular blog on C#.Net. Ashish is having 6 years of work experience and an engineering degree in Computer Science and Engg.

2 Comments+ Add Comment

  • When i start the windows service it immediately stops?

    • Have you followed all the steps given in article on how to create a service?

Leave a comment