madskristensen / WebEssentials.AspNetCore.ServiceWorker

Other
339 stars 61 forks source link

Raises System.InvalidOperationException on production server while working correctly in Visual Studio #19

Open HaraldMuehlhoffCC opened 6 years ago

HaraldMuehlhoffCC commented 6 years ago

While it worked like a charm running inside Visual Studio 2017 under Windows 10 I got

System.InvalidOperationException: Unable to resolve service for type 'Microsoft. AspNetCore.Http.IHttpContextAccessor' while attempting to activate 'WebEssential s.AspNetCore.Pwa.ServiceWorkerTagHelperComponent'.

while running it on one of my Windows 2012 R2 production servers. Had to add

services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

to ConfigureServices.

WayneHiller commented 6 years ago

Same here. Actually I get the error just running the site under IIS on my dev box.

LoneBunny commented 6 years ago

I ran into this same issue as well. I want to run my app through IIS, so it's always available in my test environment. I prefer for my test and production environments to mirror each other as closely as possible. Searching for a while, I found this article that explains the issue:

https://dotnetthoughts.net/progressive-web-apps-on-aspnet-core/

The suggestions provided there helped me out. I just adjusted ConfigureServices in the Startup.cs file like this:

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton(typeof(IHttpContextAccessor), typeof(HttpContextAccessor));
            services.AddMvc();
            services.AddProgressiveWebApp();
        }

Just a note, I also needed to add a using reference in my Startup class to Microsoft.Extensions.DependencyInjection.

Hope this helps.