没有配置文件的 WCF 配置
有谁知道如何在不使用配置文件的情况下以编程方式公开 WCF 服务的好例子? 我知道 WCF 的服务对象模型现在更加丰富,所以我知道这是可能的。 我只是还没有看到如何执行此操作的示例。 相反,我想看看如何在没有配置文件的情况下进行消费。
在有人问之前,我有一个非常具体的需要,即在没有配置文件的情况下执行此操作。 我通常不会推荐这种做法,但正如我所说,在这种情况下有一个非常具体的需求。
Does anyone know of a good example of how to expose a WCF service programatically without the use of a configuration file? I know the service object model is much richer now with WCF, so I know it's possible. I just have not seen an example of how to do so. Conversely, I would like to see how consuming without a configuration file is done as well.
Before anyone asks, I have a very specific need to do this without configuration files. I would normally not recommend such a practice, but as I said, there is a very specific need in this case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
正如我发现的,在没有配置文件的情况下使用 Web 服务非常简单。 您只需创建一个绑定对象和地址对象,并将它们传递给客户端代理的构造函数或通用 ChannelFactory 实例。 您可以查看默认的 app.config 以了解要使用的设置,然后在实例化代理的某处创建一个静态帮助器方法:
Consuming a web service without a config file is very simple, as I've discovered. You simply need to create a binding object and address object and pass them either to the constructor of the client proxy or to a generic ChannelFactory instance. You can look at the default app.config to see what settings to use, then create a static helper method somewhere that instantiates your proxy:
在客户端和服务器端都很容易做到。 朱瓦尔·洛伊(Juval Lowy)的书中有很好的例子。
至于您对配置文件的评论,我想说配置文件是一个可怜的人在代码中完成它的第二个。 当您控制将连接到服务器的每个客户端并确保它们已更新并且用户无法找到它们并更改任何内容时,配置文件非常有用。 我发现 WCF 配置文件模型有局限性,设计起来有些困难,而且维护起来很困难。 总而言之,我认为微软将配置文件作为默认的处理方式是一个非常糟糕的决定。
编辑:您无法使用配置文件执行的操作之一是使用非默认构造函数创建服务。 这导致了 WCF 中的静态/全局变量和单例以及其他类型的无意义。
It's very easy to do on both the client and the server side. Juval Lowy's book has excellent examples.
As to your comment about the configuration files, I would say that the configuration files are a poor man's second to doing it in code. Configuration files are great when you control every client that will connect to your server and make sure they're updated, and that users can't find them and change anything. I find the WCF configuration file model to be limiting, mildly difficult to design, and a maintenance nightmare. All in all, I think it was a very poor decision by MS to make the configuration files the default way of doing things.
EDIT: One of the things you can't do with the configuration file is to create services with non-default constructors. This leads to static/global variables and singletons and other types of non-sense in WCF.
我发现下面链接中围绕该主题的博客文章非常有趣。
我喜欢的一个想法是能够将绑定或行为或地址 XML 部分从配置传递到适当的 WCF 对象,并让它处理属性的分配 - 目前您不能这样做。
与网络上的其他人一样,我也遇到了需要 WCF 实现使用与托管应用程序(.NET 2.0 Windows 服务)不同的配置文件的问题。
http://salvoz.com/blog/2007/12 /09/以编程方式设置-wcf-配置/
I found the blog post at the link below around this topic very interesting.
One idea I like is that of being able to just pass in a binding or behavior or address XML section from the configuration to the appropriate WCF object and let it handle the assigning of the properties - currently you cannot do this.
Like others on the web I am having issues around needing my WCF implementation to use a different configuration file than that of my hosting application (which is a .NET 2.0 Windows service).
http://salvoz.com/blog/2007/12/09/programmatically-setting-wcf-configuration/
在服务器上这并不容易 side..
对于客户端,您可以使用 ChannelFactory
It is not easy on the server side..
For client side, you can use ChannelFactory
所有 WCF 配置都可以通过编程方式完成。 因此可以在没有配置文件的情况下创建服务器和客户端。
我推荐 Juval Lowy 的《Programming WCF Services》一书,其中包含许多编程配置的示例。
All WCF configuration can be done programatically. So it's possible to create both servers and clients without a config file.
I recommend the book "Programming WCF Services" by Juval Lowy, which contains many examples of programmatic configuration.
如果您有兴趣消除在 IIS 托管的 web.config 中使用 System.ServiceModel 部分,我已在此处发布了一个如何执行此操作的示例 (http://bejabbers2.blogspot.com/2010/02/wcf-zero-config-in- net-35-part-ii.html)。 我展示了如何自定义 ServiceHost 以创建元数据和 wshttpbinding 端点。 我以通用方式完成此操作,不需要额外的编码。 对于那些不立即升级到 .NET 4.0 的人来说,这可能非常方便。
If you are interested in eliminating the usage of the System.ServiceModel section in the web.config for IIS hosting, I have posted an example of how to do that here (http://bejabbers2.blogspot.com/2010/02/wcf-zero-config-in-net-35-part-ii.html). I show how to customize a ServiceHost to create both metadata and wshttpbinding endpoints. I do it in a general purpose way that doesn't require additional coding. For those who aren't immediately upgrading to .NET 4.0 this can be pretty convenient.
在这里,这是完整且有效的代码。 我认为这会对你有很大帮助。 我一直在搜索,但从未找到完整的代码,这就是为什么我尝试放置完整且有效的代码。 祝你好运。
Here, this is complete and working code. I think it will help you a lot. I was searching and never finds a complete code that's why I tried to put complete and working code. Good luck.