[英]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.