Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / WCF

3 Ways to generate proxy for WCF Service

3.00/5 (5 votes)
16 Jun 2014CPOL3 min read 127.8K  
In Windows Communication Foundation, for a client application to communicate with a WCF Service, we have following options: Using ChannelFactory Generating Proxies I have already discussed about difference between ChannelFactory and Proxies in one of my previous WCF Tutorial on this blog. In this ar

In Windows Communication Foundation, for a client application to communicate with a WCF Service, we have following options:

  • Using ChannelFactory
  • Generating Proxies

I have already discussed about difference between ChannelFactory and Proxies in one of my previous WCF Tutorial on this blog. In this article, we will focus only on multiple ways to generate proxy in Windows Communication Foundation.

“A Proxy in Windows Communication Foundation is a class that enables client applications to communicate with a service by sending and receiving messages. It actually encapsulates a number of service details like service path, service implementation technology, platform being used, communication protocol etc. as well as all the methods (signature only) of the Service Contract.”

Windows Communication Foundation supports following three ways to generate proxy for a WCF Service.

  • Adding Service Reference
  • Implementing ClientBase
  • Using Tool i.e. SvcUtil.exe

So, in this WCF Tutorial, we will carry out all three possible ways to generate proxy for a WCF service. Let’s create a simple WCF Service first.

Create WCF Service

I have already provided implementation for a new WCF Service creation and hosting in a separate WCF Tutorial here. Please follow the steps to create a “StudentService” and host in a console application.

Option 1: Generate Proxy Adding Service Reference

Implementation of generating proxy by adding a service reference is also available here for the same WCF service i.e. “StudentService”.

Option 2: Generate Proxy by implementing ClientBase<T> class

Generating proxy by using ClientBase<T> class option has an advantage that it creates proxy at run time, so it will accommodate service implementation changes. Let’s follow the steps to generate proxy.

    • Add a Client Project to solution named as “ClientApp2″ that is basically a Console Application.

ClientBase Proxy

    • Add reference of StudentService Project to ClientApp2.
    • Add following proxy class using ClientBase<T> as:
public class StudentServiceProxy : ClientBase<IStudentService>, IStudentService
{
         public string GetStudentInfo(int studentId)
         {
               return base.Channel.GetStudentInfo(studentId);
         }
}

Note: Don’t forget to add “using StudentService” to class.

    • Following is the code for program.cs class in ClientApp2. We are using above created proxy class to communicate with WCF Service “StudentService“.
C#
class Program
{
        static void Main(string[] args)
        {
                    StudentServiceProxy myclient;
                    myclient = new StudentServiceProxy();

                    int studentId = 1;
                    Console.WriteLine("Calling StudentService with StudentId = 1…..");
                    Console.WriteLine("Student Name = {0}", myclient.GetStudentInfo(studentId));
                    Console.ReadLine();
        }
 }

Note: Don’t forget to add “using System.ServiceModel” to class.

  • App.Config file will have following configuration:
<system.serviceModel>
          <bindings>
              <wsHttpBinding>
                  <binding name="WSHttpBinding_IStudentService" />
              </wsHttpBinding>
          </bindings>
          <client>
               <endpoint address="http://localhost:4321/StudentService"
                                binding="wsHttpBinding"
                                bindingConfiguration="WSHttpBinding_IStudentService"
                                contract="StudentService.IStudentService"
                                name="WSHttpBinding_IStudentService">
               </endpoint>
           </client>
</system.serviceModel>

Now, when we run the client application, we will receive the following same output as we get in earlier option “Adding Service Reference”.

WCF Proxy

Option 3: Generate Proxy by using SvcUtil.exe Tool

Let’s generate proxy by using third option i.e. SvcUtil.exe Tool by following step by step approach.

  • Add a Client Project to solution named as “ClientApp3″ that is basically a Console Application.

SvcUtil Tool

  • Our WCF Service must be running, so let’s run our service.

WCF Service Running

  • Open Visual Studio Command Prompt and generate proxy using svcutil.exe tool as follows:

svcutil http://localhost:4321/StudentService /out:StudentServiceProxy.cs

SvcUtil Command It will generate a new class “StudentServiceProxy.cs”.

  • Add newly created proxy class to our client application “ClientApp3″.
  • Call WCF Service using proxy class as:
C#
class Program
{
        static void Main(string[] args)
        {
                   StudentServiceClient myclient;
                   myclient = new StudentServiceClient();
                   int studentId = 1;
                   Console.WriteLine("Calling StudentService with StudentId = 1…..");
                   Console.WriteLine("Student Name = {0}", myclient.GetStudentInfo(studentId));
                   Console.ReadLine();
         }
}

When we run the application, it will call StudentService method getStudentInfo and generate the same output as we received in other options.

Hopefully, this WCF tutorial will be helpful to understand concepts of WCF Proxy and different ways to generate it.

Other Related Tutorials

The post 3 Ways to generate proxy for WCF Service 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)