更新时间:2023-02-15 19:48:25
您不必将HttpContextAccessor
注册为范围内的依赖项.只需使用一个单例.
You don't have to register the HttpContextAccessor
as a scoped dependency. Just use a singleton.
我们正在使用以下方法进行生产:
We have this working in production using this:
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
与此初始化程序结合:
public class SessionDetailsTelemetryEnrichment : TelemetryInitializerBase
{
public SessionDetailsTelemetryEnrichment(IHttpContextAccessor httpContextAccessor) : base(httpContextAccessor)
{
}
protected override void OnInitializeTelemetry(HttpContext platformContext, RequestTelemetry requestTelemetry, ITelemetry telemetry)
{
telemetry.Context.User.AuthenticatedUserId =
platformContext.User?.Claims.FirstOrDefault(c => c.Type == "Username")?.Value ?? string.Empty;
}
}