C# EF6 使用 Unity 对一个上下文进行多个异步调用 - Asp.Net Web Api
C# EF6 make multiple async calls to one context using Unity - Asp.Net Web Api
我在访问我的 SPA 站点时收到以下错误,该站点在加载时对 API 进行了一些调用:
A second operation started on this context before a previous
asynchronous operation completed. Use 'await' to ensure that any
asynchronous operations have completed before calling another method
on this context. Any instance members are not guaranteed to be thread
safe.
如果我不使用相同的上下文并尝试更新值,我会收到以下错误,请参阅此问题:
The relationship between the two objects cannot be defined because
they are attached to different ObjectContext objects.
这意味着我无法使用此处建议的答案来使用多个上下文:
UnityConfig.cs:
container.RegisterType<DbContext>(new HierarchicalLifetimeManager());
container.RegisterType<ISupplierRepository, SupplierRepository>();
container.RegisterType<IContactRepository, ContactRepository>();
我该如何解决这个问题?
用 Microsoft.Practices.Unity.Mvc
中的 PerRequestLifetimeManager
解决了它。
container.RegisterType<DbContext>(new PerRequestLifetimeManager());
container.RegisterType<ISupplierRepository, SupplierRepository>();
container.RegisterType<IContactRepository, ContactRepository>();
我在访问我的 SPA 站点时收到以下错误,该站点在加载时对 API 进行了一些调用:
A second operation started on this context before a previous asynchronous operation completed. Use 'await' to ensure that any asynchronous operations have completed before calling another method on this context. Any instance members are not guaranteed to be thread safe.
如果我不使用相同的上下文并尝试更新值,我会收到以下错误,请参阅此问题:
The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects.
这意味着我无法使用此处建议的答案来使用多个上下文:
UnityConfig.cs:
container.RegisterType<DbContext>(new HierarchicalLifetimeManager());
container.RegisterType<ISupplierRepository, SupplierRepository>();
container.RegisterType<IContactRepository, ContactRepository>();
我该如何解决这个问题?
用 Microsoft.Practices.Unity.Mvc
中的 PerRequestLifetimeManager
解决了它。
container.RegisterType<DbContext>(new PerRequestLifetimeManager());
container.RegisterType<ISupplierRepository, SupplierRepository>();
container.RegisterType<IContactRepository, ContactRepository>();