title: MassTransit: Can not receive command from Saga to Consumer [Fixed]
body: <p>I have a workflow publishing an event from controller(belongs to <code>csproj1</code>) which'll be consumed by <code>MySaga</code>(belongs to <code>.csproj2</code>), then the Saga when receiving the published message it'll send a new Command that'll be consumed from <code>CommandConsumer</code> (belongs to <code>csproj2</code>).</p>
  <h2>Problem:</h2>
  <p>Can not receive <strong>command</strong> in <code>CommandConsumer</code> while sending it from <code>MySaga</code>.</p>
  <p>I'm using <strong>Azure Service Bus</strong> and <strong>Consumer Saga</strong></p>
  <p>Here is my <code>startup.cs</code> which I think is not well configured:</p>
  <pre class="lang-cs prettyprint-override"><code>
          services.AddMassTransit(x =&gt;
          {                
              x.AddConsumersFromNamespaceContaining&lt;CommandConsumer&gt;();            
              x.AddSaga&lt;MySaga&gt;().InMemoryRepository();
  
              x.UsingAzureServiceBus((context, cfg) =&gt;
              {
                  cfg.Host(Configuration[&quot;ServiceBus:ConnectionString&quot;]);
                  cfg.ConfigureEndpoints(context);
              });
          });
  
  </code></pre>
  <p>Here is <code>MySaga.cs</code></p>
  <pre class="lang-cs prettyprint-override"><code>//...
  
      public readonly Uri targetEndpoint = new Uri($&quot;queue:{KebabCaseEndpointNameFormatter.Instance.SanitizeName(nameof(MyCommand))}&quot;);
  
      public async Task Consume(ConsumeContext&lt;AceptedEvent&gt; context)
      {
          var sendEndpoint = await context.GetSendEndpoint(targetEndpoint);
          await sendEndpoint.Send&lt;MyCommand&gt;(new
          {
              CorrelationId,
              context.Message.Item1,
              context.Message.Item2
          });
      }
  //...
  
  </code></pre>
  <p>Here is <code>CommandConsumer.cs</code></p>
  <pre class="lang-cs prettyprint-override"><code>
  public class CommandConsumer: IConsumer&lt;MyCommand&gt;
  {
      public ILogger&lt;MyCommand&gt; _logger { get; }
      public IService _myService { get; }
      public MyCommandConsumer(ILogger&lt;MyCommand&gt; logger, Iservice myService)
      {
          _logger = logger;
          _myService = myService;
      }
      
  
      public async Task Consume(ConsumeContext&lt;MyCommand&gt; context)
      {
          _logger?.LogInformation($&quot;{context.Message.CorrelationId}&quot;);            
          try
          {
              await _myService.DoSomething();
              await context.Publish&lt;AcceptedEvent&gt;(new
              {
                  CorrelationId = context.CorrelationId.Value,
                  State = &quot;NewState&quot;
              });
          }
          catch (Exception ex)
          {
              await context.Publish&lt;RefusedEvent&gt;(new
              {
                  CorrelationId = context.CorrelationId.Value,
                  State = &quot;Failed&quot;,
                  Error = ex.Message
              });
              throw;
          }
       }
  }
  
  </code></pre>
  <p>If you have any recommendation ideas thanks for sharing and challenging me!</p>
  <h2>Solution:</h2>
  <p>As @ChrisPatterson mentioned, I used the <code>Publish</code> method since I have just one consumer for my command and it works as expected.</p>
  <p>More than that I fixed my configuration for <code>ReceiveEndpoint</code>:</p>
  <pre class="lang-cs prettyprint-override"><code>
  var repository = new InMemorySagaRepository&lt;MySaga&gt;();
          x.UsingAzureServiceBus((context, cfg) =&gt;
          {
              cfg.Host(Configuration[&quot;ServiceBus:ConnectionString&quot;]);
              cfg.ReceiveEndpoint(&quot;queue&quot;, cfgEndpoint =&gt;
              {
                  cfgEndpoint.Saga&lt;MySaga&gt;(repository);
              });
              cfg.ReceiveEndpoint(KebabCaseEndpointNameFormatter.Instance.SanitizeName(nameof(MyCommand)),
                  cfgEndpoint =&gt;
                  {
                      cfgEndpoint.ConfigureConsumer&lt;CommandConsumer&gt;(context);
                  });
  
          });
  
  </code></pre>