从不同的配置文件加载wcf

从不同的配置文件加载wcf,wcf,app-config,Wcf,App Config,我正在尝试使用与应用程序启动路径不同的路径中的应用程序配置文件。我发现我可以使用以下命令更改配置文件名: AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", NewConfigFullFilename); 在此之后,某些方法可以正常工作,对于ex.ConfigurationManager.OpenExeConfigurationConfigurationUserLevel.None,将加载正确的文件,而对于ex.ConfigurationMan

我正在尝试使用与应用程序启动路径不同的路径中的应用程序配置文件。我发现我可以使用以下命令更改配置文件名:

AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", NewConfigFullFilename);
在此之后,某些方法可以正常工作,对于ex.ConfigurationManager.OpenExeConfigurationConfigurationUserLevel.None,将加载正确的文件,而对于ex.ConfigurationManager.GetSectionsystem.serviceModel/client,其他方法将不工作,对于ex.ConfigurationManager.GetSectionsystem.serviceModel/client,将不从文件加载节

我发现这是因为我需要使用配置文件创建服务或通道工厂。写作:

channelFactory = new ChannelFactory<TContract>(EndpointConfigurationName);
现在我想知道是否还有其他方法可以做到这一点,或者AppDomain.CurrentDomain.SetDataAPP_CONFIG_文件不够是一个bug,或者是其他任何建议


谢谢

如果至少要使用一个双工操作,请考虑实施自定义ServiceHost和ChannelFactory以及DuplexChannelFactory

您可以将配置类实例传递给它们的构造函数,并在必要时使用通过该实例检索的相关部分

这可能需要比您已经完成的工作更多的工作,但使用以下内容,代码肯定会更整洁:

// Use path to relevant configuration file
string configPath = null;
Configuration config = ConfigurationManager.OpenExeConfiguration( configPath );
CustomServiceHost host = new CustomServiceHost( typeof( Service ), config );
host.Open();
// Use path to relevant configuration file
string configPath = null;
Configuration config = ConfigurationManager.OpenExeConfiguration( configPath );
CustomServiceHost host = new CustomServiceHost( typeof( Service ), config );
host.Open();