<

[Webinar] Streamline your web hosting managementRegister Today

x

Creating Proxy at runtime for WCF Service

Published on
10,568 Points
4,468 Views
1 Endorsement
Last Modified:
Approved
Here I am going to explain creating proxies at runtime for WCF Service. So basically we use to generate proxies using Add Service Reference and then giving the Url of the WCF service then generate proxy files at client side. Ok, what if something gets changed at service? Do we need to regenerate the proxy files? Yes of course, we have to. How to over come such limitation? Here comes WCF handy in creating proxies at runtime. So we need not to create proxy using Add Service Reference option.
 
Actually we are not going to create proxy at runtime, we are just invoking calls to WCF service using ChannelFactory class which is available under System.ServiceModel namespace. But to achieve this we need to expose the Interface (which defines the service and operation) to the client. This option is more suitable if both the service and client is in your control.

Ok lets start implementing the same. I'll create a small calculator service.

Define an Service Contract and Operation Contract. Create an separate class library so that it can be exposed to client.
namespace CalculatorServiceContract
{
  [ServiceContract]
  public interface ICalculator 
  {
    [OperationContract]
    int Add (int a, int b);
  }
}

Open in new window


Next step is to implement the Interface in a class. Add reference to the CalculatorServiceContract dll in the Service class project. And implement as follows

namespace CalculatorService
{

    // Implementation of CalculatorServiceContract 

    public class CalculatorService : ICalculator
    {
        public int Add(int a, int b)
        {
            return a + b;
        }
   }
}

Open in new window


Next step is to configure web.config at service.
<system.serviceModel>
    <behaviors>
         
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
         
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <basicHttpBinding>
        <binding name="basicHttpBinding_AtServer"/>
      </basicHttpBinding>
    </bindings>
    <services>
      <service name="CalculatorService">
        <endpoint address="http://localhost/CalculatorSample/CalculatorService.svc"
                  binding="basicHttpBinding" bindingConfiguration="basicHttpBinding_AtServer"
                  contract="CalculatorServiceContract.ICalculator"/>
      </service>
      
    </services>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

Open in new window


Once done with the above steps, build the project and host in IIS. You can also use Visual Studio built-in web server by doing appropriate changes to web.config. For better flexibility I am using IIS.

Next is to create client project.

Create a Client project. It can be ASP.NET or Windows app or even console app. After creating new project try adding reference to CalculatorServiceContract dll. By doing this you are exposing the definition of service and operation to client. Also you will need to add the reference to System.ServiceModel.

Configure the web.config (in case of ASP.NET app) or app.config (Windows app or console app).

<system.serviceModel>
    <client>
      <endpoint binding="basicHttpBinding"
                address="http://localhost/CalculatorSample/CalculatorService.svc"
                contract="CalculatorServiceContract .ICalculator"
                 name="basicHttpBind"/>
    </client>
  </system.serviceModel>

Open in new window


Then try to invoke the Service calls using ChannelFactory class. ChannelFactory class is used to create endpoint listener. You can also have multiple endpoint listeners by creating channels of the same instance multiple times except with a different endpoint configuration. Below is the code for the same.

          public static void main(string args[])
          {

/*Create a ChannelFactory instance of type ICalculator. 
  You need to create an channel by specifying the binding name which is defined at web.config in client project which is shown above. 
  So the the below code will create an endpoint listener*/

            ICalculator calc = new ChannelFactory<ICalculator>("basicHttpBind").CreateChannel();
             int A = calc.Add(12, 15);
             Console.WriteLine(A);
           }

Open in new window


Here ChannelFactory will be invoking the service calls by using the endpoint binding information. Endpoint can be of any type. If you want to use netTcpBinding, then appropriate changes need to be done in web.config(server) and web.config(client).

The main advantage of following the above method is whenever any changes is done at service methods, client need not to worry about it. But if any endpoint configuration changes happen in service then appropriate changes has to be done to config file at client because service calls happens only through the endpoint configurations.
1
Comment
Author:Sathish DV
0 Comments

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Join & Write a Comment

Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
Suggested Courses
Course of the Month10 days, 22 hours left to enroll

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month