简体   繁体   English

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

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

I have a class (myservice) in a wcf class library which accesses endpoints and appsettings from the app.config file in the class library. 我在wcf类库中有一个类(myservice),该类从类库中的app.config文件访问终结点和appsettings。 This class (myservice) is called from my controller (web project). 从我的控制器(Web项目)中调用此类(myservice)。 I find it's trying to read the web.config file for the web project and not the app.config file. 我发现它正在尝试读取Web项目的web.config文件,而不是app.config文件。 How would I access the correct file? 我将如何访问正确的文件?

Besides not getting any values, I'm getting this error 除了没有得到任何值,我得到这个错误

exePath must be specified when not running inside a stand alone exe 不在独立exe中运行时必须指定exePath

I'm using this in myservice- 我在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);

You should not use ConfigurationManager.OpenExeConfiguration in your library. 您不应在库中使用ConfigurationManager.OpenExeConfiguration The recommended way is to have your configuration passed from the using code; 推荐的方法是从使用代码传递配置。 if for some reasons you don't want to do that, you can use Settings mechanism that creates configuration sections without having you directly access the configuration file. 如果由于某些原因您不想这样做,则可以使用设置机制来创建配置部分,而无需直接访问配置文件。

The problem is with the line 问题是线

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

There is an overload for this call available 此呼叫有重载

ConfigurationManager.OpenExeConfiguration(string exepath)

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

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