0

我在使用 .Net Core (3.1) 和 Mysql (provider Pomelo.mysql 3.1.1) 时遇到问题。我有一个非常基本的函数(只是从表中获取所有记录,一个单行函数),但由于某种原因,我总是得到 ObjectDisposedException,所有堆栈跟踪都发生在 Pomelo Provider 中。有谁知道这可能是什么原因造成的?

System.ObjectDisposedException: Cannot access a disposed object.
      Object name: 'MySqlConnection'.
         at MySql.Data.MySqlClient.MySqlConnection.VerifyNotDisposed() in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlConnection.cs:line 707
         at MySql.Data.MySqlClient.MySqlConnection.get_Session() in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlConnection.cs:line 480
         at MySqlConnector.Core.ICancellableCommandExtensions.ResetCommandTimeout(ICancellableCommand command) in C:\projects\mysqlconnector\src\MySqlConnector\Core\ICancellableCommand.cs:line 42
         at MySql.Data.MySqlClient.MySqlCommand.ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlCommand.cs:line 261

初始化代码是:

            services
                .AddDbContextPool<DeviceDbContext>(o => o
                    .UseMySql(connectionString, o => o
                        .ServerVersion(new ServerVersion(new Version(8, 0, 19), ServerType.MySql))
                        .MigrationsAssembly(typeof(DeviceDbContext).Assembly.GetName().Name)
                        .EnableRetryOnFailure()))
                .AddHostedService<MigrationsHostedService<DeviceDbContext>>()
                .AddHealthChecks()
                .AddMySql(connectionString, "mysql");
return services;

然后它只是

services.AddDbContext();

之后的用法相当简单:

private readonly DeviceDbContextdbContext;

        public DeviceEntityFrameworkRepository(DeviceDbContext dbContext)
        {
            EnsureArg.IsNotNull(dbContext, nameof(dbContext));
            this.dbContext = dbContext;
        }

        public async Task<IEnumerable<Device>> FindAllDevicesAsync()
            => await this.dbContext.Set<Device>().ToListAsync().ConfigureAwait(false);

并且完整的异常堆栈是

System.ObjectDisposedException: Cannot access a disposed object.
      Object name: 'MySqlConnection'.
         at MySql.Data.MySqlClient.MySqlConnection.VerifyNotDisposed() in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlConnection.cs:line 707
         at MySql.Data.MySqlClient.MySqlConnection.get_Session() in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlConnection.cs:line 480
         at MySqlConnector.Core.ICancellableCommandExtensions.ResetCommandTimeout(ICancellableCommand command) in C:\projects\mysqlconnector\src\MySqlConnector\Core\ICancellableCommand.cs:line 42
         at MySql.Data.MySqlClient.MySqlCommand.ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlCommand.cs:line 261
         at System.Data.Common.DbCommand.ExecuteReaderAsync(CancellationToken cancellationToken)
         at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
         at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
         at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
         at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.AsyncEnumerator.InitializeReaderAsync(DbContext _, Boolean result, CancellationToken cancellationToken)
         at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementationAsync[TState,TResult](Func`4 operation, Func`4 verifySucceeded, TState state, CancellationToken cancellationToken)
         at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementationAsync[TState,TResult](Func`4 operation, Func`4 verifySucceeded, TState state, CancellationToken cancellationToken)
         at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
4

1 回答 1

0

初始化代码是:

            services
                .AddDbContextPool<DeviceDbContext>(o => o
                    .UseMySql(connectionString, o => o
                        .ServerVersion(new ServerVersion(new Version(8, 0, 19), ServerType.MySql))
                        .MigrationsAssembly(typeof(DeviceDbContext).Assembly.GetName().Name)
                        .EnableRetryOnFailure()))
                .AddHostedService<MigrationsHostedService<DeviceDbContext>>()
                .AddHealthChecks()
                .AddMySql(connectionString, "mysql");
return services;

然后它只是

services.AddDbContext();

听起来你正在实例化两个 DbContexts。您可能想使用其中一个AddDbContextPool()AddDbContext(),但不能同时使用两者。

出于测试目的,首先使用AddDbContext()并查看您是否仍然过早地处置上下文。

如果您愿意,您还可以发布/上传您的完整项目,以便我们仔细查看您的代码到底出了什么问题。

于 2020-05-06T02:27:22.897 回答