Calling a WCF Self Hosted Console Application

By | May 26, 2014

In previous WCF Tutorial, we discussed about different hosting options available for a WCF Service. We also implemented hosting a WCF Service in a Console Application. Now in this part, we are going to call our already console hosted StudentService from a client. I’ll strongly recommend to go through previous WCF Tutorial for creating and hosting a WCF Service in a Console Application.

Just to recap, what we did in previous post:

  • Created a StudentService Class Library
  • Add a Console Application to solution
  • Hosted the WCF service in Console Application
  • Run the hosted Service

Now, when the self hosted service is live and listening at following address:
            http://localhost:4321/StudentService

We will create a new client application to consume this self hosted WCF service. Let’s add a new Console Application project to existing solution as:

Client Application

For client application to communicate with our WCF Service “StudentService”, we will implement a proxy class. Windows Communication Foundation (WCF) supports multiple ways to generate proxy as:

  • Adding Service Reference to Client
  • Using Tool i.e. SvcUtil.exe, or
  • Implement ClientBase class.

Each proxy generate approach has some pros and cons that we will discuss in a separate WCF Tutorial but for this implementation, we will use the first approach i.e. Adding a Service Reference to client.

Now, right click on ClientApp project and choose “Add Service Reference”, following dialog will appear:

Add Service Reference

In address Textbox, paste the StudentService URL and press “Go” button. This tool will search for the service and list all available WCF services in Services area as shown. Provide a meaningful name for Namespace and press “OK” button.
Note: WCF Service must be running before we press the “Go” button. Also System.ServiceModel must be referenced to ClientApp project as we did in case of StudentHost application.

Under Service Reference, StudentProxy will be added as shown in below diagram:

WCF Proxy

As a new proxy is added to ClientApp, we will use this to call our WCF Service and get the output. Here is the detailed code for calling our Windows Communication Foundation StudentService.

WCF Client

Now, its time to run the application and see the output. First run the WCF host application and then client. You will see the following result:

WCF Hosting

This WCF Tutorial is completing the previous one using step by step approach and hopefully will be useful for understanding WCF Self hosting in a more practical way.

Top 10 Interview Questions and Answers Series:

Category: Uncategorized Tags:

About IMRAN ABDUL GHANI

Imran Abdul Ghani is working as Software Developer(Senior) with extensive knowledge in Web development technologies especially C#, ASP.NET, MVC, WCF, Web API, ADO.NET Entity Framework, jQuery etc. He has several years of experience in designing/developing enterprise level applications. He is Microsoft Certified Solution Developer for .NET (MCSD.NET) since 2005. You can reach his blogging at www.webdevelopmenthelp.net, www.topwcftutorials.net, and www.sharepointfordummies.net.