将现有的.Net Core 1.1项目迁移到.Net Core 2.1时。我已将DbContext添加到ConfigureConfig方法的服务中,如下所示。
public void ConfigureServices(IServiceCollection services)
{
//Configure the DB Context
services.AddDbContext<AxDataContext>(options =>
axDbContextOption.Configure(options), ServiceLifetime.Transient);
}
后来,我试图获得这样的服务。
private AxDataContext DataContext =
this.ServiceProvider.GetService<AxDataContext>();
遇到了这样的错误。
An unhandled exception occurred while processing the request.
ObjectDisposedException: Cannot access a disposed object.
Object name: 'IServiceProvider'.
Microsoft.Extensions.DependencyInjection.ServiceLookup.ThrowHelper.ThrowObjectDisposedException()
Stack Query Cookies Headers
ObjectDisposedException: Cannot access a disposed object.
Object name: 'IServiceProvider'.
Microsoft.Extensions.DependencyInjection.ServiceLookup.ThrowHelper.ThrowObjectDisposedException()
Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService<T>(IServiceProvider provider)
Aximpro.Framework.WebCore.Providers.PageProvider.get_DataContext() in PageProvider.cs
+
private AxDataContext DataContext => this.mDataContext ?? (this.mDataContext = this.ServiceProvider.GetService<AxDataContext>());
同一段代码在.Net Core 1.1中运行正常。尽管this link描述了类似的问题,但我还是不明白,.Net Core 2.1在恢复注册服务时是否有任何更改。