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:
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:
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:
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:
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.
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:
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.
The post Calling a WCF Self Hosted Console Application appeared first on WCF Tutorial.