手动WCF和ChannelFactory
我刚刚开始使用 WCF,正在使用 Miguel A. Castro 发布的一篇旧文章,名为 WCF 手册方式。在文章中他提到使用 ChannelFactory 创建服务代理。在文章中,他展示了此代码来创建代理:
IProductAdmin productAdminChannel = new ChannelFactory<IProductAdmin>().CreateChannel();
当我尝试使用该代码与 web.config 中配置的端点时,我不断收到有关此端点为空的错误。显然,如果我在 ChannelFactory 构造函数上指定端点的名称,它就可以工作,但这似乎不是可重用性的最佳选择。但如果我这样做,它似乎也有效:
IProductAdmin productAdminChannel = new ChannelFactory<IProductAdmin>("*").CreateChannel();
这只是 ChannelFactory 类工作方式的改变吗(因为这篇文章已经有近 2 年历史了)?创建 WCF 服务代理和可重用性的“最佳实践”是什么?
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 productAdminChannel = 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不能代表原文,但这也许只是作者的疏忽?据我所知,您帖子中的第二个列表始终是使用配置文件创建通道的方法。传递 * 将使用文件中通道类型的默认/第一个配置。如果同一类型有多个命名配置,您还可以传递特定名称而不是 *。
我已经使用 (“*”) 路由好几年了,如果每种类型只有一个端点,那么这是一种很好的方法。
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.