简体   繁体   中英

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. This class (myservice) is called from my controller (web project). I find it's trying to read the web.config file for the web project and not the app.config file. 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

I'm using this in 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. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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