Это метод вызова моего промежуточного программного обеспечения CorrelationId
Код: Выделить всё
public async Task InvokeAsync(HttpContext context, ICorrelationIdGenerator correlationIdGenerator)
{
var correlationId = correlationIdGenerator.Get() == null
? Guid.NewGuid.ToString()
: correlationIdGenerator.Get();
correlationIdGenerator.Set(correlationId);
if (!context.Request.Headers.ContainsKey(Http.Headers.CORRELATION_ID))
{
context.Request.Headers.Add(Http.Headers.CORRELATION_ID, correlationId);
}
context.Response.OnStarting(() =>
{
if (!context.Response.Headers.ContainsKey(Http.Headers.CORRELATION_ID))
{
context.Response.Headers.Add(Http.Headers.CORRELATION_ID, correlationId);
}
return Task.CompletedTask;
});
}
Код: Выделить всё
Services.AddScoped();
Services.AddDaprClient();
На данный момент я вызываю корреляциюIdGenerator каждый раз, когда вызываю PublishEventAsync Dapr, вот так:
Код: Выделить всё
private readonly DaprClient _client;
private readonly ICorrelationIdGenerator _cid;
public MyClass(DaprClient client, ICorrelationIdGenerator cid)
{
_client = client;
_cid = cid;
}
var correlationId = _cid.Get();
var metadata = new Dictionary
{
{ Http.Headers.CORRELATION_ID, correlationId }
};
await _client.PublishEventAsync("component", "my-topic", data, metadata: metadata);
Я использую Dapr.AspNetCore v1.11.0 и Dapr.Workflow v1.11.0.
Подробнее здесь: https://stackoverflow.com/questions/781 ... r-messages