Asp.net mvc 使用Ninject和DbContext时的并发请求
以下是我负责数据的项目:Asp.net mvc 使用Ninject和DbContext时的并发请求,asp.net-mvc,asp.net-web-api,ninject,Asp.net Mvc,Asp.net Web Api,Ninject,以下是我负责数据的项目: public interface IRepository<T> where T : class { IQueryable<T> All(); T GetById(int id); void Add(T entity); void Update(T entity); .... } 当我尝试使用Ninject依赖性解析时,会出现异常: “在创建模型时无法使用上下文。如果在OnModel
public interface IRepository<T> where T : class
{
IQueryable<T> All();
T GetById(int id);
void Add(T entity);
void Update(T entity);
....
}
当我尝试使用Ninject依赖性解析时,会出现异常:
“在创建模型时无法使用上下文。如果在OnModelCreating方法内使用上下文,或者如果多个线程同时访问同一上下文实例,则可能引发此异常。请注意,DbContext和相关类的实例成员不保证线程安全。”
如果我一次只取一页,一切都很好。我正在使用一个简单的工具“XENU”同时获取多个页面。这就是我通过一次获取多个页面而导致DBContext错误的原因
public static class NinjectWebCommon
{
private static readonly Bootstrapper bootstrapper = new Bootstrapper();
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
bootstrapper.Initialize(CreateKernel);
}
public static void Stop()
{
bootstrapper.ShutDown();
}
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices(kernel);
System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new Ninject.WebApi.DependencyResolver.NinjectDependencyResolver(kernel);
return kernel;
}
private static void RegisterServices(IKernel kernel)
{
EmailSettings emailSettings = new EmailSettings
{
ServerName = Settings.Default.EmailServerName,
...
};
kernel.Bind<IEmailSender>().To<EmailSender>().WithConstructorArgument("settings", emailSettings);
kernel.Bind<IUowData>().To<UowData>().InRequestScope().WithConstructorArgument("context",new ApplicationDbContext());
}
}
公共静态类NinjectWebCommon
{
私有静态只读引导程序Bootstrapper=new Bootstrapper();
公共静态void Start()
{
RegisterModule(typeof(OnePerRequestHttpModule));
RegisterModule(typeof(NinjectHttpModule));
初始化(CreateKernel);
}
公共静态无效停止()
{
bootstrapper.ShutDown();
}
私有静态IKernel CreateKernel()
{
var kernel=新的标准内核();
kernel.Bind().ToMethod(ctx=>()=>newbootstrapper().kernel);
kernel.Bind().To();
注册服务(内核);
System.Web.Http.GlobalConfiguration.Configuration.dependencysolver=新的Ninject.WebApi.dependencysolver.ninjectdependencysolver(内核);
返回内核;
}
私有静态无效注册服务(IKernel内核)
{
EmailSettings EmailSettings=新的EmailSettings
{
ServerName=Settings.Default.EmailServerName,
...
};
kernel.Bind().To().WithConstructorArgument(“设置”,emailSettings);
kernel.Bind().To().InRequestScope().WithConstructorArgument(“context”,新的ApplicationDbContext());
}
}
为整个应用程序生成一个DB上下文。删除它并添加一个绑定
.Bind<DbContext>().To<ApplicationDbContext>().InRequestScope();
.Bind().To().InRequestScope();
为整个应用程序生成一个DB上下文。删除它并添加一个绑定
.Bind<DbContext>().To<ApplicationDbContext>().InRequestScope();
.Bind().To().InRequestScope();
为整个应用程序生成一个DB上下文。删除它并添加一个绑定
.Bind<DbContext>().To<ApplicationDbContext>().InRequestScope();
.Bind().To().InRequestScope();
为整个应用程序生成一个DB上下文。删除它并添加一个绑定
.Bind<DbContext>().To<ApplicationDbContext>().InRequestScope();
.Bind().To().InRequestScope();
我使用的是Unity IoC,但它帮助我理解我错在哪里。谢谢!我正在使用Unity IoC,但它帮助我理解我错在哪里。谢谢!我正在使用Unity IoC,但它帮助我理解我错在哪里。谢谢!我正在使用Unity IoC,但它帮助我理解我错在哪里。谢谢!
.WithConstructorArgument("context",new ApplicationDbContext());
.Bind<DbContext>().To<ApplicationDbContext>().InRequestScope();