【问题标题】:Get Application Directory from within Configure method in Startup.Cs从 Startup.Cs 的 Configure 方法中获取应用程序目录
【发布时间】:2021-08-10 02:46:57
【问题描述】:

您不能在 EF7 上将相对连接字符串与 SQLite 一起使用,因此我需要一种在配置 DBContext 的 ConfigureServices 例程期间从 Startup.cs 中获取应用程序目录的方法。

知道如何使用 .NetCoreApp 库执行此操作吗?

    public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddMvc();

        //Figure out app directory here and replace token in connection string with app directory.......    

        var connectionString = Configuration["SqliteConnectionString"];

        if (string.IsNullOrEmpty(connectionString))
            throw new Exception("appSettings.json is missing the SqliteConnectionString entry.");
        services.AddDbContext<MyContext>(options =>
        {
            options.UseSqlite(connectionString, b => b.MigrationsAssembly("xyz.myproject.webapp"));

        });
    }

【问题讨论】:

    标签: c# asp.net-core


    【解决方案1】:

    您可以将环境存储在本地属性中,然后您可以访问它以获取基本路径,如下所示:

    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
    
    
        Configuration = builder.Build();
    
        environment = env;
    }
    
    public IHostingEnvironment environment { get; set; }
    public IConfigurationRoot Configuration { get; }
    
    public void ConfigureServices(IServiceCollection services)
    {
        // you can access environment.ContentRootPath here
    }
    

    【讨论】:

    • 效果很好,我更喜欢这种方法。我完全错过了构造函数中的 IHostingEnvironment。感谢您指出这一点。
    • 我不明白这是如何工作的,ConfigureServices() 在 Startup() 之前运行,所以从 ConfigureServices 访问时环境变量不会总是为空吗?
    • 不应该先调用Startup构造函数,startup不是静态类,在调用构造函数之前不能调用ConfigureServices来创建Startup的实例,因为它是实例方法而不是静态方法。它在 2.0 项目模板中确实发生了一些变化,现在 IConfiguration 在 Program 中创建并传递到 Startup 构造函数,尽管旧语法仍然有效。
    【解决方案2】:

    您可以使用以下方法获取应用程序基目录:

    AppContext.BaseDirectory;
    

    【讨论】:

      【解决方案3】:

      对于函数应用,从 IFunctionsHostBuilder 对象中获取上下文,然后访问 ApplicationRootPath 对我有用:

      public class ContainerConfig : FunctionsStartup
      {
          public override void Configure(IFunctionsHostBuilder builder)
          {
              if (builder == null)
              {
                  throw new ArgumentNullException(nameof(builder));
              }
      
              var context = builder.GetContext();
      
              ConfigureAppSettings(builder.Services, context);
      
              ...
          }
      
          public void ConfigureAppSettings(IServiceCollection services, FunctionsHostBuilderContext context)
          {
              ...
      
              var config = new ConfigurationBuilder()
                  .SetBasePath(context.ApplicationRootPath)
                  ...
          }
      }
      

      【讨论】:

        【解决方案4】:

        只需在需要获取路径的地方使用依赖注入即可。

        例子

        ValueController(..., IHostingEnvironment env)
        {
        Console.WriteLine(env.ContentRootPath);
        ...
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-10-11
          • 2015-02-18
          • 2015-01-23
          • 2011-07-28
          • 1970-01-01
          • 2017-05-06
          • 2023-03-10
          相关资源
          最近更新 更多