【问题标题】:Use custom names for IHostingEnvironment为 IHostingEnvironment 使用自定义名称
【发布时间】:2016-11-21 13:09:19
【问题描述】:

我们正在运行 ASP.NET Core 并热切地使用 IHostingEnvironment。

由于我们组织中的约定,我们的环境名称与默认 IHostingEnvironment 实现的名称不匹配。

为了解决这个问题,我们制作了以下扩展方法:

public static class HostingEnvironmentExtensions
{
        public static bool IsProd(this IHostingEnvironment env) => env.IsEnvironment("prod");            

        public static bool IsTest(this IHostingEnvironment env) => env.IsEnvironment("test");                 
}

这很好用,但感觉有点危险,因为 IsProduction 和 IsStaging 的默认方法现在无法按您预期的方式工作。

处理与 ASP.NET Core 默认值不同的环境名称的正确方法是什么?

【问题讨论】:

    标签: c# asp.net-core asp.net-core-mvc


    【解决方案1】:

    您可以通过创建自己的实现来解决这个问题。我创建了自己的IWorkingEnvironment 界面,如下所示:

    public interface IWorkingEnvironment
    {
        string EnvironmentName { get; set; }
    }
    

    还有我自己的“已知”环境名称:

    public static class EnvironmentNames
    {
        public static readonly string Local = nameof(Local);
    
        public static readonly string Dev = nameof(Dev);
    
        public static readonly string Test = nameof(Test);
    
        public static readonly string Prod = nameof(Prod);
    }
    

    接着是IWorkingEnvironment上的扩展方法:

    public static class WorkingEnvironmentExtensions
    {
        public static bool IsLocal(this IWorkingEnvironment environment)
        {    
            return environment.EnvironmentName == EnvironmentNames.Local;
        }
    
        public static bool IsDev(this IWorkingEnvironment environment)
        {    
            return environment.EnvironmentName == EnvironmentNames.Dev;
        }
    
        // etc...
    }
    

    然后我使用 ASP.NET 实现IWorkingEnvironment IHostingEnvironment:

    public class AspNetWorkingEnvironment : IWorkingEnvironment
    {
        private readonly IHostingEnvironment _hostingEnvironment;
    
        public AspNetWorkingEnvironment(IHostingEnvironment hostingEnvironment)
        {
            _hostingEnvironment = hostingEnvironment;
        }
    
        public string EnvironmentName => _hostingEnvironment.EnvironmentName;
    }
    

    现在我们要做的就是在我们的依赖注入容器中注册AspNetWorkingEnvironment 作为IWorkingEnvironment 的实现(使用单例生命周期):

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddSingleton<IWorkingEnvironment, AspNetWorkingEnvironment>();
    
        // etc...
    }
    

    【讨论】:

    • 谢谢。我真的很希望能够使用 IHostingEnvironment,因为这意味着新开发人员不需要知道要使用哪个接口,但我认为这是当前(缺乏)HostingEnvironment 可配置性的最佳方法跨度>
    【解决方案2】:

    最简洁的方法是扩展您自己的实现IHostingEnvironment 的类型并将其注入您的控制器和服务中。

    但是,如果您坚持依赖内置方法,您可以(有点危险地)覆盖 EnvironmentName 类型中的 string 值以匹配您的自定义值:

    public Startup(IHostingEnvironment env)
    {
        var envNameType = typeof(EnvironmentName);
    
        envNameType.GetField(EnvironmentName.Development).SetValue(null, "dev");
        envNameType.GetField(EnvironmentName.Production).SetValue(null, "prod");
        envNameType.GetField(EnvironmentName.Staging).SetValue(null, "stag");
    
        // given that current environment name == "prod", both would evaluates to 'true'
        var isInProd = env.IsEnvironment("prod");
        var isProd = env.IsProduction();
    
        // ...
    }
    

    在内部,内置的 IsEnvironment() 方法依赖于来自 EnvironmentName 的值来确定环境名称。

    见Source

    【讨论】:

    • 感谢您的回复。就像你自己说的,这是一种疯狂的科学家方法,基本上没有“正确的方法”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-23
    • 2020-03-09
    • 2020-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多