App.config中的外WCF的ChannelFactory配置? [英] WCF ChannelFactory configuration outside of App.config?
问题描述
我有一个Windows服务,使得使用一个插件系统。我用下面的代码在插件基类提供每个DLL一个单独的配置(所以它会从 plugin.dll.config
读):
I've got a Windows Service that makes use of a plugin system. I'm using the following code in the plugin base class to provide a separate configuration per DLL (so it'll read from plugin.dll.config
):
string dllPath = Assembly.GetCallingAssembly().Location;
return ConfigurationManager.OpenExeConfiguration(dllPath);
这些插件需要对WCF服务调用,所以我跑入的问题是,新的ChannelFactory<>(endPointName)
只查找托管应用程序的App.config中的端点配置
These plugins need to make calls to WCF services, so the problem I'm running into is that new ChannelFactory<>("endPointName")
only looks in the hosted application's App.config for the endpoint configuration.
有没有办法简单地告诉的ChannelFactory在其他配置文件看或以某种方式注入我配置
对象?
Is there a way to simply tell the ChannelFactory to look in another configuration file or somehow inject my Configuration
object?
我能想到的唯一办法的办法,这是手动创建一个端点,绑定对象从值从 plugin.dll.config
阅读并把它们传递给的ChannelFactory化合物的一;>
重载。这确实似乎是,虽然重新发明轮子,它可能会带来麻烦与端点,使大量使用的行为和有约束力的配置。 或许有通过传递一个配置部分很容易地创建终端和对象绑定的方式?
The only way I can think of to approach this is to manually create an EndPoint and Binding object from values read in from plugin.dll.config
and pass them to one of the ChannelFactory<>
overloads. This really seems like recreating the wheel though, and it could get really messy with an endPoint that makes heavy use of behavior and binding configurations. Perhaps there's a way to create EndPoint and Binding objects easily by passing it a configuration section?
推荐答案
有2个选项。
选项1.使用频道。
如果您正在使用渠道直接合作,.NET 4.0和.NET 4.5拥有的 ConfigurationChannelFactory 。 看起来像这样在 MSDN的例子
If you are working with channels directly, .NET 4.0 and .NET 4.5 has the ConfigurationChannelFactory. The example on MSDN looks like this:
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = "Test.config";
Configuration newConfiguration = ConfigurationManager.OpenMappedExeConfiguration(
fileMap,
ConfigurationUserLevel.None);
ConfigurationChannelFactory<ICalculatorChannel> factory1 =
new ConfigurationChannelFactory<ICalculatorChannel>(
"endpoint1",
newConfiguration,
new EndpointAddress("http://localhost:8000/servicemodelsamples/service"));
ICalculatorChannel client1 = factory1.CreateChannel();
正如兰登指出的那样,你可以通过简单地传递null,则使用配置文件中的端点地址,像这样的:
As pointed out by Langdon, you can use the endpoint address from the configuration file by simply passing in null, like this:
var factory1 = new ConfigurationChannelFactory<ICalculatorChannel>(
"endpoint1",
newConfiguration,
null);
ICalculatorChannel client1 = factory1.CreateChannel();
这是在MSDN的文档。
选项2.代理工作。
如果您正在使用的代码生成的代理工作,就可以读取配置文件并加载的 ServiceModelSectionGroup 。有参与比单纯使用更多的工作在 ConfigurationChannelFactory
,但至少你可以继续使用生成的代理(即引擎盖下使用的ChannelFactory
和管理 IChannelFactory
为您服务。
If you're working with code-generated proxies, you can read the config file and load a ServiceModelSectionGroup. There is a bit more work involved than simply using the ConfigurationChannelFactory
but at least you can continue using the generated proxy (that under the hood uses a ChannelFactory
and manages the IChannelFactory
for you.
巴勃罗Cibraro显示了这样一个很好的例子在这里:的获取WCF绑定和行为从任何配置源
Pablo Cibraro shows a nice example of this here: Getting WCF Bindings and Behaviors from any config source
这篇关于App.config中的外WCF的ChannelFactory配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!