by Anonymous » 22 Feb 2025, 00:00
Ich habe einen Scoped Kontext , auf den durch Methode aus transienten zugegriffen wird. Dienst.
Code: Select all
public class Context : IContext
{
public string CorrelationId { get; set; }
public Context(string id)
{
CorrelationId = id;
}
}
< /code>
Kontext -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>
und Singleton -Objekt: < /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>
Die gesamte Idee besteht darin, die Kontext -ID weiterzugeben, die für die jeweilige "Anforderung" eindeutig ist. Die Anforderung stammt nicht von DotNet
Controller, sie stammt aus der Warteschlangenempfängerklasse.
Ich habe einen Scoped Kontext , auf den durch Methode aus transienten zugegriffen wird. Dienst.[code]public class Context : IContext
{
public string CorrelationId { get; set; }
public Context(string id)
{
CorrelationId = id;
}
}
< /code>
Kontext -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>
und Singleton -Objekt: < /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>
Die gesamte Idee besteht darin, die Kontext -ID weiterzugeben, die für die jeweilige "Anforderung" eindeutig ist. Die Anforderung stammt nicht von DotNet [/code] Controller, sie stammt aus der Warteschlangenempfängerklasse.