繁体   English   中英

从Web项目访问类库中的app.config文件

[英]Access app.config file in class library from web project

我在wcf类库中有一个类(myservice),该类从类库中的app.config文件访问终结点和appsettings。 从我的控制器(Web项目)中调用此类(myservice)。 我发现它正在尝试读取Web项目的web.config文件,而不是app.config文件。 我将如何访问正确的文件?

除了没有得到任何值,我得到这个错误

不在独立exe中运行时必须指定exePath

我在myservice中使用它-

var appSettings = ConfigurationManager.AppSettings;

var configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
var serviceModelSectionGroup = ServiceModelSectionGroup.GetSectionGroup(configuration);
var clientSection = serviceModelSectionGroup.Client;
var endpoint = clientSection.Endpoints;
return new EndpointAddress(endpoint[0].Address);

您不应在库中使用ConfigurationManager.OpenExeConfiguration 推荐的方法是从使用代码传递配置。 如果由于某些原因您不想这样做,则可以使用设置机制来创建配置部分,而无需直接访问配置文件。

问题是线

 var configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);

此呼叫有重载

ConfigurationManager.OpenExeConfiguration(string exepath)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM