MSDN > Home page del forum > Windows Communication Foundation > Building proxy with ChannelFactory vs svcutil ??
Formula una domandaFormula una domanda
 

Con rispostaBuilding proxy with ChannelFactory vs svcutil ??

  • venerdì 3 luglio 2009 8.03Serge Calderara Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    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

Risposte

  • sabato 4 luglio 2009 11.08Alex80 Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Con rispostaContiene codice
    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

Tutte le risposte

  • venerdì 3 luglio 2009 18.33Alex80 Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    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
  • venerdì 3 luglio 2009 21.30Nordine Ben Bachir Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    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
  • venerdì 3 luglio 2009 22.51Serge Calderara Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    Thnaks for your comment;
    At the end performance is the same for both approach ?
    Your experience is build from the one of others
  • venerdì 3 luglio 2009 22.53Serge Calderara Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    "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
  • sabato 4 luglio 2009 11.08Alex80 Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Con rispostaContiene codice
    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