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

18,303

Solution 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);
}

Solution 2

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!

Share:
18,303
Admin
Author by

Admin

Updated on June 03, 2022

Comments

  • Admin
    Admin almost 2 years

    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 :)