Search code examples
asp.net-coreamazon-sqsmasstransit

MassTransit with AWS SQS - exception sending dynamic message


I'm using Masstransit with AWS SQS/SNS as a transport. Now I'm stuck with a simple problem - adding CorrelationId to published message. Since I'm following recommendation to use interfaces as DTOs, so I can't use CorrelatedBy<Guid> interface and I decided to add __CorrelationId property to messages directly.

I'm using following wrapper to publish message:

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;
    }
}

And here is the usage:

        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
        });

This works well in console app with following bus registration:

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

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

But is 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();

It fails on publish with Object reference not set to an instance of an object. and stacktrace

   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 looks like simple diagnostics method and I can't fugure out, what can be failing there.


Solution

  • You can't send object or other types like that with MassTransit. Messages must be reference types.

    Relevant Documentation