C# 使用Autofac注册的泛型类型错误

C# 使用Autofac注册的泛型类型错误,c#,generics,dependency-injection,autofac,C#,Generics,Dependency Injection,Autofac,我有服务 public class CircleProfileService : CircleService<ICircleProfileInput, ICircleProfileOutput, ICircleProfile>, ICircleProfileService { public CircleProfileService(ICircleProfileQueryBuilder queryBuilder,

我有服务

public class CircleProfileService : CircleService<ICircleProfileInput, ICircleProfileOutput, ICircleProfile>, ICircleProfileService
{
        public CircleProfileService(ICircleProfileQueryBuilder queryBuilder,
                                             ICircleQueryProcessor queryProcessor,
                                             ICircleParser<ICircleProfile> parser)
            : base(queryBuilder, queryProcessor, parser)
        {
        }
        ... other methods
}
公共类CircleProfileService:CircleService、ICIRceProfileService
{
公共环路规划服务(ICIRCEPROFILEQUERYBUILDER queryBuilder,
ICIRRequestyProcessor查询处理器,
ICIRCLarser(语法分析器)
:base(queryBuilder、queryProcessor、parser)
{
}
…其他方法
}
正如您所看到的,我尝试注入解析器的泛型类型

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);
        }
    }
公共接口ICIRCLEARSER其中TOutput:ICIRCLEARSEDOUTPUT
{
IEnumerable解析(字符串json);
}
公共类CircleParser:ICerclParser where TOutput:ICerclParsersDoutput,new()
//new()是使项目可构建的唯一选项,因为jsonParser需要非抽象类型。。。
{
私有只读IJsonParser-jsonParser;
公共电路解析器(IJsonParser-jsonParser)
{
this.jsonParser=jsonParser;
}
公共IEnumerable解析(字符串json)
{
返回jsonParser.Parse(json);
}
}
下面是我如何注册我的泛型类型(就像在Autofac文档中一样)

builder.RegisterGeneric(typeof(CircleParser)).As(typeof(ICircleParser));
但这总是引发无法注入解析器的异常

我做错了什么


错误:

Autofac.Core.DependencyResolutionException:在类型“MyProj.Services.DataServices.Circle.CircleProfileService”上找到的具有“Autofac.Core.Activators.Reflection.DefaultConstructorFinder”的构造函数中,没有一个可以使用可用服务和参数调用: 无法解析构造函数Void.ctor的参数“MyProj.Services.DataServices.Circle.Parsers.Interfaces.ICircleParser
1[MyProj.Services.DataServices.Circle.Outputs.Interfaces.ICircleProfile]parser”(MyProj.Services.DataServices.Circle.QueryBuilders.Interfaces.ICircleProfileQueryBuilder,MyProj.Services.DataServices.Circle.QueryProcessors.Interfaces.ICIRCLeRequestProcessor,MyProj.Services.DataServices.Parsers.Interfaces.ICircleParser
1[MyProj.Services.DataServices.Circle.Outputs.Interfaces.ICircleProfile])


无法向rep发表评论,因此我不得不发表帖子


您是否有
IJsonParser
的绑定?如果没有,您需要添加一个绑定,以便autofac可以将它的实现注入
CircleParser

您可以列出问题中的异常消息吗?@MikeCorcoran,Done错误似乎相当清楚。autofac找不到实现
ICIRParser的类的实现
。您已经注册了打开的泛型
ICIRCLEASER
,但是您仍然需要一些实现您请求autofac为您解析的结果类型的东西。@MikeCorcoran,如果我理解正确,我应该添加一些空的
类CircleProfileParser:CircleParser,ICIRCLEASER
?问题是您将
ICircleProfile
作为
ICircleParser解析器
依赖项中的泛型参数。Autofac不知道在
ICircleProfile
中适合什么。您可以选择:1.注册直接从
ICircleParser
解析的内容,例如:
container.Register.as()
或2.在
CircleProfileService
依赖项构造函数中使用具体类型,如
PublicCircleProfileService(…,ICircleParser解析器)
 builder.RegisterGeneric(typeof(CircleParser<>)).As(typeof(ICircleParser<>));