没有修改闭包的ASP.NET Core MvcJsonOptions依赖项注入? [英] ASP.NET Core MvcJsonOptions dependency injection without modified closure?

查看:93
本文介绍了没有修改闭包的ASP.NET Core MvcJsonOptions依赖项注入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(这个问题类似于没有修改闭包的ASP.NET Core MvcOptions依赖项注入吗?但是,我很难申请这个问题的解决方案。)

(This question is similar to ASP.NET Core MvcOptions dependency injection without modified closure? However, I struggle to apply the solution of that question to this one.)

在我的ASP.NET Core 1.1.3项目中,我当前注入了一个 ITraceWriter ConfigureServices 方法中的 MvcJsonOptions 的依赖方式如下:

In my ASP.NET Core 1.1.3 project, I currently inject an ITraceWriter dependency into MvcJsonOptions in the ConfigureServices method in the following way:

public override IServiceProvider ConfigureServices(IServiceCollection services)
{
    services.AddProjectSpecificStuff();

    ITraceWriter traceWriter = null;

    services.AddMvc().AddJsonOptions(options =>
    {
        options.SerializerSettings.TraceWriter = traceWriter;
    });

    var provider = base.ConfigureServices(services);

    traceWriter = provider.GetService<ITraceWriter>();

    return provider;
}

此方法有效,但导致诸如ReSharper之类的代码分析器抱怨对已修改内容的访问

This works, but causes code analyzers such as ReSharper to complain about access to modified closure.

是否有另一种方法可以在不使用修改的闭包的情况下实现相同的依赖注入?

Is there an alternative to achieve the same dependency injection without using modified closure?

推荐答案

您可能可以将分配推迟到。配置方法,它应该有效。

You can likely postpone the assignment to .Configure method, it should work.

public override IServiceProvider ConfigureServices(IServiceCollection services)
{
    services.AddProjectSpecificStuff();

    ITraceWriter traceWriter = null;

    services.AddMvc().AddJsonOptions(options =>
    {
        ...
        // other options here
    });

    return base.ConfigureServices(services);
}

public void Configure(IApplicationBuilder app, ITraceWriter traceWriter, IOptions<MvcJsonOptions> jsonOptions)
{
    services.AddMvc().AddJsonOptions(options =>
    {
        options.SerializerSettings.TraceWriter = provider.GetService<ITraceWriter>();
    });
}

只要您的依赖项(例如 IOptions< MvcJsonOptions> ITraceWriter 是单例。

This will work for as long as your dependencies (like IOptions<MvcJsonOptions> and ITraceWriter are singletons.

但是,此行为将来可能会更改,因为某些计划/想法是有关ASP.NET Core GitHub问题的,因此ASP.NET Core可能会对此进行更改以隐式创建范围内的上下文,该上下文在 .Configure 期间使用,它可能会也可能不会破坏上面的代码(如果有的话)。

However, this behavior may change in future, as some plans/ideas were on the ASP.NET Core GitHub issues, that the ASP.NET Core may change this to create an implicitly scoped context which is used during .Configure, it may or may not break the above code (if it ever comes).

但是您可能要考虑直接实例化 ITraceWriter 并将其作为实例传递给IoC容器,例如

But you may want consider to directly instantiate the ITraceWriter and pass it as instance to the IoC container, such as

ITraceWriter traceWriter = new TraceWriter(/* other dependencies */);
services.AddSingleton<ITraceWriter>(traceWriter);

只要是单例,也可以在合成根中进行组合。

It's okay to compose it in the composition root, as long as it's a singleton.

或者您也可以选择s粗糙且有点难看的解决方案,并通过工厂方法实例化 MvcJsonOptions

Or alternatively you choose this brute and somewhat ugly solution and instantiate MvcJsonOptions via factory method:

services.AddSingleton<IOptions<MvcJsonOptions>>(provider => Options.Create(new MvcJsonOptions
{
    SerializerSettings.TraceWriter = provider.GetService<ITraceWriter>()
}));

然后它很丑陋(尽管您可以将其隐藏在扩展方法后面),并且可以覆盖默认设置通过mddleware / extension方法。

Then it's pretty ugly (though you could hide that behind an extension method) and you may override defaults set by the mddleware/extension method.

这篇关于没有修改闭包的ASP.NET Core MvcJsonOptions依赖项注入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆