0

When I try to call this service and the method GetOrganization() within the http context accessor is null. What could cause the problem?

public class ShopService
{
    private static IHttpContextAccessor? _httpContextAccessor;

    public ShopService(IHttpContextAccessor? httpContextAccessor)
    {
        _httpContextAccessor = httpContextAccessor;
    }
    public static async Task<Organization?> GetOrganization(EnvironmentType environment)
    {
             string? accessToken;
            if (_httpContextAccessor?.HttpContext != null)
            {
                accessToken = GetToken();
            }
            else
            {
                throw new NotImplementedException();
            }
    
            // ... other unimportant code
    }
}

This calls the method from the service:


                Organization = User?.Identities.FirstOrDefault()?.AuthenticationType switch
                {
                    "Google" => ShopService.GetOrganization(EnvironmentType.Google).Result,
                    // other unimportant code
                    };

I have this in startup: builder.Services.AddHttpContextAccessor(); builder.Services.AddScoped<ShopService>();

Am I missing something why does it not work? Am I supposed to add something in startup, other than the ShopService
service?

MatejDodevski
  • 187
  • 13
  • 2
    Please add code and data as text ([using code formatting](/editing-help#code)), not images. Images: A) don't allow us to copy-&-paste the code/errors/data for testing; B) don't permit searching based on the code/error/data contents; and [many more reasons](//meta.stackoverflow.com/a/285557). Images should only be used, in addition to text in code format, if having the image adds something significant that is not conveyed by just the text code/error/data. – gunr2171 Jul 07 '22 at 14:55
  • 1
    What is this being injected into? Please also show you DI setup. It's preferable to paste in code rather than screenshots so others can replicate the problem. – SpruceMoose Jul 07 '22 at 14:55
  • What framework are you targeting? – gunr2171 Jul 07 '22 at 14:55
  • Does this answer your question? [How to add IHttpContextAccessor in the Startup class in the DI in ASP.NET Core 1.0?](https://stackoverflow.com/questions/38184583/how-to-add-ihttpcontextaccessor-in-the-startup-class-in-the-di-in-asp-net-core-1) – gunr2171 Jul 07 '22 at 14:56
  • 2
    Maybe because it's `static`? – haim770 Jul 07 '22 at 15:03
  • @SpruceMoose I don't understand what 'what is this being injected into' means, I update the question with code – MatejDodevski Jul 07 '22 at 15:03
  • @haim770 it has to be static – MatejDodevski Jul 07 '22 at 15:04
  • @gunr2171 I am using .net core, developing a blazor app – MatejDodevski Jul 07 '22 at 15:04
  • @madreflection how do I change this and not be static, how do I call the method from the service then? – MatejDodevski Jul 07 '22 at 15:07
  • 2
    @MatejDodevski, If it has to be `static` then you can't expect it to be set using the constructor (that probably wasn't event called when you merely do `GetOrganization()`). You better pass it as an argument it to the `GetOrganization()` method itself, possibly after resolving it manually in the callsite. – haim770 Jul 07 '22 at 15:08

1 Answers1

-1

Thank you for all your suggestions. I removed the 'static' from the method and added this wherever I needed the service @inject ShopService _shopService

MatejDodevski
  • 187
  • 13
  • 2
    IHttpContextAccessor should not be used in Blazor apps according to [Doc](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-context?view=aspnetcore-6.0#blazor-and-shared-state) – T.Trassoudaine Jul 07 '22 at 15:19