Click here to Skip to main content
15,886,362 members
Articles / WCF

Calling a WCF Self Hosted Console Application

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
9 Jun 2014CPOL2 min read 14.7K   11  
Calling a WCF Self Hosted Console Application

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 the previous WCF Tutorial for creating and hosting a WCF Service in a Console Application.

Just to recap, what we did in the 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 the 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 the 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 generated 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”, the 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 the 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, it's 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 a step by step approach and hopefully will be useful for understanding WCF Self hosting in a more practical way.

The post Calling a WCF Self Hosted Console Application appeared first on WCF Tutorial.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Emaratech
United Arab Emirates United Arab Emirates
Imran Abdul Ghani has more than 10 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 WCF Tutorials, Web Development, SharePoint for Dummies.

Comments and Discussions

 
-- There are no messages in this forum --