MobileBlazorBindings icon indicating copy to clipboard operation
MobileBlazorBindings copied to clipboard

IJSRuntime : {System.NullReferenceException: Object reference not set to an instance of an object. in DelegatingHandler

Open nssidhu opened this issue 3 years ago • 2 comments

@Eilon

I have filed the bug on with Refit, but was wondering if something is already known about this. I am trying to use IJSRuntime in Refit as for Adding Auth token using Delegating handler.

https://github.com/reactiveui/refit/issues/1316

nssidhu avatar Feb 15 '22 18:02 nssidhu

Hi @nssidhu , I think I've seen things like this happen when the wrong Dependency Injection (DI) scope is used. I think the IJSRuntime service can be consumed only from transitive or scoped services, but not from singleton service. Maybe that's the issue here?

Eilon avatar Feb 15 '22 18:02 Eilon

I tried all singleton, transient & scoped, same error

builder.Services.AddScoped<RefitAuthHeaderHandler>();

            var setting = new RefitSettings
            {
                ContentSerializer = new NewtonsoftJsonContentSerializer(
                            new JsonSerializerSettings
                            {
                                ContractResolver = new CamelCasePropertyNamesContractResolver()
                            })
            };

            builder.Services.AddRefitClient<IXXXXXXXHttpAPI>(setting)
            .ConfigureHttpClient(c =>
                {
                    c.BaseAddress = new System.Uri("https://XXXXXppdev.azurewebsites.net/api");
                })
            .AddHttpMessageHandler<RefitAuthHeaderHandler>();

nssidhu avatar Feb 15 '22 19:02 nssidhu