Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An excepiton occured when sent message with async #59

Closed
ajdwfnhaps opened this issue Nov 24, 2017 · 2 comments
Closed

An excepiton occured when sent message with async #59

ajdwfnhaps opened this issue Nov 24, 2017 · 2 comments

Comments

@ajdwfnhaps
Copy link

aspnetcore2.0+mysql
DotNetCore.CAP version: 2.1.0
DotNetCore.CAP.RabbitMQ version: 2.1.0
DotNetCore.CAP.MySql version: 2.1.0

Microsoft.EntityFrameworkCore.UnitOfWork version 2.0.2

项目引用其它nuget包:

<ItemGroup>
    <PackageReference Include="Autofac" Version="4.6.1" />
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.2.0" />
    <PackageReference Include="Autofac.Extras.Quartz" Version="3.5.0-unstable0010" />
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
    <PackageReference Include="LibLog" Version="4.2.6" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.0" />
    <PackageReference Include="MySql.Data" Version="8.0.8-dmr" />
    <PackageReference Include="System.Text.Encoding.CodePages" Version="4.4.0" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="1.0.0" />
    <PackageReference Include="Quartz" Version="3.0.0-beta1" />
    <PackageReference Include="Quartz.Serialization.Json" Version="3.0.0-beta1" />
    <PackageReference Include="NLog.Extensions.Logging" Version="1.0.0-rtm-rc2" />
  </ItemGroup>

异常触发条件:

  • 定义了一个Quartz Job,每5秒执行一次,Quzrtz Host on aspnetcore2.0 website

  • 当使用PublishAsync发布MQ消息时,第1次触发Job时正常工作,从第2次触发Job开始,可能触发错误: There is already an open DataReader associated with this Connection which must be closed first.

详细错误信息:

ex
{MySql.Data.MySqlClient.MySqlException (0x80004005): There is already an open DataReader associated with this Connection which must be closed first.
   at MySql.Data.Serialization.MySqlSession.StartQuerying(MySqlCommand command)
   at MySql.Data.MySqlClient.CommandExecutors.TextCommandExecutor.<ExecuteReaderAsync>d__3.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MySql.Data.MySqlClient.CommandExecutors.TextCommandExecutor.<ExecuteNonQueryAsync>d__1.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MySql.Data.MySqlClient.MySqlTransaction.<CommitAsync>d__2.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MySql.Data.MySqlClient.MySqlTransaction.Commit()
   at DotNetCore.CAP.Abstractions.CapPublisherBase.ClosedCap() in D:\Work\aimei_finance\trunk\code\aimei_finance\CAP\DotNetCore.CAP\Abstractions\CapPublisherBase.cs:line 164
   at DotNetCore.CAP.Abstractions.CapPublisherBase.PublishWithTransAsync(String name, String content) in D:\Work\aimei_finance\trunk\code\aimei_finance\CAP\DotNetCore.CAP\Abstractions\CapPublisherBase.cs:line 137
   at DotNetCore.CAP.Abstractions.CapPublisherBase.PublishAsync[T](String name, T contentObj, String callbackName) in D:\Work\aimei_finance\trunk\code\aimei_finance\CAP\DotNetCore.CAP\Abstractions\CapPublisherBase.cs:line 36
   at AAM.Quartz.Domain.Service.Finance.AccountPeriodService.SendAccountPeriodMQMessageAsync() in D:\Work\aimei_finance\trunk\code\aimei_finance\Quartz\AAM.Quartz.Domain.Service\Finance\AccountPeriodService.cs:line 32
   at AAM.Quartz.Tasks.Finance.AccountPeriodJob.<Invoke>d__3.MoveNext() in D:\Work\aimei_finance\trunk\code\aimei_finance\Quartz\AAM.Quartz.Task\Finance\AccountPeriodJob.cs:line 25
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at AAM.Quartz.Tasks.JobBase.<Execute>d__2.MoveNext() in D:\Work\aimei_finance\trunk\code\aimei_finance\Quartz\AAM.Quartz.Task\JobBase.cs:line 32}
    Data: {System.Collections.ListDictionaryInternal}
    ErrorCode: -2147467259
    HResult: -2147467259
    HelpLink: null
    InnerException: null
    Message: "There is already an open DataReader associated with this Connection which must be closed first."
    Number: 0
    Source: "MySqlConnector"
    SqlState: null
    StackTrace: "   at MySql.Data.Serialization.MySqlSession.StartQuerying(MySqlCommand command)\r\n   at MySql.Data.MySqlClient.CommandExecutors.TextCommandExecutor.<ExecuteReaderAsync>d__3.MoveNext()\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at MySql.Data.MySqlClient.CommandExecutors.TextCommandExecutor.<ExecuteNonQueryAsync>d__1.MoveNext()\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at MySql.Data.MySqlClient.MySqlTransaction.<CommitAsync>d__2.MoveNext()\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at MySql.Data.MySqlClient.MySqlTransaction.Commit()\r\n   at DotNetCore.CAP.Abstractions.CapPublisherBas
e.ClosedCap() in D:\\Work\\aimei_finance\\trunk\\code\\aimei_finance\\CAP\\DotNetCore.CAP\\Abstractions\\CapPublisherBase.cs:line 164\r\n   at DotNetCore.CAP.Abstractions.CapPublisherBase.PublishWithTransAsync(String name, String content) in D:\\Work\\aimei_finance\\trunk\\code\\aimei_finance\\CAP\\DotNetCore.CAP\\Abstractions\\CapPublisherBase.cs:line 137\r\n   at DotNetCore.CAP.Abstractions.CapPublisherBase.PublishAsync[T](String name, T contentObj, String callbackName) in D:\\Work\\aimei_finance\\trunk\\code\\aimei_finance\\CAP\\DotNetCore.CAP\\Abstractions\\CapPublisherBase.cs:line 36\r\n   at AAM.Quartz.Domain.Service.Finance.AccountPeriodService.SendAccountPeriodMQMessageAsync() in D:\\Work\\aimei_finance\\trunk\\code\\aimei_finance\\Quartz\\AAM.Quartz.Domain.Service\\Finance\\AccountPeriodService.cs:line 32\r\n   at AAM.Quartz.Tasks.Finance.AccountPeriodJob.<Invoke>d__3.MoveNext() in D:\\Work\\aimei_finance\\trunk\\code\\aimei_finance\\Quartz\\AAM.Quartz.Task\\Finance\\AccountPeriodJob.cs:line 25\r\n   
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at AAM.Quartz.Tasks.JobBase.<Execute>d__2.MoveNext() in D:\\Work\\aimei_finance\\trunk\\code\\aimei_finance\\Quartz\\AAM.Quartz.Task\\JobBase.cs:line 32"
    TargetSite: {Void StartQuerying(MySql.Data.MySqlClient.MySqlCommand)}

1.ConfigureServices
//注入Db Context和UnitOfWork
```
services.AddMyDbContext(Configuration);

        services.AddCap(x =>
        {
            x.UseEntityFramework<aimeiFinanceContext>();
            x.UseRabbitMQ((m) => {
                m.HostName = "10.0.1.7";
                m.Port = 5672;
                m.UserName = "aimei";
                m.Password = "a123456";
            });
            x.UseDashboard();
        });

2.AddMyDbContext扩展方法为:

///


/// 配置要使用的Db Context
///

///
///
public static void AddMyDbContext(this IServiceCollection services, IConfigurationRoot config)
{
//EF Core For Mysql
//Core 2.0 支持连接池配置,但暂测试不成功 AddDbContextPool
services.AddDbContext(options => options.UseMySql(config.GetSection("ConnectionStrings:housekeeper").Value))
.AddUnitOfWork();

        services.AddDbContext<aimeiFinanceContext>(options => options.UseMySql(config.GetSection("ConnectionStrings:aimei_finance").Value))
               .AddUnitOfWork<aimeiFinanceContext>();
    }

3.Configure

//使用CAP
app.UseCap();


4.Quartz Job

///


/// 账务系统账期定时触发统计作业
///

public class AccountPeriodJob : JobBase
{
private readonly ILogger _logger;
private readonly IAccountPeriodService _accountPeriodService;

    public AccountPeriodJob(ILogger<AccountPeriodJob> logger, IAccountPeriodService accountPeriodService)
    {
        _logger = logger;
        _accountPeriodService = accountPeriodService;
    }

    protected override async Task Invoke(IJobExecutionContext context)
    {
        _logger.LogInformation("AccountPeriodJob Runing...");
        await _accountPeriodService.SendAccountPeriodMQMessageAsync();
        //return;
    }
}

5.服务类:

public interface IAccountPeriodService
{
Task SendAccountPeriodMQMessageAsync();
}

public class AccountPeriodService : IAccountPeriodService
{
    private readonly ILogger<AccountPeriodService> _logger;
    private readonly ICapPublisher _capBus;
    private readonly IUnitOfWork<aimeiFinanceContext> _financeunitOfWork;

    public AccountPeriodService(ILogger<AccountPeriodService> logger, ICapPublisher capPublisher, IUnitOfWork<aimeiFinanceContext> financeunitOfWork)
    {
        _logger = logger;
        _capBus = capPublisher;
        _financeunitOfWork = financeunitOfWork;
    }

    public Task<bool> SendAccountPeriodMQMessageAsync()
    {

   //异步发布时,会第二次触发时可能会抛出错误
_capBus.PublishAsync("sample.finance.mac.acc.detail", DateTime.Now);
//如果使用同步方法,一切正常
//_capBus.Publish("sample.finance.mac.acc.detail", DateTime.Now);

        return Task.FromResult(true);
    }
@yang-xiaodong
Copy link
Member

Hi, Thanks for your issue , I will check it later.

@yang-xiaodong
Copy link
Member

Reference of #62

I will close this issue, if other questions, please feedback to #62

@yang-xiaodong yang-xiaodong changed the title 使用异步发布消息时,会出现错误:There is already an open DataReader associated with this Connection which must be closed first. An excepiton occured when sent message with async Nov 25, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants