I have the following code to read the 'loggingConfiguration' from web.config file that is using EntLib 4.0
Configuration entLibConfig =
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration
(#"/Web.config");
LoggingSettings loggingSettings =
(LoggingSettings)entLibConfig.GetSection(LoggingSettings.SectionName);
The loggingSettings object is null after the 2nd line executes.
Any suggestions on what is going wrong here?
ExeConfigurationFileMap exc = new ExeConfigurationFileMap();
exc.ExeConfigFilename = #"Web.exe.config";
Configuration _config = ConfigurationManager.OpenMappedExeConfiguration(exc,ConfigurationUserLevel.None);
LoggingSettings log = _config.GetSection("loggingConfiguration") as LoggingSettings;
Try this, it works for me.
The reason why you get null returned for LoggingSettings is that there is no LoggingSettings configured in the web.config that you are opening. This is probably because the path specified is incorrect.
I have a web appliation set up with 2 web.configs: The first in the root and the second in a Config folder.
/Root
web.config
/Root/Config
web.config
The web.config in the Config folder contains the LoggingSettings. The code to read the LoggingSettings from a page that is not located in the Config folder is:
Configuration entLibConfig =
WebConfigurationManager.OpenWebConfiguration(#"~/Config");
LoggingSettings loggingSettings =
(LoggingSettings)entLibConfig.GetSection(LoggingSettings.SectionName);
This should work in the Development Web Server as well as IIS.
Related
I have ASP.NET Core (2.1) project that has appsettings.json. I use WebHost.CreateDefaultBuilder(). The appsettings.json file has following configuration in File Properties:
Build Action: Content
Copy to Output Directory: Do not copy
After build the appsettings.json ends up in bin\Debug\netcoreapp2.1\MyProj.runtimeconfig.json.
The ASP.NET Core runtime loads it fine.
I created WebJobs (for .Net Core 2.1) and wanted to do the same - set Build Action to Content and let it loaded. In the Main() of Program.cs I have code like
var builder = new HostBuilder()
...
.ConfigureAppConfiguration(b =>
{
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
b.SetBasePath(Directory.GetCurrentDirectory());
b.AddJsonFile("appsettings.json", false, true);
b.AddJsonFile($"appsettings.{environment}.json", true, true);
b.AddEnvironmentVariables();
// Adding command line as a configuration source
if (args != null)
{
b.AddCommandLine(args);
}
}
But the runtime tries to load appsettings.json (instead of MyWebJobProj.runtimeconfig.json). So I had to set Build Action to None and Copy to Output Directory to Always.
However I would prefer the same approach like in ASP.NET Core - it handles somehow the file name transformation. Although in WebHost.CreateDefaultBuilder() is basically the same code like I have in my WebJob. What does the magic file name transformation in the configuration and why it works only in one type of project?
The file [ProjName].runtimeconfig.json has a completely different meaning than appsettings.json. Ensure that appsettings.json was copied to output (set 'Copy to output' to 'always' or 'newer').
How to find the Config File location via ConfigurationManager?
I have the ConfigurationManager class in code and I'm debugging it. I'd like to know to which config file it's pointing to (web.config or app.config, etc).
Is there any property or method on the ConfigurationManager that can help with this?
The configuration file itself is represented by Configuration object. To get this object, run this:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
Then you can view the file path via config.FilePath.
Update. As pointed out by Schadensbegrenzer for web application you will need another code to load the config file:
Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
Can somebody tell me how to set the global system.net/mailSettings configuration in ASP.NET?
If I change it in the GUI of IIS Manager the file:
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\web.config
is changed.
Is there a way to access this file via the Microsoft.Web.Administration namespace?
I got it:
System.Configuration.Configuration webconfig = System.Web.Configuration.WebConfigurationManager.OpenMachineConfiguration();
System.Net.Configuration.MailSettingsSectionGroup mailSection = webconfig.GetSectionGroup("system.net/mailSettings") as System.Net.Configuration.MailSettingsSectionGroup;
mailSection.Smtp.From = "aspx#defaultmail.com";
mailSection.Smtp.Network.Host = Service.Config.SMTP_SERVER;
mailSection.Smtp.Network.Port = Service.Config.SMTP_PORT;
webconfig.Save(System.Configuration.ConfigurationSaveMode.Modified);
My site is structured in this way:
Root Directory
Arcade
default.aspx
web.config
default.aspx
web.config
I have a method Method1() which accesses:
System.Configuration.ConfigurationManager.AppSettings["Total_Unique_Plays_Required_For_High_Score_Board"]
This value exists in the root/Arcade/Web.config file but not in the root/web.config file.
When I execute Method1() from a page in the /arcade directory it works fine. However, when I execute this method as a timed event from global.asax it searches for the value in the root web.config file and throws a System.NullException.
Does anyone know how I can specify to search for the value in the root/arcade/web.config file and not the root/web.config file?
You can open the web.config file first.
So calling this will load your child file; notice you give the path to the folder containing the web.config, not the actual config file.
var config = WebConfigurationManager.OpenWebConfiguration("~/Arcade");
You can now get your values, like:
string MyValue = config.AppSettings.Settings["MySetting"].Value;
You can also get the list of app settings by calling:
KeyValueConfigurationCollection appSettings = config.AppSettings.Settings;
OK so we have something that is currently using OpenExeConfiguration for reading a config file, however this doesn't work when running in the web context.
I've tried a variety of different ways of opening the web.config programmatically but I can't seem to get it to read the correct web.config file. In case it matters I am currently debugging it in VS 2008.
1. config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(System.Web.HttpContext.Current.Request.ApplicationPath);
2. config = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap { ExeConfigFilename = "web.config" }, ConfigurationUserLevel.None);
3. config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
4. config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(null);
5. System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath);
It either opens up the wrong config file (either the machine config, or the VS /IDE/Web.config) or complains about the error:
{System.Configuration.ConfigurationErrorsException: An error occurred loading a configuration file: Failed to map the path '/'. ---> System.InvalidOperationException: Failed to map the path '/'.
Edit -
OK so a combination of
config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
AND running Visual Studio 2008 As Administrator worked. Am hoping we don't run into security/permission issues when we deploy to our web server / client environments!
So in the end I used this code (had to handle whether the web application was running, or if our unit test code was running).
System.Configuration.Configuration config = null;
if (System.Web.HttpContext.Current != null && !System.Web.HttpContext.Current.Request.PhysicalPath.Equals(string.Empty))
config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
else
config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
Also have to be running Visual Studio in Administrator mode - which I found out you can set as a property on your shortcut so you don't need to remember each time in Windows 7 to right click and run as administrator :)