阅读大量文档和问题后,我仍然坚持以下几点。这些都是与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)
执行的决心调用的代码实际上是在execute()方法的ASP.NET MVC控制器。
任何帮助将不胜感激!
请确保你没有一个错字在你的configs。如果没有错别字,请发布例外详情。我已经试过你的代码,它可以很好的与Autofac 2.5.2.830,.NET 4 – 2011-12-27 17:45:01
谢谢,这很奇怪。如果这很重要,这一切都发生在一个果园CMS模块内。这意味着我没有意识到配置。 – Piedone 2011-12-27 17:58:06
我已添加例外详细信息。谢谢你的时间!如果它很重要,我使用的Autofac版本是2.2.4.900。 – Piedone 2011-12-27 18:08:23