手动WCF和ChannelFactory

发布于 2024-10-11 10:00:11 字数 641 浏览 7 评论 0原文

我刚刚开始使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

第七度阳光i 2024-10-18 10:00:11

我不能代表原文,但这也许只是作者的疏忽?据我所知,您帖子中的第二个列表始终是使用配置文件创建通道的方法。传递 * 将使用文件中通道类型的默认/第一个配置。如果同一类型有多个命名配置,您还可以传递特定名称而不是 *。

我已经使用 (“*”) 路由好几年了,如果每种类型只有一个端点,那么这是一种很好的方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文