Manual WCF and ChannelFactory
I am just getting started with WCF and am using an older article posted by Miguel A. Castro called WCF the Manual Way. In the article he mentions using the ChannelFactory to create a service proxy. In the article, he shows this code to create the proxy :
IProductAdmin productAdminChannel = new ChannelFactory<IProductAdmin>().CreateChannel();
When I try using that code with the endpoints configured in the web.config, I keep getting errors about this endpoint being null. Obvioulsy it works if I specify the name of the endpoint on the ChannelFactory constructor, but that doesn't seem to be the best option for re-usability. But it also seems to work if I do this :
IProductAdmin pro开发者_开发技巧ductAdminChannel = new ChannelFactory<IProductAdmin>("*").CreateChannel();
Is this just a change in how the ChannelFactory class works (since the article is almost 2 years old)? What is the "best practice" for creating WCF Service Proxies and re-usability?
I can't speak for the original article, but maybe it's just an oversight by the author? As far as I know, the 2nd listing in your post has always been the way to create the channel using the config file. Passing a * will use the default/first configuration for the channel type in the file. You can also pass a specific name instead of a * in the case that you have multiple named configs for the same type.
I have been using the ("*") route for several years now, and it's a good way to go if you are only going to have one endpoint per type.
精彩评论