0
drg_tverkroost created
Hi there,
Unfortunately the issue still exists. Still got NullReferenceException. Why? I checked the documentation and solutions in support forum. I created a custom LookupApiRequestService. See
and
[Extension property in Identity Users not working in Azure #6195](https://abp.io/support/questions/6195/Extension-property-in-Identity-Users-not-working-in-Azure)
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using System.Globalization;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using System;
using Volo.Abp.AspNetCore.Components.Web.Extensibility;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Http.Client.Authentication;
using Volo.Abp.Http.Client;
using Volo.Abp.MultiTenancy;
namespace NEXTjeugd.Blazor
{
[Dependency(ReplaceServices = true)]
public class CustomBlazorServerLookupApiRequestService : ILookupApiRequestService, ITransientDependency
{
public IHttpClientFactory HttpClientFactory { get; }
public IRemoteServiceHttpClientAuthenticator HttpClientAuthenticator { get; }
public IRemoteServiceConfigurationProvider RemoteServiceConfigurationProvider { get; }
public ICurrentTenant CurrentTenant { get; }
public IHttpContextAccessor HttpContextAccessor { get; }
public NavigationManager NavigationManager { get; }
public ILogger<CustomBlazorServerLookupApiRequestService> Logger { get; }
public CustomBlazorServerLookupApiRequestService(IHttpClientFactory httpClientFactory,
IRemoteServiceHttpClientAuthenticator httpClientAuthenticator,
ICurrentTenant currentTenant,
IHttpContextAccessor httpContextAccessor,
NavigationManager navigationManager,
IRemoteServiceConfigurationProvider remoteServiceConfigurationProvider,
ILogger<CustomBlazorServerLookupApiRequestService> logger)
{
HttpClientFactory = httpClientFactory;
HttpClientAuthenticator = httpClientAuthenticator;
CurrentTenant = currentTenant;
HttpContextAccessor = httpContextAccessor;
NavigationManager = navigationManager;
RemoteServiceConfigurationProvider = remoteServiceConfigurationProvider;
Logger = logger;
}
public async Task<string> SendAsync(string url)
{
var client = HttpClientFactory.CreateClient();
var requestMessage = new HttpRequestMessage(HttpMethod.Get, url);
var uri = new Uri(url, UriKind.RelativeOrAbsolute);
if (!uri.IsAbsoluteUri)
{
var remoteServiceConfig = await RemoteServiceConfigurationProvider.GetConfigurationOrDefaultOrNullAsync("Default");
if (remoteServiceConfig != null)
{
// Blazor tiered mode
var baseUrl = remoteServiceConfig.BaseUrl;
client.BaseAddress = new Uri(baseUrl);
AddHeaders(requestMessage);
await HttpClientAuthenticator.Authenticate(new RemoteServiceHttpClientAuthenticateContext(client, requestMessage, new RemoteServiceConfiguration(baseUrl), string.Empty));
}
else
{
// Blazor server mode
client.BaseAddress = new Uri(NavigationManager.BaseUri);
foreach (var header in HttpContextAccessor.HttpContext!.Request.Headers.Where(x => x.Key != "Accept-Encoding"))
{
requestMessage.Headers.TryAddWithoutValidation(header.Key, header.Value.ToArray());
}
}
}
var response = await client.SendAsync(requestMessage);
var responseString = await response.Content.ReadAsStringAsync();
Logger.LogInformation("Response: {0}", responseString);
return responseString;
}
protected virtual void AddHeaders(HttpRequestMessage requestMessage)
{
if (CurrentTenant.Id.HasValue)
{
requestMessage.Headers.Add(TenantResolverConsts.DefaultTenantKey, CurrentTenant.Id.Value.ToString());
}
var currentCulture = CultureInfo.CurrentUICulture.Name ?? CultureInfo.CurrentCulture.Name;
if (!currentCulture.IsNullOrEmpty())
{
requestMessage.Headers.AcceptLanguage.Add(new(currentCulture));
}
}
}
}
Is there a solution for this?
ABP Framework version**: v8.2.3 UI Type**: Blazor Server Database System**: EF Core (SQL Server) Tiered (for MVC) Exception message and full stack trace**:
1 Answer(s)
-
0
hi
It seems that the
HttpContextAccessor.HttpContext
isnull
. Can you set a breakpoint to confirm this?You can skip adding headers if
HttpContext
isnull
if (HttpContextAccessor.HttpContext != null ) { foreach (var header in HttpContextAccessor.HttpContext!.Request.Headers.Where(x => x.Key != "Accept-Encoding")) { requestMessage.Headers.TryAddWithoutValidation(header.Key, header.Value.ToArray()); } }
Thanks.