Код: Выделить всё
public class Context : IContext
{
public string CorrelationId { get; set; }
public Context(string id)
{
CorrelationId = id;
}
}
< /code>
Context Accessor: < /p>
internal class RequestContextRegistrator : IRequestContextRegistrator
{
private IContext context;
public IContext RegisterContext(IContext context)
{
this.context = context;
return context;
}
public IContext Get()
{
return context ?? new Context()
{
CorrelationId = context.CorrelationId
};
}
}
< /code>
и объект Singleton: < /p>
public class QueueSender
{
private readonly IRequestContextRegistrator provider;
public QueueSender(IRequestContextRegistrator provider)
{
this.provider = provider;
}
public async Task Send(TCommand command)
{
var context = provider.Get();
var message = PrepareServiceBusMessage(command, userAgent, context?.CorrelationId);
}
}
< /code>
Вся идея заключается в том, чтобы иметь возможность передать контекст, который является уникальным для конкретного «запроса». Запрос не поступает от контроллера dotnet Подробнее здесь: https://stackoverflow.com/questions/641 ... -singleton