【发布时间】:2019-02-04 16:15:03
【问题描述】:
我在 Autofac 中有如下配置:
builder.Register<ServiceFactory>(x => y => x.Resolve<IComponentContext>().Resolve(y));
使用此配置我得到错误:
System.ObjectDisposedException:此解析操作已经 结束了。 使用 lambda 注册组件时,无法存储 lambda 的 IComponentContext 'c' 参数。 相反,要么从 'c' 再次解析 IComponentContext,要么解析基于 Func 的工厂以从中创建后续组件。
如果我使用以下内容,则可以:
builder.Register<ServiceFactory>(x => {
IComponentContext context = x.Resolve<IComponentContext>();
return y => context.Resolve(y);
});
这个配置不能在一行代码中完成吗?
我错过了什么?
【问题讨论】:
-
您没有遗漏任何东西。无法从内部 lambda 中解析组件上下文。它必须在外部解决,并在内部使用。