使用interface作为Autofac中的类型参数来解析泛型

时间:2011-12-27 17:31:04

标签: c# generics autofac

在阅读了大量的文档和问题后,我仍然坚持以下内容。这些是在Autofac中注册的接口/实现:

public interface ITestService<T>
{
}

public class TestService<T> : ITestService<T>
{
}

public interface ITest<TService, T>
    where TService : ITestService<T>
{
}

public class Test<TService, T> : ITest<TService, T>
    where TService : ITestService<T>
{
}

注册如下,其中builder是一个ContainerBuilder实例,它更新了一个中央IComponentRegistry:

builder.RegisterGeneric(typeof(TestService<>)).As(typeof(ITestService<>)).InstancePerLifetimeScope();

builder.RegisterGeneric(typeof(Test<,>)).As(typeof(ITest<,>)).InstancePerLifetimeScope();

现在可行(其中_componentContext是IComponentContext实例):

_componentContext.Resolve<ITest<TestService<MyType>, MyType>>();

这不(抛出ComponentNotRegisteredException):

_componentContext.Resolve<ITest<ITestService<TNodeToNodeConnectorRecord>, TNodeToNodeConnectorRecord>>();

如果不了解ITestService的实施,有关如何解决的任何提示都可以使用?由于

_componentContext.Resolve<ITestService<MyType>>();

按预期工作,使用它的类型可能会以某种方式使用但我还没有成功。

更新,异常详情 引发的异常如下:

"The requested service 'MyProject.ITest`2[[MyProject.ITestService`1[[MyProject.MyType, MyProject, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]], MyProject, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null],[MyProject.MyType, MyProject, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]]' has not been registered."

堆栈追踪:

   at Autofac.ResolutionExtensions.Resolve(IComponentContext context, Service service, IEnumerable`1 parameters)
   at Autofac.ResolutionExtensions.Resolve(IComponentContext context, Type serviceType, IEnumerable`1 parameters)
   at Autofac.ResolutionExtensions.Resolve[TService](IComponentContext context, IEnumerable`1 parameters)
   at Autofac.ResolutionExtensions.Resolve[TService](IComponentContext context)
   at MyProject.SomeController`4.Execute(RequestContext requestContext) in d:\SomeController.cs:line 55
   at System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext)
   at System.Web.Mvc.MvcHandler.<>c__DisplayClass6.<>c__DisplayClassb.<BeginProcessRequest>b__5()
   at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass1.<MakeVoidDelegate>b__0()
   at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
   at System.Web.Mvc.MvcHandler.<>c__DisplayClasse.<EndProcessRequest>b__d()
   at System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f)
   at System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action)
   at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
   at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)
   at Orchard.Mvc.Routes.ShellRoute.HttpAsyncHandler.EndProcessRequest(IAsyncResult result) in D:\MyProject.SomeRoutes.cs:line 148
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

执行resolve调用的代码实际上是在ASP.NET MVC控制器的Execute()方法中。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:2)

不幸的是,您的Autofac版本有一个错误:Generic parameters constrained as generic interfaces fail to resolve.

它已被修复,据说在2.5.1中。您必须升级才能使用该功能。

或者您可以尝试解决方法:

public interface ITestService<T>
{
}

public class TestService<T> : ITestService<T>
{
}

public interface ITest<T>
{
  ITestService<T> TestService { get; }
}

public class Test<T> : ITest<T>
{
  readonly ITestService<T> _TestService;

  public Test(ITestService<T> testService)
  {
    _TestService = testService;
  }

  public ITestService<T>
  {
    get
    {
      return this._TestService;
    }
  }
}

builder.RegisterGeneric(typeof(TestService<>)).As(typeof(ITestService<>)).InstancePerLifetimeScope();
builder.RegisterGeneric(typeof(Test<>)).As(typeof(ITest<>)).InstancePerLifetimeScope();
...

_componentContext.Resolve<ITest<TNodeToNodeConnectorRecord>>();

// if you need to specify the ITestService type to use:
var testService = _componentContext.Resolve<TestService<TNodeToNodeConnectorRecord>>();
var test = _componentContext.Resolve<Func<ITestService<TNodeToNodeConnectorRecord>, ITest<TNodeToNodeConnectorRecord>>>()(testService);