Creating Proxy at runtime for WCF Service

AID: 5729
  • Status: Published

1440 points

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);
  }
}
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:

Select allOpen 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;
        }
   }
}
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:

Select allOpen 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>
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:

Select allOpen 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>
                                    
1:
2:
3:
4:
5:
6:
7:
8:

Select allOpen 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);
           }
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:

Select allOpen 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.
Asked On
2011-05-25 at 08:03:23ID5729
Tags

WCF

,

runtime proxy

,

ChannelFactory

Topic

Web Services and WCF

Views
815

Comments

Add your Comment

Please Sign up or Log in to comment on this article.

Join Experts Exchange Today

Gain Access to all our Tech Resources

Get personalized answers

Ask unlimited questions

Access Proven Solutions

Search 3.2 million solutions

Read In-Depth How-To Guides

1000+ articles, demos, & tips

Watch Step by Step Tutorials

Learn direct from top tech pros

And Much More!

Your complete tech resource

See Plans and Pricing

30-day free trial. Register in 60 seconds.

Loading Advertisement...

Top Web Services and WCF Experts

  1. apeter

    41,940

    0 points yesterday

    Profile
    Rank: Sage
  2. kaufmed

    37,400

    0 points yesterday

    Profile
    Rank: Genius
  3. TheLearnedOne

    25,250

    0 points yesterday

    Profile
    Rank: Savant
  4. CodeCruiser

    24,800

    0 points yesterday

    Profile
    Rank: Genius
  5. DarrenD

    19,350

    0 points yesterday

    Profile
    Rank: Sage
  6. BuggyCoder

    18,965

    0 points yesterday

    Profile
    Rank: Sage
  7. ambience

    17,400

    0 points yesterday

    Profile
    Rank: Sage
  8. rkworlds

    16,498

    0 points yesterday

    Profile
    Rank: Genius
  9. srosebabu

    15,248

    2,000 points yesterday

    Profile
    Rank: Guru
  10. navneethegde

    14,625

    0 points yesterday

    Profile
    Rank: Wizard
  11. askanilkris

    9,600

    0 points yesterday

    Profile
    Rank: Master
  12. Mikal613

    6,800

    0 points yesterday

    Profile
    Rank: Genius
  13. mas_oz2003

    6,600

    0 points yesterday

    Profile
    Rank: Genius
  14. dj_alik

    6,600

    0 points yesterday

    Profile
    Rank: Sage
  15. DaveBaldwin

    5,925

    0 points yesterday

    Profile
    Rank: Genius
  16. santhimurthyd

    5,400

    0 points yesterday

    Profile
    Rank: Wizard
  17. gauthampj

    4,920

    0 points yesterday

    Profile
    Rank: Genius
  18. Tchuki

    4,800

    0 points yesterday

    Profile
    Rank: Wizard
  19. lucky85

    4,168

    0 points yesterday

    Profile
    Rank: Wizard
  20. cactus_data

    4,000

    0 points yesterday

    Profile
    Rank: Genius
  21. anarki_jimbel

    4,000

    0 points yesterday

    Profile
    Rank: Genius
  22. for_yan

    4,000

    0 points yesterday

    Profile
    Rank: Genius
  23. ve3ofa

    3,800

    0 points yesterday

    Profile
    Rank: Genius
  24. techChallenger1

    3,600

    0 points yesterday

    Profile
    Rank: Guru
  25. KBerger

    3,500

    0 points yesterday

    Profile

Hall Of Fame