.NET Framework Developer Center > .NET Development Forums > Windows Communication Foundation > Building proxy with ChannelFactory vs svcutil ??
Ask a questionAsk a question
 

AnswerBuilding proxy with ChannelFactory vs svcutil ??

  • Friday, July 03, 2009 8:03 AMSerge Calderara Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    dear all,

    I have a simple question that i wonder the reason of it.
    I have seen in many sample and forums sample code that people most of the time use the ChannelFactory class to create the proxy and some others use service reference in project while others use the tool svcutil to generate the proxy class.

    Is there any real different by generating the proxy through ChannelFactory or is it a matter of taste ?

    thnaks for help
    serge
    Your experience is build from the one of others

Answers

  • Saturday, July 04, 2009 11:08 AMAlex80 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    From MSDN "How to use a ChannelFactory" http://msdn.microsoft.com/en-us/library/ms734681.aspx.

    To get started with a ChannelFactory you need to:

            BasicHttpBinding myBinding = new
     BasicHttpBinding();

    EndpointAddress myEndpoint =
    new EndpointAddress("http://localhost/MathService/Ep1" );

    ChannelFactory<IMath> myChannelFactory =
    new ChannelFactory<IMath>(myBinding, myEndpoint);

    // Create a channel.
    IMath wcfClient1 = myChannelFactory.CreateChannel();
    I believe the example given in MSDN is even a bit too complicated. Just passing the endpoint config name should be enough. So ultimately it's not a big difference. Just remember that you have to manage proper caching/reuse of the ChannelFactory yourself. Do not recreate the ChannelFactory everytime you create a channel!

    With a client it would be just a single object you have to create. Nothing else to worry about.

    MyService svc = new MyService();

    svc.MyOperation();


    Alex

All Replies

  • Friday, July 03, 2009 6:33 PMAlex80 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    A client created through a service reference is something like a "ready to go", "out of the box" solution. It's extremely easy to use and simplifies getting started with WCF.

    Svcutil is simply the command-line version of "Add service reference".

    Using a ChannelFactory is slightly more effort e.g. to instantiate one you have to specifiy its configuration manually. Under the hood, a client created through the service reference does all that stuff for you. A Channelfactory, however, is more flexible and efficent. I personally only use ChannelFactories because they allow me to inject them into a component for unit testing (with some wrappers). Furthermore, creating a ChannelFactory is quite expensive. Because of that a client caches those factories. As for me I prefer to control the lifecycle of the factory explicitly. It makes me more aware about when/how the factories are created. In order to use a ChannelFactory, you would still add a service reference (or through svcutil).

    Hth,
    Alex


    Alex
  • Friday, July 03, 2009 9:30 PMNordine Ben Bachir Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Use the channelfactory to share your datacontracts between the server and the client when you are writting both the client and the server. I personally use the channelfactory as often as possible, specially during development, when you don't want to constantly regenerate proxy classes.
    Nordine Ben Bachir
  • Friday, July 03, 2009 10:51 PMSerge Calderara Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thnaks for your comment;
    At the end performance is the same for both approach ?
    Your experience is build from the one of others
  • Friday, July 03, 2009 10:53 PMSerge Calderara Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    "Using a ChannelFactory is slightly more effort e.g. to instantiate one you have to specifiy its configuration manually"
    What do you mean that you need to configure it manually ?
    Do you means that you are not using either  a config file for you channel setting and endpoint and you define all those settings through code ?
    Your experience is build from the one of others
  • Saturday, July 04, 2009 11:08 AMAlex80 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    From MSDN "How to use a ChannelFactory" http://msdn.microsoft.com/en-us/library/ms734681.aspx.

    To get started with a ChannelFactory you need to:

            BasicHttpBinding myBinding = new
     BasicHttpBinding();

    EndpointAddress myEndpoint =
    new EndpointAddress("http://localhost/MathService/Ep1" );

    ChannelFactory<IMath> myChannelFactory =
    new ChannelFactory<IMath>(myBinding, myEndpoint);

    // Create a channel.
    IMath wcfClient1 = myChannelFactory.CreateChannel();
    I believe the example given in MSDN is even a bit too complicated. Just passing the endpoint config name should be enough. So ultimately it's not a big difference. Just remember that you have to manage proper caching/reuse of the ChannelFactory yourself. Do not recreate the ChannelFactory everytime you create a channel!

    With a client it would be just a single object you have to create. Nothing else to worry about.

    MyService svc = new MyService();

    svc.MyOperation();


    Alex