Skip to content

Commit e8d140b

Browse files
committed
allow httpContextAccessor is null
1 parent cdb0903 commit e8d140b

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

src/WeihanLi.Web.Extensions/Services/HttpContextTenantProvider.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,24 @@ IOptions<HttpContextTenantProviderOptions> options
5858
_httpContextAccessor = httpContextAccessor;
5959
_options = options;
6060
}
61-
#nullable disable
62-
public string GetTenantId()
61+
62+
public string? GetTenantId()
6363
{
64-
ArgumentNullException.ThrowIfNull(_httpContextAccessor.HttpContext);
64+
if (_httpContextAccessor.HttpContext is null)
65+
{
66+
return null;
67+
}
68+
6569
return _options.Value.TenantIdFactory.Invoke(_httpContextAccessor.HttpContext);
6670
}
6771

68-
public TenantInfo GetTenantInfo()
72+
public TenantInfo? GetTenantInfo()
6973
{
70-
ArgumentNullException.ThrowIfNull(_httpContextAccessor.HttpContext);
74+
if (_httpContextAccessor.HttpContext is null)
75+
{
76+
return null;
77+
}
78+
7179
return _options.Value.TenantInfoFactory.Invoke(_httpContextAccessor.HttpContext);
7280
}
73-
#nullable restore
7481
}

src/WeihanLi.Web.Extensions/Services/HttpContextUserIdProvider.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ IOptions<HttpContextUserIdProviderOptions> options
3838

3939
public string? GetUserId()
4040
{
41-
ArgumentNullException.ThrowIfNull(_httpContextAccessor.HttpContext);
41+
if (_httpContextAccessor.HttpContext is null)
42+
{
43+
return null;
44+
}
45+
4246
return _userIdFactory.Invoke(_httpContextAccessor.HttpContext);
4347
}
4448
}

0 commit comments

Comments
 (0)