簡體   English   中英

使用 AWS SQS 的 MassTransit - 異常發送動態消息

[英]MassTransit with AWS SQS - exception sending dynamic message

我將 Masstransit 與 AWS SQS/SNS 一起用作傳輸工具。 現在我遇到了一個簡單的問題——將 CorrelationId 添加到已發布的消息中。 由於我遵循將接口用作 DTO 的建議,因此我不能使用CorrelatedBy<Guid>接口,因此我決定直接將__CorrelationId屬性添加到消息中。

我正在使用以下包裝器來發布消息:

public class MessageBus : IMessageBus
{
    // ... omitted for brevity

    public async Task Publish<T>(object message)
        where T : class
    {
        await this.publishEndpoint.Publish<T>(this.CreateCorrelatedMessage(message));
    }

    private object CreateCorrelatedMessage(object message)
    {
        dynamic corellatedMessage = new ExpandoObject();

        foreach (var property in message.GetType().GetProperties())
        {
            ((IDictionary<string, object>)corellatedMessage).Add(property.Name, property.GetValue(message));
        }

        corellatedMessage.__CorrelationId = this.correlationIdProvider.CorrelationId;
        return corellatedMessage;
    }
}

這是用法:

        await this.messageBus.Publish<ObtainRequestToken>(new
        {
            Social = SocialEnum.Twitter,
            AppId = credentials.First(x => x.Name == "TwitterConsumerKey").Value,
            AppSecret = credentials.First(x => x.Name == "TwitterConsumerSecret").Value,
            ReturnUrl = returnUrl
        });

這在具有以下總線注冊的控制台應用程序中運行良好:

        var busControl = Bus.Factory.CreateUsingAmazonSqs(cfg =>
        {
            cfg.Host("eu-west-1", h =>
            {
                h.AccessKey("xxx");
                h.SecretKey("xxx");

                h.Scope($"Local", scopeTopics: true);
            });
        });

但是是 ASP.NET Core application with

        services.AddMassTransit(cfg =>
        {
            cfg.UsingAmazonSqs((context, sqsCfg) =>
            {
                var amazonAccount = this.Configuration
                    .GetSection("AmazonAccount")
                    .Get<AmazonAccountConfig>();

                sqsCfg.Host(amazonAccount.Region, h =>
                {
                    h.AccessKey(amazonAccount.KeyId);
                    h.SecretKey(amazonAccount.SecretKey);

                    h.Scope(this.Environment.EnvironmentName, scopeTopics: true);
                });
            });

            cfg.SetEndpointNameFormatter(new DefaultEndpointNameFormatter(this.Environment.EnvironmentName + "_", false));
        });

        services.AddMassTransitHostedService();

它在發布時失敗, Object reference not set to an instance of an object.

   at MassTransit.Logging.EnabledDiagnosticSource.StartSendActivity[T](SendContext1 context, ValueTuple2[] tags)
   at MassTransit.AmazonSqsTransport.Transport.TopicSendTransport.SendPipe1.<Send>d__5.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult()
   at GreenPipes.Agents.PipeContextSupervisor1.<GreenPipes-IPipeContextSource<TContext>-Send>d__7.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at GreenPipes.Agents.PipeContextSupervisor1.<GreenPipes-IPipeContextSource<TContext>-Send>d__7.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at GreenPipes.Agents.PipeContextSupervisor1.<GreenPipes-IPipeContextSource<TContext>-Send>d__7.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult()
   at MassTransit.Initializers.MessageInitializer2.<Send>d__9.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult()
   at MassTransit.Transports.PublishEndpoint.<>c__DisplayClass18_01.<<PublishInternal>g__PublishAsync|0>d.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid1[T0](CallSite site, T0 arg0)
   at xxx.Main.Api.Messaging.MessageBus.<Publish>d__51.MoveNext() in D:\repo\projects\xxx\mainapi\xxx.Main.Api\Messaging\MessageBus.cs:line 34 

StartSendActivity看起來像簡單的診斷方法,我無法弄清楚,那里可能會失敗。

您不能使用 MassTransit 發送object或類似的其他類型。 消息必須是引用類型。

相關文件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM