exePath must be specified when not running inside a stand alone exe

↘锁芯ラ 提交于 2019-12-22 01:12:52

问题


When i am using a web application, the line of code below

Configuration objConfig = 
    ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None);

in class library are giving this error:

"exePath must be specified when not running inside a stand alone exe."

Previously a console application was being used, and the code could access the app.config. I tried using the System.Web.Configuration in class library but the dll was not present in the .Net tab for "Add reference".

Kindly help :)


回答1:


You need to use a different configuration manager in a web context. The following code block shows an example of how to deal with this:

System.Configuration.Configuration configuration = null;         
if (System.Web.HttpContext.Current != null)
{
   configuration =
       System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
}
else
{
  configuration =
      ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
}



回答2:


I'm not sure what you're doing; but at first glance it looks like you're trying to use code written for a WinForms application in a web environment. This almost certainly will not work, since your web app won't have the permissions you need.
Try looking up how to do this in a web environment (since you seem to be dealing with config files, try searching on WEB.CONFIG to start)




回答3:


I tried to use the answer from @shane but ended up with the same exception using Hangfire. This code worked for me though:

System.Configuration.Configuration configFile = null;
if (System.Web.HttpContext.Current != null)
{
    configFile =
        System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
}
else
{
    System.Configuration.ExeConfigurationFileMap map = new ExeConfigurationFileMap { ExeConfigFilename = $"{System.AppDomain.CurrentDomain.BaseDirectory}Web.Config" };
    configFile = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
}

Note that editing Web.config will cause the application pool to restart!



来源:https://stackoverflow.com/questions/16720576/exepath-must-be-specified-when-not-running-inside-a-stand-alone-exe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!