Asp.net core 2.0 注入IEnumerable<;IInputFormatters>;

Asp.net core 2.0 注入IEnumerable<;IInputFormatters>;,asp.net-core-2.0,Asp.net Core 2.0,我很难将IEnumerable注入到其他服务中。 我已经注册了自己的InputFromatter,还添加了JsonFormatters。所以,至少应该有3个输入格式化程序,但是当我尝试注入IEnumerable时,我总是得到null(就像根本没有格式化程序一样)。 我的注册情况如下: services.AddMvcCore(config => { config.InputFormatters.Insert(0, new UserCon

我很难将
IEnumerable
注入到其他服务中。 我已经注册了自己的
InputFromatter
,还添加了
JsonFormatters
。所以,至少应该有3个输入格式化程序,但是当我尝试注入
IEnumerable
时,我总是得到
null
(就像根本没有格式化程序一样)。 我的注册情况如下:

services.AddMvcCore(config =>
            {
                config.InputFormatters.Insert(0, new UserContextFormatter());
                config.ModelBinderProviders.Insert(0, new ModelBinderProvider());
            })
                .AddAuthorization()
                .AddFluentValidation(fv => fv.RegisterValidatorsFromAssemblyContaining<Startup>())
                .AddJsonOptions(opt =>
                {
                    opt.SerializerSettings.Formatting = Formatting.Indented;
                    opt.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                    opt.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
                    opt.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
                })
                .AddJsonFormatters()
                .AddApiExplorer();
services.AddMvcCore(配置=>
{
Insert(0,新的UserContextFormatter());
插入(0,新的ModelBinderProvider());
})
.AddAuthorization()
.AddFluentValidation(fv=>fv.RegisterValidatorsFromAssemblyContaining())
.AddJsonOptions(opt=>
{
opt.SerializerSettings.Formatting=格式化.缩进;
opt.SerializerSettings.ReferenceLoopHandling=ReferenceLoopHandling.Ignore;
opt.SerializerSettings.NullValueHandling=NullValueHandling.Ignore;
opt.SerializerSettings.ContractResolver=新的CamelCasePropertyNamesContractResolver();
})
.AddJsonFormatters()文件
.AddApiExplorer();
看起来很简单很愚蠢,但我还不够好去做。有什么想法吗? 谢谢

对于
IEnumerable
,它未注册为服务,因此您无法解析它或从依赖项注入访问它

对于
输入格式化程序
ModelBinderProviders
,它们被附加到
操作设置操作
,因此您可以从
IOOptions
访问它们

请尝试以下代码:

    public class HomeController : ControllerBase
{
    private readonly MvcOptions _options;
    public HomeController(IOptions<MvcOptions> options)
    {
        _options = options.Value;
        var inputFormatters = _options.InputFormatters;
        var outputFormatters = _options.OutputFormatters;
        var modelBinderProviders = _options.ModelBinderProviders;
    }
公共类HomeController:ControllerBase
{
私有只读MVCOPIONS\u选项;
公共家庭控制器(IOPS选项)
{
_选项=选项.值;
var inputFormatters=\u options.inputFormatters;
var outputFormatters=\u options.outputFormatters;
var modelBinderProviders=\u options.modelBinderProviders;
}