Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error on startup in core 2.0 'React.AspNet.HttpContextLifetimeProvider` #433

Closed
dasjestyr opened this issue Aug 13, 2017 · 26 comments
Closed

Comments

@dasjestyr
Copy link

After having followed the first part of the tutorial, I try to run the web site and it throws on startup with

"Cannot resolve scoped service 'React.AspNet.HttpContextLifetimeProvider+PerRequestRegistrations' from root provider."

Is this just the result of a breaking change by AspNetCore 2.0? Is there an incoming fix?

@dasjestyr dasjestyr changed the title Error on startup in core 2.0 Error on startup in core 2.0 'React.AspNet.HttpContextLifetimeProvider` Aug 13, 2017
@Daniel15
Copy link
Member

Likely a breaking change in 2.0. I haven't tested with 2.0 yet. Feel free to submit a pull request if you do work out what broke.

@dasjestyr
Copy link
Author

I'd give it a crack but I can't seem to get it to build. dev-build.bat is completing with errors. "**/*.resx couldn't be found" is about the only intelligible output I'm seeing at the moment.

@Daniel15
Copy link
Member

Does it look like this?

Microsoft.Common.CurrentVersion.targets(2867,5): error MSB3552: Resource file "**/*.resx" cannot be found.

You may be hitting this issue: https://dan.cx/2017/05/fixing-msb3552-resource-file-resx-cannot-be-found:

this is caused by a long-standing MSBuild bug: Wildcard expansion is silently disabled when a wildcard includes a file over MAX_PATH. [...] In my case, my build server was running an old version of npm, which is known to create extremely long file paths. The way to "fix" this is by reducing the nesting of your folders. If you're using npm, upgrading to a newer version (or switching to Yarn) should fix the issue. Otherwise, you may need to move your project to a different directory, such as a directory in the root of C:.

@dasjestyr
Copy link
Author

That's the one

@Daniel15
Copy link
Member

One of the causes I've seen of that is old npm versions. Check npm --version and if it's 2.x or older then npm install --global npm to upgrade.

@dasjestyr
Copy link
Author

Yeah I saw that in the instructions but that didn't seem to help. Then I tried moving the repo folder all the way down to my C: drive to help shorten the paths, but windows is warning me that the filenames are still too long.

@ShikiGami
Copy link
Contributor

I got it to run without any modification to the React.Net source. The problem is the way now things are initialized in Startup.cs in ASP.NET Core 2.0

The problem is that now services are not available at some points in the Startup process. To ensure that 'React.AspNet.HttpContextLifetimeProvider+PerRequestRegistrations' is available the easiest way is to change the type of ConfigureServices(IServiceCollection services) from void to IServiceProvider and add the following line at the end of ConfigureServices(IServiceCollection services) :
return services.BuildServiceProvider();

Either way I'm going to make a pull request where I update the whole React.NET to ASP.NET Core 2.0. One of the biggest advantages is that now with .NET Standard 2.0 is no longer necessary to make so many divisions between .NET Framework and .NET Core libraries.
The only inconvenience is that support would go from .NET Framework 4.5.1 to 4.6.1.

@amccool
Copy link

amccool commented Aug 27, 2017

@ShikiGami your fix works for me

change
public void ConfigureServices(IServiceCollection services)
to
public IServiceProvider ConfigureServices(IServiceCollection services)

and return services.BuildServiceProvider();

@dasjestyr
Copy link
Author

It may be worth noting that for obvious reasons, only had the problem on a windows machine. I had no issue starting a new project on Linux.

@maksgit
Copy link

maksgit commented Sep 4, 2017

@ShikiGami
I looked at your repo https://github.com/ShikiGami/React.NET/blob/origin/core20update/tutorial-code/Startup.cs and didn't see changes which you mentioned. Maybe I do something wrong?

@oleh-zheleznyak
Copy link

@ShikiGami,
your fix works like a charm! Many thanks!

@Daniel15
Copy link
Member

Daniel15 commented Oct 11, 2017

Hey @ShikiGami, thanks for your comment! I actually hit this same error when updating my own website, and your comment helped resolve it :)

I know you have a PR updating some code in this repo, but please also feel free to open a pull request updating the documentation on the ReactJS.NET website. The documentaton page is at https://reactjs.net/getting-started/aspnetcore.html, and the contents are located in this repository. Otherwise, I'll do it, but it likely won't be for another week or two as I'm pretty busy over the next week.

Daniel15 added a commit to Daniel15/Website that referenced this issue Oct 16, 2017
@ZakariaAhmed
Copy link

Thanks for the fix, works great now !

@DanielAdolfsson
Copy link

DanielAdolfsson commented Dec 6, 2017

Just to add some clarity as to what is happening here.

The DependencyInjection framework is by default configured to use call-site validation whenever a service is requested. When UseReact is called, it's ensuring that the required per-request services are available by using GetService from the root call-site, and this is obviously incompatible with the call-site validation feature.

You can disable call-site validation in you WebHost configuration by adding:
.UseDefaultServiceProvider(options => options.ValidateScopes = false)

@dustinsoftware
Copy link
Member

The samples have been updated to .NET Core 2.0, so closing this issue for now. Please feel free to re-open if you'd like some more help!

@ngohungphuc
Copy link

The issue still happened in .Net Core 2.0

@dustinsoftware
Copy link
Member

Hi there, what version of React.AspNet are you building against? The samples in this repo use .Net Core 2.0 so I’m curious why you are still getting errors.

@ngohungphuc
Copy link

I'm using lastest version of .Net core 2.0

@pinty
Copy link

pinty commented Apr 10, 2018

@ShikiGami
Thanks for pointing out the solution, it works!

@Masuzu
Copy link

Masuzu commented May 26, 2018

@ShikiGami Thanks for the solution!

@hermanho
Copy link

The service should be checked within a scope because the class HttpContextLifetimeProvider.PerRequestRegistrations is registered as scoped lifetime.

services.AddScoped<HttpContextLifetimeProvider.PerRequestRegistrations>();

Here is the code to check the service in a scope.

private static void EnsureServicesRegistered(IApplicationBuilder app)
{
  using (var scope = app.ApplicationServices.CreateScope())
  {
    var registrations = scope.ServiceProvider.GetService<HttpContextLifetimeProvider.PerRequestRegistrations>();
    if (registrations == null)
    {
      throw new ReactNotInitialisedException("Please call services.AddReact() before app.UseReact().");
    }
  }
}

@jbrinkle
Copy link

Happening on VS2017 new AspNetCore MVC app with Core 2.1.
However, following migratory steps from Core 2.0 to 2.1, it seems fine. I cannot for the life of me figure out why

@hlalit03
Copy link

I am trying the sample code under ReactJS.NET tutorial(https://reactjs.net/tutorials/aspnetcore.html) & despite making the changes you mentioned it doesn't work. Same works with ASP.NET Core 2.0

@dustinsoftware
Copy link
Member

dustinsoftware commented Nov 15, 2018 via email

@milesvdw
Copy link

@ShikiGami your fix works for me

change
public void ConfigureServices(IServiceCollection services)
to
public IServiceProvider ConfigureServices(IServiceCollection services)

and return services.BuildServiceProvider();

This worked perfectly. Thanks so much! Saved me a real headache.

@mbileya
Copy link

mbileya commented Jun 15, 2019

@ShikiGami - your fix worked for me too. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests