c# - 简单注入(inject)器 - 部分类注入(inject)

标签 c# c#-4.0 dependency-injection entity-framework-6 simple-injector

如何使用部分类配置注入(inject)?

container.RegisterPerWebRequest<IDataContextAsync, DataBaseContext>();
container.RegisterPerWebRequest<IStoredProcedureContext, DataBaseContext>();

我的代码:

public partial class DataBaseContext : DataContext
public partial class DataBaseContext : IStoredProcedureContext

错误:

-[Torn Lifestyle] The registration for IStoredProcedureContext maps to the same implementation and lifestyle as the registration for IDataContextAsync does. They both map to DataBaseContext (Web Request). This will cause each registration to resolve to a different instance: each registration will have its own instance during a single Web Request. See the Error property for detailed information about the warnings. Please see https://simpleinjector.org/diagnostics how to fix problems and how to suppress individual warnings.

最佳答案

参见here

var container = new Container();
container.Options.DefaultScopedLifestyle = new WebRequestLifestyle();

var registration = Lifestyle.Scoped.CreateRegistration<DataBaseContext>(container);
container.AddRegistration(typeof(IStoredProcedureContext), registration);

container.Verify();

关于c# - 简单注入(inject)器 - 部分类注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34857524/

相关文章:

c# - 我的 SQL 语句有什么问题,有些东西不工作

c# - 不区分大小写比较的 CompareAttribute

c# - 反射测试未显示预期数字

c# - 带有对象初始化器和 LINQ 的 Ninject

c# - 使用静态记录器、静态帮助程序类进行依赖注入(inject)

c# - LINQ 表达式帮助 Func TEntity、TType

c# - UpdatePanel 回发错误 : Sys. WebForms.PageRequestManagerParserErrorException

c# - 无法更改DataGrid中的前景

winforms - 如何在MS图表上显示工具提示

android - "Check that you have added the HiltAndroidRule"用于测试中的 ContentProvider