我有服務嚴重註冊泛型類型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:與類型 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder'「中發現的構造的無MyProj.Services.DataServices.Circle.CircleProfileService '可以用可用的服務和參數調用: 無法解析參數'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])''。
你能列出異常消息嗎? –
@MikeCorcoran,完成 – demo
該錯誤似乎相當清楚。 autofac可以找不到實現'ICircleParser'的類。你已經註冊了開放的通用'ICircleParser',但你仍然需要一些實現你要求autofac爲你解決的結果類型。 –