自动解析.Net Core框架并没有在DryIoC中找到我的注册。
这使用新的.Net Core 2框架,DryIoC 2.10.7和DryIoc.Microsoft.DependencyInjection 1.2.2。我无法更新到DryIoC 2.11.7,因为DryIoc.Microsoft.DependencyInjection没有获取最新版本。在2.11.7安装之后我确实尝试重新安装后者但是没有用(模糊引用)。
这是我的启动代码:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.AddControllersAsServices();
var container = new Container(rules => rules.With(propertiesAndFields: PropertiesAndFields.Auto))
.WithDependencyInjectionAdapter(services);
container.ConfigureServiceProvider<CompositionRoot>();
}
以下是我的注册码:
public CompositionRoot(IRegistrator registrator, IContainer container)
{
//System Clock
registrator.Register<IClock, ConcreteClock>(Reuse.Singleton, null, null, IfAlreadyRegistered.Keep);
container.RegisterInstance<ISystemConfiguration>(new Configuration(container.Resolve<IClock>()), Reuse.Singleton, IfAlreadyRegistered.Keep);
//Logging
container.RegisterInstance<ILogging>(CreateLogger(container.Resolve<ISystemConfiguration>()), Reuse.Singleton, IfAlreadyRegistered.Keep);
}
最后是控制器:
public class AdminController : Controller
{
private readonly ILogging _log;
private readonly IClock _clock;
public AdminController(ILogging log,
IClock clock)
{
_log = log;
_clock = clock;
}
}
以下是例外:
System.InvalidOperationException:无法解析类型的服务 &#39; SharedAssets.Interfaces.ILogging&#39;在尝试激活时 &#39; DataDictionaryService.Controllers.AdminController&#39 ;.在 Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(类型 serviceType,Type implementationType,ISet
1 callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(Type serviceType, Type implementationType, ISet
1 callSiteChain)at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor 描述符,类型serviceType,ISet1 callSiteChain) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(Type serviceType, ISet
1 callSiteChain)at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateCallSite(类型 serviceType,ISet1 callSiteChain) at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(Type serviceType, ServiceProvider serviceProvider) at System.Collections.Concurrent.ConcurrentDictionaryExtensions.GetOrAdd[TKey,TValue,TArg](ConcurrentDictionary
2 字典,TKey键,Func`3 valueFactory,TArg arg)at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(类型 serviceType)at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(的IServiceProvider provider,输入serviceType)at Microsoft.AspNetCore.Mvc.Controllers.ServiceBasedControllerActivator.Create(ControllerContext actionContext)at Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider&LT;&GT; c__DisplayClass5_0.g__CreateController0(ControllerContext controllerContext)at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(州及放大器; 接下来,范围&amp;范围,对象&amp; state,Boolean&amp; isCompleted)at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__14.MoveNext() ---从抛出异常的先前位置开始的堆栈跟踪结束--- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务) Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.d__22.MoveNext() ---从抛出异常的先前位置开始的堆栈跟踪结束--- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext 上下文) Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State&amp; next, 适用范围和放大器;范围,对象&amp; state,Boolean&amp; isCompleted)at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.d__17.MoveNext() ---从抛出异常的先前位置开始的堆栈跟踪结束--- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务) Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.d__15.MoveNext() ---从抛出异常的先前位置开始的堆栈跟踪结束--- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务) Microsoft.AspNetCore.Builder.RouterMiddleware.d__4.MoveNext() ---从抛出异常的先前位置开始的堆栈跟踪结束--- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务) Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware.d__7.MoveNext() ---从抛出异常的先前位置开始的堆栈跟踪结束--- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务) Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.d__7.MoveNext()
谢谢!
答案 0 :(得分:1)
使用自定义DI框架时,您需要更新ConfigureServices
以返回自定义IServiceProvider
。
public IServiceProvider ConfigureServices(IServiceCollection services) {
services.AddMvc()
.AddControllersAsServices();
var container = new Container(rules => rules.With(propertiesAndFields: PropertiesAndFields.Auto))
.WithDependencyInjectionAdapter(services);
var provider = container.ConfigureServiceProvider<CompositionRoot>();
return provider
}
在运行时,您的提供程序将用于解析类型并注入依赖项。
参考: Introduction to Dependency Injection in ASP.NET Core: Replacing the default services container