在asp.net mvc 网站中,使用autofac 容器, 并且使用 IObserver<in T> 作为通知的接收者,
如:`
public class WebsiteChangeObserver : IObserver<WebsiteChangeEvent>
{
private readonly OrderProcessingService _orderProcessingService;
public WebsiteChangeObserver(OrderProcessingService orderProcessingService)
{
this._orderProcessingService = orderProcessingService;
}
public void OnCompleted()
{
}
public void OnError(Exception error)
{
}
public void OnNext(WebsiteChangeEvent value)
{
// var _orderProcessingService = ServiceLocator.Current.GetInstance<OrderProcessingService>();
var _orderProcessingService = IocManager.Instance.Container.Resolve<OrderProcessingService>();
if (value.ChangeState == EntryState.Added)
{
var orderId = value.OrigEntity.OrderId;
_orderProcessingService.MarkOrderAsDeployed(orderId);
}
}
} `
在网站启动的时候, 抛出一个错误:
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.
在 autofac 的 文档上 找到如下说明。 http://docs.autofac.org/en/la...
For application startup code or background threads, you may need to look at a different service locator mechanism like Common Service Locator to bypass the need for per-request scope
怎么解决这个问题呢?