简体   繁体   English

如何在ASP.NET MVC 6中为不同的环境注册不同的服务?

[英]How to register different services for different environments in ASP.NET MVC 6?

What is the right way to configure services for different environments? 为不同环境配置服务的正确方法是什么?

For example I want to add FakeService to services collection for DEV configuration and RealService for Release configuration. 例如,我想将FakeService添加到DEV配置的服务集合和发布配置的RealService。

public void ConfigureServices(IServiceCollection services)
{
    /* Need to check environment */
    services.AddSingleton<IService, FakeService>();
    ....
}

MVC 6 has a value that defines what environment it is, this can be set by the environment variable ASPNET_ENV. MVC 6有一个值来定义它是什么环境,这可以由环境变量ASPNET_ENV设置。 You can grab that value from IHostingEnvironment: 您可以从IHostingEnvironment获取该值:

public void ConfigureServices(IServiceCollection services)
{
    var env = services.BuildServiceProvider().GetRequiredService<IHostingEnvironment>();
    if (env.IsDevelopment())
        Console.WriteLine("Development");
    else if (env.IsProduction())
        Console.WriteLine("Production");
    else if (env.IsEnvironment("MyCustomEnvironment"))
        Console.WriteLine("MyCustomEnvironment");
}

You can set the value in VS2015 on your project by Right-click > Properties > Debug > Environment Variables: 您可以通过右键单击>属性>调试>环境变量在项目中设置VS2015中的值:

环境变量

Here's some more info on configuring with environment variables . 以下是有关使用环境变量进行配置的更多信息。

It's just a matter of reading this from your configuration file, and making a decision in code accordingly: 只需从配置文件中读取此内容,然后相应地在代码中做出决定:

bool isDev = Boolean.Parse(ConfigurationManager.AppSettings["IsDev"]);

if (isDev) {
    services.AddSingleton<IService, FakeService>();
} else {
    services.AddSingleton<IService, RealService>();
}

Another option is to use compiler directives: 另一种选择是使用编译器指令:

if #DEBUG
    services.AddSingleton<IService, FakeService>();
#else
    services.AddSingleton<IService, RealService>();
#endif

While in debug configuration there is a DEBUG defined constant (automatically defined by Visual Studio) while there is no such constant defined for release mode. 在调试配置中,有一个DEBUG定义的常量(由Visual Studio自动定义),而没有为发布模式定义的此类常量。

if #DEBUG will work only for debug. if #DEBUG仅适用于调试。

Check out the conditional methods https://msdn.microsoft.com/en-us/library/aa288458(v=vs.71).aspx 查看条件方法https://msdn.microsoft.com/en-us/library/aa288458(v=vs.71).aspx

Use [Conditional("DEV")] or [Conditional("RELEASE")] before that method. 在该方法之前使用[Conditional("DEV")][Conditional("RELEASE")] It will execute when it matches mentioned configuration. 它将在匹配上述配置时执行。

I'd suggest adding a layer of indirection. 我建议添加一个间接层。 That is, use the IoC container to inject the appropriate version of the service: 也就是说,使用IoC容器注入适当版本的服务:

services.AddService(container.Resolve<IMyServiceType>);

... of course you'll need to make sure the container returns the appropriate type for each environment. ...当然,您需要确保容器为每个环境返回适当的类型。

I'm not sure you can do this with the default IoC container though; 我不确定你能用默认的IoC容器做到这一点; you might need to replace it with Ninject, Autofac, or whatever. 您可能需要将其替换为Ninject,Autofac或其他任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当控制器类在不同的程序集中时,如何将Controller注册到ASP.NET MVC中? - How to register a Controller into ASP.NET MVC when the controller class is in a different assembly? C#/ ASP.NET MVC:如果我知道用户名和密码,如何在其他Web应用程序中注册/登录? - C#/ASP.NET MVC: how to register/log in a different web application if I know username & password? 如何在另一个asp.net mvc项目中使用asp.net mvc项目功能 - How to use an asp.net mvc project capabilities in a different asp.net mvc project ASP.NET MVC-如何将参数传递给其他控制器上的操作 - ASP.NET MVC - how to pass parameters to actions on a different controller 如何在asp.net MVC中的2个不同项目中处理Service和ViewModels? - How to handle Service and ViewModels in 2 different projects in asp.net MVC? 如何从ASP.NET MVC中的其他操作返回视图 - How to return a view from a different action in ASP.NET MVC 如何使用不同的页面引擎创建ASP.NET MVC 4项目? - how to create ASP.NET MVC 4 project with different page engine? ASP.NET MVC返回不同的视图 - ASP.NET MVC return a different view ASP.NET MVC 5验证-属性不同 - ASP.NET MVC 5 Validation - Property are different 在Asp.net中的autofac容器中注册不同的程序集类型 - Register different assembly types in autofac container in Asp.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM