我有一个此类,它从Header获取Authorization Header值并将其存储到这样的变量中:
pr 22 03:02:04 cirunner dockerd[1103]: time="2019-04-22T03:02:04.136857571Z" level=error msg="Handler for DELETE /v1.18/containers/runner-301e5f4d-project-786-concurrent-0-build-4 returned error: No such container: runner-301e5f4d-project-786-concurrent-0-build-4"
Apr 22 03:02:04 cirunner kernel: [1616845.656927] aufs au_opts_verify:1597:dockerd[1568]: dirperm1 breaks the protection by the permission bits on the lower branch
Apr 22 03:02:04 cirunner kernel: [1616846.186616] aufs au_opts_verify:1597:dockerd[1568]: dirperm1 breaks the protection by the permission bits on the lower branch
Apr 22 03:02:05 cirunner kernel: [1616846.383784] aufs au_opts_verify:1597:dockerd[1568]: dirperm1 breaks the protection by the permission bits on the lower branch
Apr 22 03:02:05 cirunner systemd-udevd[1187]: Could not generate persistent MAC address for veth0675b93: No such file or directory
Apr 22 03:02:05 cirunner kernel: [1616846.385245] device veth8b64bcd entered promiscuous mode
Apr 22 03:02:05 cirunner kernel: [1616846.385299] IPv6: ADDRCONF(NETDEV_UP): veth8b64bcd: link is not ready
Apr 22 03:02:05 cirunner systemd-udevd[1188]: Could not generate persistent MAC address for veth8b64bcd: No such file or directory
Apr 22 03:02:05 cirunner kernel: [1616846.788755] eth0: renamed from veth0675b93
Apr 22 03:02:05 cirunner kernel: [1616846.804716] IPv6: ADDRCONF(NETDEV_CHANGE): veth8b64bcd: link becomes ready
Apr 22 03:02:05 cirunner kernel: [1616846.804739] docker0: port 3(veth8b64bcd) entered forwarding state
Apr 22 03:02:05 cirunner kernel: [1616846.804747] docker0: port 3(veth8b64bcd) entered forwarding state
Apr 22 03:02:20 cirunner kernel: [1616861.819201] docker0: port 3(veth8b64bcd) entered forwarding state
Apr 22 03:37:13 cirunner dockerd[1103]: time="2019-04-22T03:37:13.298195303Z" level=error msg="Handler for GET
/v1.18/containers/6f6b71442b5bbc70f980cd05272c8f05d514735f39e9b73b52a094a0e87db475/json returned error: No such container: 6f6b71442b5bbc70f980cd05272c8f05d514735f39e9b73b52a094a0e87db475"
我已经在我的public class AuthenticationHeader
{
private static IHttpContextAccessor _httpContextAccessor;
public AuthenticationHeader(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
public string AuthHeader => _httpContextAccessor.HttpContext?.Request.Headers["Authorization"]
}
Startup.cs
然后使用构造函数注入在我的NetworkClient中使用此类。
services.AddScoped<AuthenticationHeader>();
我在这里docs读过public ClubMatasClient(HttpClient client, ILogger<ClubMatasClient> logger, AuthenticationHeader authHeader)
{
_client = client;
client.BaseAddress = new Uri("url");
client.DefaultRequestHeaders.Add("Accept", "application/json");
_logger = logger;
AuthToken = authHeader.AuthHeader;
}
和Scoped
的生命周期,我很困惑应该使用哪一个。我不想混用我的请求标头中的身份验证标头。
答案 0 :(得分:2)
作用域是每个http请求,瞬态是每个类型。
由于每个请求都是您的授权标头,因此您可以将AuthenticationHeader
类注册为Scoped,这意味着为满足您的请求而创建的所有类型都将获得 same 实例。
但是您也可以将其注册为Transient,然后为您的请求而创建的所有类型都将获得AuthenticationHeader
的 new 实例,该实例在内部使用IHttpContextAccessor,它将为您提供相同的HttpContext http请求。
所以你们两个都可以,唯一的考虑就是内存消耗。范围将使用更少的内存。
编辑:
实际上,您也可以选择Singleton,因为IHttpContextAccessor
registered作为Singleton,internally使用AsyncLocal来存储当前上下文。但是我不建议这样做,因为将来AuthenticationHeader
可以通过一些其他的逻辑来扩展,从而破坏行为。