簡體   English   中英

在Autofac中注冊的通用類型錯誤

[英]Badly registered generic type with Autofac

我有服務

public class CircleProfileService : CircleService<ICircleProfileInput, ICircleProfileOutput, ICircleProfile>, ICircleProfileService
{
        public CircleProfileService(ICircleProfileQueryBuilder queryBuilder,
                                             ICircleQueryProcessor queryProcessor,
                                             ICircleParser<ICircleProfile> parser)
            : base(queryBuilder, queryProcessor, parser)
        {
        }
        ... other methods
}

如您所見,我嘗試注入解析器的通用類型。

public interface ICircleParser<out TOutput> where TOutput : ICircleParsedOutput
{
        IEnumerable<TOutput> Parse(string json);
}

public class CircleParser<TOutput> : ICircleParser<TOutput> where TOutput: ICircleParsedOutput, new() 
//new() is only one option to make project buildable as jsonParser want non-abstract type...
    {
        private readonly IJsonParser jsonParser;

        public CircleParser(IJsonParser jsonParser)
        {
            this.jsonParser = jsonParser;
        }

        public IEnumerable<TOutput> Parse(string json)
        {
            return jsonParser.Parse<TOutput>(json);
        }
    }

這是我注冊我的通用類型的方式(類似於Autofac文檔中的所有內容)

 builder.RegisterGeneric(typeof(CircleParser<>)).As(typeof(ICircleParser<>));

但這總是拋出無法注入解析器的異常...

我究竟做錯了什么?


錯誤:

Autofac.Core.DependencyResolutionException:在類型為“ MyProj.Services.DataServices.Circle.CircleProfileService”的“ Autofac.Core.Activators.Reflection.DefaultConstructorFinder”中找不到任何構造函數,無法使用可用的服務和參數調用:無法解析參數“ MyProj.Services.DataServices.Circle.Parsers.Interfaces.ICircleParser 1[MyProj.Services.DataServices.Circle.Outputs.Interfaces.ICircleProfile] parser' of constructor 'Void .ctor(MyProj.Services.DataServices.Circle.QueryBuilders.Interfaces.ICircleProfileQueryBuilder, MyProj.Services.DataServices.Circle.QueryProcessors.Interfaces.ICircleQueryProcessor, MyProj.Services.DataServices.Circle.Parsers.Interfaces.ICircleParser 1 [MyProj.Services.DataServices.Circle.Outputs.Interfaces.ICircleProfile])。

無法對代表發表評論,所以我被迫發布了一個帖子。

您是否具有IJsonParser的綁定? 如果不是,則需要添加一個,以便autofac可以將其實現注入CircleParser

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM