Nancy DependencyResolver

时间:2016-11-18 01:06:33

标签: c# autofac nancy

我正在使用Nancy Framework和autofac,我在Bootstrapper中确实喜欢这个:

var services = Assembly.Load("ProjectDomain");
builder.RegisterAssemblyTypes(services)
    .AsClosedTypesOf(typeof(ICommandHandler<>))
    .InstancePerRequest();
builder.RegisterAssemblyTypes(services)
    .AsClosedTypesOf(typeof(IValidationHandler<>))
    .InstancePerRequest();

但它不起作用.....

ICommandHandler<TCommand> handler =
    DependencyResolver.Current.GetService<ICommandHandler<TCommand>>();

处理程序为空!如何解决?帮助我,谢谢...

1 个答案:

答案 0 :(得分:0)

我相信你正在注册类型AsSelf(这是默认的autofac行为)。你能试试吗?

builder.RegisterAssemblyTypes(services)
    .AsClosedTypesOf(typeof(ICommandHandler<>))
    .AsImplementedInterface()
    .InstancePerRequest();