【问题标题】:Execute code with Application Pool load使用应用程序池加载执行代码
【发布时间】:2015-04-14 09:52:52
【问题描述】:

我的网站一直是一个单独的 vb.net 项目,里面有大量的页面和类(我不喜欢它)。

现在我需要使用外部 C# 应用程序中的一些类。所以我用所有遗留代码制作了一个新的 vb.net dll,并在ASP.NET 网站和C# 应用程序中使用它。

遗憾的是,我的一些类从web.config 读取配置(其中两个是static)。如果它们在外部 dll 文件中,它们无法访问web.config 文件,所以我想删除对它的任何引用并在应用程序池加载时配置它们,在任何页面加载之前强>。

这可能吗?我正在搜索但没有成功!

谢谢!

【问题讨论】:

  • 你能澄清一下你的解决方案结构和你的web.config阅读代码吗?在链接的 dll 中不应阻止您阅读网络或应用程序配置
  • 好吧,事实证明我的问题很愚蠢,即使从我的 dll 将System.configuration 导入到我的旧 dll 中,我也可以使用 ConfigurationManager.AppSettings()!谢谢!

标签: c# asp.net vb.net iis application-pool


【解决方案1】:

这是一个依赖注入模式的示例

using System;

namespace ConsoleApplication1
{
    class Program
    {
        public class NewConfigClass
        {
            public string MyConfigParameter {get;set;}
        }

        public class LegacyNeedsConfig
        {
            public string ConfigurableSetting {get;set;}
            public LegacyNeedsConfig(NewConfigClass config)
            {
                this.ConfigurableSetting = config.MyConfigParameter;
            }
        }

        static void Main(string[] args)
        {
            NewConfigClass config = new NewConfigClass();
            config.MyConfigParameter = "hello world"; //read from web or app config

            LegacyNeedsConfig legacy = new LegacyNeedsConfig(config);

            Console.WriteLine(legacy.ConfigurableSetting);
        }

    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-12
    • 1970-01-01
    • 2012-03-13
    • 2020-06-03
    • 1970-01-01
    • 2013-03-05
    • 2011-06-17
    相关资源
    最近更新 更多