Microsoft Developer Network >
Página Inicial dos Fóruns
>
Windows Communication Foundation
>
Building proxy with ChannelFactory vs svcutil ??
Building proxy with ChannelFactory vs svcutil ??
- 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
Respostas
- 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();
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!
EndpointAddress myEndpoint =
new EndpointAddress("http://localhost/MathService/Ep1" );
ChannelFactory<IMath> myChannelFactory =
new ChannelFactory<IMath>(myBinding, myEndpoint);
// Create a channel.
IMath wcfClient1 = myChannelFactory.CreateChannel();
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- Marcado como RespostaSteven Cheng - MSFTMSFT, Moderadorquarta-feira, 8 de julho de 2009 11:48
Todas as Respostas
- 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 - 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 - Thnaks for your comment;
At the end performance is the same for both approach ?
Your experience is build from the one of others - "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 - 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();
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!
EndpointAddress myEndpoint =
new EndpointAddress("http://localhost/MathService/Ep1" );
ChannelFactory<IMath> myChannelFactory =
new ChannelFactory<IMath>(myBinding, myEndpoint);
// Create a channel.
IMath wcfClient1 = myChannelFactory.CreateChannel();
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- Marcado como RespostaSteven Cheng - MSFTMSFT, Moderadorquarta-feira, 8 de julho de 2009 11:48

