我試圖使這項工作:充分利用SingleInstance分量PerRequest服務
builder.RegisterInstance(EngineFactory.Instance).SingleInstance().ExternallyOwned();
builder.Register(c =>
{
EngineFactory engineFactory = DependencyResolver.Current.GetService<EngineFactory>();
//EngineFactory engineFactory = c.Resolve<EngineFactory>();
IEngineService engineService = engineFactory.GetService();
return engineService;
}).InstancePerRequest().As<IEngineService>();
而且我得到的錯誤:
No scope with a Tag matching 'AutofacWebRequest' is visible from the scope in which the instance was requested.
This generally indicates that a component registered as per-HTTP request is being requested by a SingleInstance() component (or a similar scenario.)
Under the web integration always request dependencies from the DependencyResolver.Current or ILifetimeScopeProvider.RequestLifetime, never from the container itself.
爲了把事情說清楚,我想EngineFactory到是單身人士(這是外部創建的),IEngineService是Per(web)請求實例。
控制器屬性注入IEngineService時發生錯誤。
我不明白我在做什麼錯?
你能分享異常的堆棧跟蹤嗎? –