C# 在web.config中设置IHttpModule.properties

C# 在web.config中设置IHttpModule.properties,c#,properties,web-config,httpmodule,ihttpmodule,C#,Properties,Web Config,Httpmodule,Ihttpmodule,我创建了自己的HttpModule,名为“JsonLogger”,用于记录json请求和响应 对于日志记录,我需要一个connectionstring 为了找到connectionstring,我在web.config中添加了一个,并通过代码获得它 web.config <connectionStrings> <add name="WebShopConfiguration" connectionString="myConString"/> </connecti

我创建了自己的HttpModule,名为“JsonLogger”,用于记录json请求和响应

对于日志记录,我需要一个connectionstring

为了找到connectionstring,我在web.config中添加了一个,并通过代码获得它

web.config

<connectionStrings>
   <add name="WebShopConfiguration" connectionString="myConString"/>
</connectionStrings>

<system.web>
   <httpModules>
     <add name="JsonLogger" type="ServiceModel.Logging.JsonLogger, ServiceModel"/>
    </httpModules>
</system.web>

<system.webServer>
   <modules>
      <add name="JsonLogger" type="ServiceModel.Logging.JsonLogger, ServiceModel"/>
   </modules>
</system.webServer>
<add name="JsonLogger" type="ServiceModel.Logging.JsonLogger, ServiceModel" ConnectionString="myConString"/>
<configSections>    
    <section name="LoggingConfiguration" type="WebServices.Services.Configurations.LoggingConfiguration"/>
</configSections>

<LoggingConfiguration
connectionString="myConString">
</LoggingConfiguration>
<connectionStrings>
   <add name="WebShopConfiguration" connectionString="myConString"/>
</connectionStrings>

<system.web>
   <httpModules>
     <add name="JsonLogger" type="ServiceModel.Logging.JsonLogger, ServiceModel"/>
    </httpModules>
</system.web>

<system.webServer>
   <modules>
      <add name="JsonLogger" type="ServiceModel.Logging.JsonLogger, ServiceModel"/>
   </modules>
</system.webServer>
一切正常,但在我看来,这并不干净(未来的开发人员不知道为什么我的web.config中会有连接字符串)

我想在web.config中添加httpmodule的地方填写connectionstring。我试过这个,但不起作用(就像我已经想的那样):

web.config

<connectionStrings>
   <add name="WebShopConfiguration" connectionString="myConString"/>
</connectionStrings>

<system.web>
   <httpModules>
     <add name="JsonLogger" type="ServiceModel.Logging.JsonLogger, ServiceModel"/>
    </httpModules>
</system.web>

<system.webServer>
   <modules>
      <add name="JsonLogger" type="ServiceModel.Logging.JsonLogger, ServiceModel"/>
   </modules>
</system.webServer>
<add name="JsonLogger" type="ServiceModel.Logging.JsonLogger, ServiceModel" ConnectionString="myConString"/>
<configSections>    
    <section name="LoggingConfiguration" type="WebServices.Services.Configurations.LoggingConfiguration"/>
</configSections>

<LoggingConfiguration
connectionString="myConString">
</LoggingConfiguration>
<connectionStrings>
   <add name="WebShopConfiguration" connectionString="myConString"/>
</connectionStrings>

<system.web>
   <httpModules>
     <add name="JsonLogger" type="ServiceModel.Logging.JsonLogger, ServiceModel"/>
    </httpModules>
</system.web>

<system.webServer>
   <modules>
      <add name="JsonLogger" type="ServiceModel.Logging.JsonLogger, ServiceModel"/>
   </modules>
</system.webServer>
经过一些研究,我仍然没有找到一种方法来做到这一点。有人知道我想实现的目标是否可能吗

非常感谢,, 基尔

我成功了

在我的web.config中,我添加了一个类型为LoggingConfiguration的configSection。 在global.asax init()中,我获取配置并使用正确的参数创建一个新的JsonLogger实例

web.config

<connectionStrings>
   <add name="WebShopConfiguration" connectionString="myConString"/>
</connectionStrings>

<system.web>
   <httpModules>
     <add name="JsonLogger" type="ServiceModel.Logging.JsonLogger, ServiceModel"/>
    </httpModules>
</system.web>

<system.webServer>
   <modules>
      <add name="JsonLogger" type="ServiceModel.Logging.JsonLogger, ServiceModel"/>
   </modules>
</system.webServer>
<add name="JsonLogger" type="ServiceModel.Logging.JsonLogger, ServiceModel" ConnectionString="myConString"/>
<configSections>    
    <section name="LoggingConfiguration" type="WebServices.Services.Configurations.LoggingConfiguration"/>
</configSections>

<LoggingConfiguration
connectionString="myConString">
</LoggingConfiguration>
<connectionStrings>
   <add name="WebShopConfiguration" connectionString="myConString"/>
</connectionStrings>

<system.web>
   <httpModules>
     <add name="JsonLogger" type="ServiceModel.Logging.JsonLogger, ServiceModel"/>
    </httpModules>
</system.web>

<system.webServer>
   <modules>
      <add name="JsonLogger" type="ServiceModel.Logging.JsonLogger, ServiceModel"/>
   </modules>
</system.webServer>
Global.asax.cs

public sealed class LoggingConfiguration : ConfigurationSection
{
    private const string ConnectionStringKey = "connectionString";

    [ConfigurationProperty(ConnectionStringKey, IsRequired = true)]
    public string ConnectionString
    {
        get { return (string) base[ConnectionStringKey]; }
        set { base[ConnectionStringKey] = value; }
    }        
}
public override void Init()
{
    base.Init();
    var loggingConfiguration = ConfigurationManager.GetSection("LoggingConfiguration") as LoggingConfiguration;
    if (loggingConfiguration != null)
    {               
        new JsonLogger(loggingConfiguration.ConnectionString).Init(this); 
    }
}
这样,我可以删除web.config中的以下代码: