ここから多量のドキュメントと質問を読んだ後、私はまだ次のことに取り組んでいます。これらは、インターフェイス/ Autofacに登録され実装されている:登録ビルダーはContainerBuilderインスタンスである場合、以下の通りである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>
{
}
それは中央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コントローラの
ご協力いただければ幸いです!
をしてください。タイプミスがない場合は、例外の詳細を投稿します。あなたのコードを試してみましたが、それはAutofac 2.5.2.830、.NET 4 –
でうまくいきました。それが重要な場合、これはすべてオーチャードCMSモジュール内で起こります。これは私が知っている構成がないことを意味します。 – Piedone
例外の詳細を追加しました。御時間ありがとうございます!それが問題なら、私が使っているAutofacのバージョンは2.2.4.900です。 – Piedone