Инъекция зависимости в переходных процессах, а затем переход в СинглтонC#

Место общения программистов C#
Anonymous
 Инъекция зависимости в переходных процессах, а затем переход в Синглтон

Сообщение Anonymous »

У меня есть Scoped Context , который доступен из Transient службы через метод. Сервис.

Код: Выделить всё

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

Вернуться в «C#»