Merge pull request #4039 from sbwalker/dev

add HttpClientFactory support
This commit is contained in:
Shaun Walker
2024-03-21 10:43:59 -04:00
committed by GitHub
3 changed files with 41 additions and 15 deletions

View File

@ -253,10 +253,7 @@ else
private void Confirm()
{
if (PageState.RenderMode == RenderModes.Interactive || ModuleState.RenderMode == RenderModes.Interactive)
{
DisplayModal();
}
OnClick();
DisplayModal();
}
}

View File

@ -16,6 +16,7 @@ namespace Oqtane.Services
{
private readonly HttpClient _httpClient;
private readonly SiteState _siteState;
private readonly IHttpClientFactory _factory;
protected ServiceBase(HttpClient httpClient, SiteState siteState)
{
@ -23,13 +24,31 @@ namespace Oqtane.Services
_siteState = siteState;
}
protected ServiceBase(IHttpClientFactory factory, SiteState siteState)
{
_factory = factory;
_siteState = siteState;
}
public HttpClient GetHttpClient()
{
if (!_httpClient.DefaultRequestHeaders.Contains(Constants.AntiForgeryTokenHeaderName) && _siteState != null && !string.IsNullOrEmpty(_siteState.AntiForgeryToken))
if (_factory != null)
{
_httpClient.DefaultRequestHeaders.Add(Constants.AntiForgeryTokenHeaderName, _siteState.AntiForgeryToken);
var client = _factory.CreateClient("oqtane");
if (!client.DefaultRequestHeaders.Contains(Constants.AntiForgeryTokenHeaderName) && _siteState != null && !string.IsNullOrEmpty(_siteState.AntiForgeryToken))
{
client.DefaultRequestHeaders.Add(Constants.AntiForgeryTokenHeaderName, _siteState.AntiForgeryToken);
}
return client;
}
else
{
if (!_httpClient.DefaultRequestHeaders.Contains(Constants.AntiForgeryTokenHeaderName) && _siteState != null && !string.IsNullOrEmpty(_siteState.AntiForgeryToken))
{
_httpClient.DefaultRequestHeaders.Add(Constants.AntiForgeryTokenHeaderName, _siteState.AntiForgeryToken);
}
return _httpClient;
}
return _httpClient;
}
// should be used with new constructor