0
我剛剛升級到MVC 3,同樣需要升級Autofac。Autofac ServiceLocator問題發佈v2.4.5.724
下面的代碼是工作,但現在失敗,此錯誤 -
這解析操作早已 結束。當使用lambdas註冊組件 時,存儲的lambda的IComponentContext 'c'參數不能爲 。相反,要麼從'c'再次解析 IComponentContext,要麼從012xx解決基於工廠的Func <>到 從中創建後續組件。
public static IServiceLocator Locator;
public class ServiceA : IServiceA
{
}
public interface IServiceA
{
}
[Test]
public void TestAutofacServiceLocator()
{
// This resolve operation has already ended. When registering components using lambdas, the IComponentContext 'c' parameter to the lambda cannot be stored.
// Instead, either resolve IComponentContext again from 'c', or resolve a Func<> based factory to create subsequent components from.
var builder = new ContainerBuilder();
builder.RegisterType<ServiceA>().As<IServiceA>();
builder.Register(c => Locator = new AutofacServiceLocator(c)).As<IServiceLocator>().SingleInstance();
var container = builder.Build();
container.Resolve<IServiceLocator>();
var x = Locator.GetInstance<IServiceA>();
Assert.NotNull(x);
}
我應該怎麼語域IServiceLocator?
我看着問題autofac registration issue in release v2.4.5.724的答案,但我仍然感到困惑。
酷的東西:) ...在MVC3,你可能會考慮刪除您的自定義服務定位器,並使用'DependencyResolver.Current'代替。 – 2011-03-25 04:41:43