In asp.net web api using Unity, I could register my services in UnityConfig.cs:
public static class UnityConfig
{
public static void RegisterComponents()
{
var container = new UnityContainer();
container.RegisterType<ITestService, TestService>();
//above code needs to be read from config file
GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
}
}
Now, I have a configuration file (located in the root of the project) where stores all those types to be registered:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
</configSections>
<unity>
<alias alias="ITestService" type="IMyServices.ITestService, IMyServices" />
<container>
<register type="ITestService" mapTo="MyServices.TestService,MyServices" />
</container>
</unity>
</configuration>
How can I get container from this file?
Below is a similar question but has not been resolved:
ASP.NET - Unity - Read configuration section from external configuration file
I find the solution to my problem which uses ExeConfigurationFileMap to specify the file path and load the specified configuration file explicitly.
public static class UnityConfig
{
public static void RegisterComponents()
{
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = "my/file/path";
Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
var section = (UnityConfigurationSection)configuration.GetSection("unity");
IUnityContainer unityContainer = new UnityContainer();
unityContainer.LoadConfiguration(section);
GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(unityContainer);
}
}
The web application I'm working on uses log4net for logging. A requirement of the project is that the connections strings should be encrypted. How do I tell log4net to use the decrypted value?
For example:
<log4net>
<root>
<level value="Debug"/>
<appender-ref ref="AdoNetAppender"/>
</root>
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="1"/>
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
<connectionString value="encryptedconnectionstringhere=="/>
Is there a way to accomplish this?
When implementing drumboog's answer, I ran into stackoverflow exceptions due to an infinitely recursive method call. This is essentially what I ended up using.
public class CustomAdoNetAppender : AdoNetAppender
{
private string _connectionString;
protected override string ResolveConnectionString(out string connectionStringContext)
{
if(string.IsNullOrEmpty(_connectionString))
{
var decrypt = new MyDecyptionLib();
_connectionString = decrypt.MyDecryptionFunction(ConfigurationManager.AppSettings["Connection"]);
}
connectionStringContext = _connectionString;
return connectionStringContext;
}
}
...and in the log4net config section
<appender name="AdoNetAppender" type="My.Name.Space.To.CustomAdoNetAppender">
Aside from writing a custom appender, you could encrypt the entire configuration section:
http://msdn.microsoft.com/en-us/library/zhhddkxy.aspx
Programmatically encrypting a config-file in .NET
Edit:
log4net is open source, so you can also try looking through their code and customizing their appender to fit your needs... maybe something like this:
public class DecryptConnectionStringAdoNetAppender : AdoNetAppender
{
protected override string ResolveConnectionString(out string connectionStringContext)
{
string result = base.ResolveConnectionString(out connectionStringContext);
if (String.IsNullOrEmpty(result))
{
return result;
}
else
{
Decrypt(result);
}
}
private string Decrypt(string encryptedValue)
{
// Your code goes here.
}
}
Then update the type attribute of the appender element in the config file:
<appender name="AdoNetAppender" type="Your.Namespace.DecryptConnectionStringAdoNetAppender">
I have a custom ConfigurationSection in my application:
public class SettingsSection : ConfigurationSection
{
[ConfigurationProperty("Setting")]
public MyElement Setting
{
get
{
return (MyElement)this["Setting"];
}
set { this["Setting"] = value; }
}
}
public class MyElement : ConfigurationElement
{
public override bool IsReadOnly()
{
return false;
}
[ConfigurationProperty("Server")]
public string Server
{
get { return (string)this["Server"]; }
set { this["Server"] = value; }
}
}
In my web.config
<configSections>
<sectionGroup name="mySettingsGroup">
<section name="Setting"
type="MyWebApp.SettingsSection"
requirePermission="false"
restartOnExternalChanges="true"
allowDefinition="Everywhere" />
</sectionGroup>
</configSections>
<mySettingsGroup>
<Setting>
<MyElement Server="serverName" />
</Setting>
</mySettingsGroup>
Reading the section works fine. The issue I'm having is that when I read the section via
var settings = (SettingsSection)WebConfigurationManager.GetSection("mySettingsGroup/Setting");
And then I proceed to modify the Server property:
settings.Server = "something";
This doesn't modify the "Server" property in the web.config file.
Note: This needs to work under medium-trust, so I can't use WebConfigurationManager.OpenWebConfiguration which works fine. Is there an explicit way to tell the ConfigSection to save itself?
Short answer - no. .NET team were (allegedly) meant to fix this in v4, but it didn't happen.
The reason is because using WebConfigurationManager.GetSection returns nested read-only NameValueCollections, which do not persist when you change their values. Using WebConfigurationManager.OpenWebConfiguration, as you've quite rightly ascertained, is the only way to obtain read-write access to the config - but then you'll get a FileIOPermission exception thrown, as OpenWebConfiguration attempts to load all inherited configs down to your web.config - which include the machine-level web.config and machine.config files in C:\WINDOWS\Microsoft.NET\Framework, which are explicitly out-of-scope of Medium Trust.
Long answer - use XDocument / XmlDocument and XPath to get/set config values.
I'm running IIS 7 Integrated mode and I'm getting
Request is not available in this context
when I try to access it in a Log4Net related function that is called from Application_Start. This is the line of code I've
if (HttpContext.Current != null && HttpContext.Current.Request != null)
and an exception is being thrown for second comparison.
What else can I check other than checking HttpContext.Current.Request for null??
A similar question is posted #
Request is not available in this context exception when runnig mvc on iis7.5
but no relevant answer there either.
Please see IIS7 Integrated mode: Request is not available in this context exception in Application_Start:
The “Request is not available in this
context” exception is one of the more
common errors you may receive on when
moving ASP.NET applications to
Integrated mode on IIS 7.0. This
exception happens in your
implementation of the
Application_Start method in the
global.asax file if you attempt to
access the HttpContext of the request
that started the application.
When you have custom logging logic, it is rather annoying to be forced either not to log application_start or to have to let an exception occurs in the logger (even if handled).
It appears that rather than testing for Request availability, you can test for Handler availability: when there is no Request, it would be strange to still have a request handler. And testing for Handler does not raise that dreaded Request is not available in this context exception.
So you may change your code to:
var currContext = HttpContext.Current;
if (currContext != null && currContext.Handler != null)
Beware, in the context of a http module, Handler may not be defined though Request and Response are defined (I have seen that in BeginRequest event). So if you need request/response logging in a custom http module, my answer may not be suitable.
This is very classic case: If you end up having to check for any data provided by the http instance then consider moving that code under the BeginRequest event.
void Application_BeginRequest(Object source, EventArgs e)
This is the right place to check for http headers, query string and etc...
Application_Start is for the settings that apply for the application entire run time, such as routing, filters, logging and so on.
Please, don't apply any workarounds such as static .ctor or switching to the Classic mode unless there's no way to move the code from the Start to BeginRequest. that should be doable for the vast majority of your cases.
Since there's no Request context in the pipeline during app start anymore, I can't imagine there's any way to guess what server/port the next actual request might come in on. You have to so it on Begin_Session.
Here's what I'm using when not in Classic Mode. The overhead is negligible.
/// <summary>
/// Class is called only on the first request
/// </summary>
private class AppStart
{
static bool _init = false;
private static Object _lock = new Object();
/// <summary>
/// Does nothing after first request
/// </summary>
/// <param name="context"></param>
public static void Start(HttpContext context)
{
if (_init)
{
return;
}
//create class level lock in case multiple sessions start simultaneously
lock (_lock)
{
if (!_init)
{
string server = context.Request.ServerVariables["SERVER_NAME"];
string port = context.Request.ServerVariables["SERVER_PORT"];
HttpRuntime.Cache.Insert("basePath", "http://" + server + ":" + port + "/");
_init = true;
}
}
}
}
protected void Session_Start(object sender, EventArgs e)
{
//initializes Cache on first request
AppStart.Start(HttpContext.Current);
}
Based on OP detailed needs explained in comments, a more appropriate solution exists.
The OP states he wishes to add custom data in its logs with log4net, data related to requests.
Rather than wrapping each log4net call into a custom centralized log call which handles retrieving request related data (on each log call), log4net features context dictionaries for setting up custom additional data to log. Using those dictionnaries allows to position your request log data for current request at BeginRequest event, then to dismiss it at EndRequest event. Any log in between will benefit from these custom data.
And things that do not happen in a request context will not try to log request related data, eliminating the need to test for request availability. This solution matches the principle Arman McHitaryan was suggesting in his answer.
For this solution to work, you will also need some additional configuration on your log4net appenders in order for them to log your custom data.
This solution can be easily implemented as a custom log enhancement module. Here is some sample code for it:
using System;
using System.Web;
using log4net;
using log4net.Core;
namespace YourNameSpace
{
public class LogHttpModule : IHttpModule
{
public void Dispose()
{
// nothing to free
}
private const string _ipKey = "IP";
private const string _urlKey = "URL";
private const string _refererKey = "Referer";
private const string _userAgentKey = "UserAgent";
private const string _userNameKey = "userName";
public void Init(HttpApplication context)
{
context.BeginRequest += WebAppli_BeginRequest;
context.PostAuthenticateRequest += WebAppli_PostAuthenticateRequest;
// All custom properties must be initialized, otherwise log4net will not get
// them from HttpContext.
InitValueProviders(_ipKey, _urlKey, _refererKey, _userAgentKey,
_userNameKey);
}
private void InitValueProviders(params string[] valueKeys)
{
if (valueKeys == null)
return;
foreach(var key in valueKeys)
{
GlobalContext.Properties[key] = new HttpContextValueProvider(key);
}
}
private void WebAppli_BeginRequest(object sender, EventArgs e)
{
var currContext = HttpContext.Current;
currContext.Items[_ipKey] = currContext.Request.UserHostAddress;
currContext.Items[_urlKey] = currContext.Request.Url.AbsoluteUri;
currContext.Items[_refererKey] = currContext.Request.UrlReferrer != null ?
currContext.Request.UrlReferrer.AbsoluteUri : null;
currContext.Items[_userAgentKey] = currContext.Request.UserAgent;
}
private void WebAppli_PostAuthenticateRequest(object sender, EventArgs e)
{
var currContext = HttpContext.Current;
// log4net doc states that %identity is "extremely slow":
// http://logging.apache.org/log4net/release/sdk/log4net.Layout.PatternLayout.html
// So here is some custom retrieval logic for it, so bad, especialy since I
// tend to think this is a missed copy/paste in that documentation.
// Indeed, we can find by inspection in default properties fetch by log4net a
// log4net:Identity property with the data, but it looks undocumented...
currContext.Items[_userNameKey] = currContext.User.Identity.Name;
}
}
// General idea coming from
// http://piers7.blogspot.fr/2005/12/log4net-context-problems-with-aspnet.html
// We can not use log4net ThreadContext or LogicalThreadContext with asp.net, since
// asp.net may switch thread while serving a request, and reset the call context
// in the process.
public class HttpContextValueProvider : IFixingRequired
{
private string _contextKey;
public HttpContextValueProvider(string contextKey)
{
_contextKey = contextKey;
}
public override string ToString()
{
var currContext = HttpContext.Current;
if (currContext == null)
return null;
var value = currContext.Items[_contextKey];
if (value == null)
return null;
return value.ToString();
}
object IFixingRequired.GetFixedObject()
{
return ToString();
}
}
}
Add it to your site, IIS 7+ configuration sample:
<system.webServer>
<!-- other stuff removed ... -->
<modules>
<!-- other stuff removed ... -->
<add name="LogEnhancer" type="YourNameSpace.LogHttpModule, YourAssemblyName" preCondition="managedHandler" />
<!-- other stuff removed ... -->
</modules>
<!-- other stuff removed ... -->
</system.webServer>
And set up appenders to log those additional properties, sample config:
<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<!-- other stuff removed ... -->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message - %property%newline%exception" />
</layout>
</appender>
<appender name="SqlAppender" type="log4net.Appender.AdoNetAppender">
<!-- other stuff removed ... -->
<commandText value="INSERT INTO YourLogTable ([Date],[Thread],[Level],[Logger],[UserName],[Message],[Exception],[Ip],[Url],[Referer],[UserAgent]) VALUES (#log_date, #thread, #log_level, #logger, #userName, #message, #exception, #Ip, #Url, #Referer, #UserAgent)" />
<!-- other parameters removed ... -->
<parameter>
<parameterName value="#userName" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{userName}" />
</layout>
</parameter>
<parameter>
<parameterName value="#Ip"/>
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{Ip}" />
</layout>
</parameter>
<parameter>
<parameterName value="#Url"/>
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{Url}" />
</layout>
</parameter>
<parameter>
<parameterName value="#Referer"/>
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{Referer}" />
</layout>
</parameter>
<parameter>
<parameterName value="#UserAgent"/>
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{UserAgent}" />
</layout>
</parameter>
</appender>
<!-- other stuff removed ... -->
</log4net>
You can get around the problem without switching to classic mode and still use Application_Start
public class Global : HttpApplication
{
private static HttpRequest initialRequest;
static Global()
{
initialRequest = HttpContext.Current.Request;
}
void Application_Start(object sender, EventArgs e)
{
//access the initial request here
}
For some reason, the static type is created with a request in its HTTPContext, allowing you to store it and reuse it immediately in the Application_Start event
This worked for me - if you have to log in Application_Start, do it before you modify the context. You will get a log entry, just with no source, like:
2019-03-12 09:35:43,659 INFO (null) - Application Started
I generally log both the Application_Start and Session_Start, so I see more detail in the next message
2019-03-12 09:35:45,064 INFO ~/Leads/Leads.aspx - Session Started (Local)
protected void Application_Start(object sender, EventArgs e)
{
log4net.Config.XmlConfigurator.Configure();
log.Info("Application Started");
GlobalContext.Properties["page"] = new GetCurrentPage();
}
protected void Session_Start(object sender, EventArgs e)
{
Globals._Environment = WebAppConfig.getEnvironment(Request.Url.AbsoluteUri, Properties.Settings.Default.LocalOverride);
log.Info(string.Format("Session Started ({0})", Globals._Environment));
}
I was able to workaround/hack this problem by moving in to "Classic" mode from "integrated" mode.
In visual studio 2012, When I published the solution mistakenly with 'debug' option I got this exception. With 'release' option it never occurred. Hope it helps.
Set application pool to .NET v4.5 Classic
public bool StartVideo(byte channel)
{
try
{
CommandObject command = new CommandObject(Commands.START_VIDEO, new byte[] {channel}, channel);
m_ResponseEvent.Reset();
lock (m_Commands)
{
m_Commands.Enqueue(command);
}
if (m_ResponseEvent.WaitOne(5000, true))
{
return m_Response == null ? false : true;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
return false;
}
You can use following:
protected void Application_Start(object sender, EventArgs e)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(StartMySystem));
}
private void StartMySystem(object state)
{
Log(HttpContext.Current.Request.ToString());
}
do this in global.asax.cs:
protected void Application_Start()
{
//string ServerSoftware = Context.Request.ServerVariables["SERVER_SOFTWARE"];
string server = Context.Request.ServerVariables["SERVER_NAME"];
string port = Context.Request.ServerVariables["SERVER_PORT"];
HttpRuntime.Cache.Insert("basePath", "http://" + server + ":" + port + "/");
// ...
}
works like a charm. this.Context.Request is there...
this.Request throws exception intentionally based on a flag
I'd like to store a set of key/value pairs in the application settings of my ASP.NET web app, but I'm not finding a straightforward way to do that. For example, these two questions tell me that StringDictionary etc. won't serialize to XML and suggest that I'll have to roll my own implementation. But it seems like this should be easier to do; after all, web.config is XML and < applicationSettings> is essentially a collection of key/value pairs, so it feels like I'm missing something obvious. Given my specific case below, do I really have to roll my own serialization, or is there an easier workaround?
The web app in question is a basic contact form that sends email to different recipients based on the value of a parameter; e.g. http://www.examplesite.com/Contact.aspx?recipient=support would send email to SupportGroup#exampledomain.com.
The goal is to be able add or remove recipients (or change their addresses) by editing the web.config file so that I don't have to recompile and can easily maintain different configurations in test and production environments. For example:
// I can use something like this for the sender address
SmtpMsg.From = New MailAddress(My.Settings.EmailSender)
// And then just edit this part of web.config to use
// different addresses in different environments.
<setting name="EmailSender" serializeAs="String">
<value>webcontact#exampledomain.com</value>
</setting>
// I want something like this for the recipients
SmtpMsg.To.Add(My.Settings.Recipients("support"))
// and presumably some sort of equivalent xml in web.config
// maybe something like this???
<recipients>
<item name="support" serializeAs="String">
<value>SupportGroup#exampledomain.com</value>
</item>
<!-- add or remove item elements here -->
</recipients>
edit: replaced VB comments w/ C# comments because of the code-coloring
The simplist way would obviously be to just drop them in the app settings, but it wouldn't be very neat:
<appSettings>
<add key="EmailSupport" value="support#somedomain.com" />
<add key="EmailSales" value="sales#somedomain.com" />
</appSettings>
Then in your code you're just doing something like:
if (!string.IsNullOrEmpty(Request["recipient"])) {
string recipientEmail =
WebConfigurationManager.AppSettings["Email" + Request["recipient"]];
// Send your email to recipientEmail
}
If you want to be a bit neater, you can create a custom Configuration Section like this (C# I'm afraid, but the docs have VB as well):
namespace EmailSystem {
public class EmailRecipientsSection : ConfigurationSection {
[ConfigurationProperty("emailSender", IsRequired = true, IsKey = false)]
public string EmailSender {
get { return (string)this["name"]; }
set { this["name"] = value; }
}
[ConfigurationProperty("emailRecipients", IsDefaultCollection = true)]
public EmailRecipientCollection EmailRecipients {
get {
var emailRecipientCollection =
(EmailRecipientCollection) base["emailRecipients"];
return emailRecipientCollection;
}
}
}
public class EmailRecipientCollection : ConfigurationElementCollection {
public EmailRecipientElement this[int index] {
get { return (EmailRecipientElement) BaseGet(index); }
set {
if (BaseGet(index) != null) {
BaseRemoveAt(index);
}
BaseAdd(index, value);
}
}
public new EmailRecipientElement this[string name] {
get { return (EmailRecipientElement) BaseGet(name); }
}
protected override ConfigurationElement CreateNewElement() {
return new EmailRecipientElement();
}
protected override object GetElementKey(ConfigurationElement element) {
return ((EmailRecipientElement) element).Name;
}
}
public class EmailRecipientElement : ConfigurationElement {
[ConfigurationProperty("name", IsRequired = true, IsKey = true)]
public string Name {
get { return (string) this["name"]; }
set { this["name"] = value; }
}
[ConfigurationProperty("emailAddress", IsRequired = true)]
public string EmailAddress {
get { return (string) this["emailAddress"]; }
set { this["emailAddress"] = value; }
}
}
}
Then in your web.config have something like this:
<configSections>
[...]
<section name="EmailSystem" type="EmailSystem, AssmeblyName" />
</configSections>
<EmailSystem emailSender="fromAddress#somedomain.com">
<emailRecipients>
<clear />
<add name="Support" emailAddress="support#somedomain.com" />
<add name="Sales" emailAddress="sales#somedomain.com" />
</emailRecipients>
</EmailSystem>
Then you can call into this:
emailRecipient = Request["recipient"];
var emailSystem = ConfigurationManager.GetSection("EmailSystem")
as EmailRecipientsSection;
string recipientEmail = emailSystem.EmailRecipients[emailRecipient].emailAddress;
// send email to recipientEmail.
You can do a couple things, though honestly I think this is easiest:
<appSettings>
<add key="testValues" value="someone#abc.com, someoneElse#abc.com, yetAnotherSomeone#abc.com" />
</appSettings>
Then you can get your object via:
String[] temp =
ConfigurationManager.AppSettings.GetValues("testValues").ToString().Split(',');
and then do a simple foreach statement to retrieve. You could even set this as a static object to be cached so it's a quicker retrieve. :)
Hope this helps,
JP
EDIT: An alternative scenario involves:
<appSettings file="test.config">
<!-- other settings to default to if test.config doesn't exist -->
</appSettings>
In this case, if you have a test.config file existing in your test environment, the AppSettings.GetValues() call will be made against that file. If the test.config file does not exist, the ConfigurationManager class will use the values within the appSettings node in your web.config file.