0
我在MVC網站上使用LightInject和ServiceProvider適配器。我試圖將它連接到MiniProfiler,但我沒有太多運氣。 MiniProfiler似乎是捕獲MVC控制器的生命週期,但沒有任何依賴關係: Mini Profiler Image Profiling Dump (I was debugging during this screenshot - ignore the 61,744 ms duration)LightInject - 攔截不攔截依賴關係
這是我如何初始化容器:
protected override IServiceProvider BuildServiceProvider(IServiceCollection serviceCollection)
{
var container = new ServiceContainer();
container.Register<IInterceptor, MiniProfilerInterceptor>();
foreach (var descriptor in serviceCollection)
{
if (descriptor.ServiceType.AssemblyQualifiedName.StartsWith("ProjectName"))
{
container.Intercept(s => s.ServiceType == descriptor.ServiceType, i => i.GetInstance<IInterceptor>());
}
}
var serviceProvider = container.CreateServiceProvider(serviceCollection);
return serviceProvider;
}
攔截器看起來是這樣的:
public class MiniProfilerInterceptor : IInterceptor
{
public object Invoke(IInvocationInfo invocation)
{
var profiler = MiniProfiler.Current;
var returnType = invocation.Method.ReturnType;
using (profiler.Step(invocation.Proxy.GetType().Name + ":" + invocation.Method.Name))
{
return invocation.Proceed();
}
}
}
這是控制器和測試依賴性:
public class SomeDependencyController : Controller
{
private readonly SomeDependency _dependency;
public SomeDependencyController(SomeDependency dependency)
{
_dependency = dependency;
}
public MvcHtmlString DoSomething()
{
_dependency.DoSomething();
return new MvcHtmlString("Did it");
}
public ActionResult DoRender()
{
_dependency.DoSomething();
return View("DoRender");
}
}
public class SomeDependency
{
public void DoSomething()
{
Thread.Sleep(1000);
}
}
我在攔截器代碼中拋出了一個斷點。它從來沒有進入攔截器的任何東西爲SomeDependency
,但它進入MVC生命週期的每一個行動SomeDependencyController