在dot net core中的mysql (pomelo mysql provider)--无法访问一个已处置的对象。

问题描述 投票:0回答:1

我在使用.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()
c# mysql .net-core pomelo
1个回答
0
投票

init代码是。

            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() 看看,你是否还在过早地处理上下文。

如果你愿意,你也可以把你的完整项目贴上来,这样我们就可以仔细看看你的代码到底有什么问题。

© www.soinside.com 2019 - 2024. All rights reserved.