MSDN > フォーラム ホーム > Windows Communication Foundation > Building proxy with ChannelFactory vs svcutil ??
質問する質問する
 

回答済みBuilding proxy with ChannelFactory vs svcutil ??

  • 2009年7月3日 8:03Serge Calderara ユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     
    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

回答

  • 2009年7月4日 11:08Alex80 ユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     回答済みコードあり
    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

すべての返信

  • 2009年7月3日 18:33Alex80 ユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     
    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
  • 2009年7月3日 21:30Nordine Ben Bachir ユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     
    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
  • 2009年7月3日 22:51Serge Calderara ユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     
    Thnaks for your comment;
    At the end performance is the same for both approach ?
    Your experience is build from the one of others
  • 2009年7月3日 22:53Serge Calderara ユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     
    "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
  • 2009年7月4日 11:08Alex80 ユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     回答済みコードあり
    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