Abhängigkeitsinjektion in transient und dann vorübergehend in SingletonC#

Ein Treffpunkt für C#-Programmierer
Anonymous
 Abhängigkeitsinjektion in transient und dann vorübergehend in Singleton

Post by Anonymous »

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.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post