chanan / BlazorStyled

CSS in Blazor Components
https://blazorstyled.io
The Unlicense
191 stars 19 forks source link

AddBlazorStyled() exception in ASP.NET Core blazor server project #93

Open TheCyberCore opened 4 years ago

TheCyberCore commented 4 years ago

Hi, I ran in a little issue with the latest version of this library with .NET Core 3.1. The project was generated with the Visual Studio 2019 wizard. When adding the service via AddBlazorStyled() in Startup.ConfigureServices() the following exception occurs.

System.InvalidOperationException: "Cannot resolve 'BlazorStyled.IStyled' from root provider because it requires scoped service 'BlazorStyled.Stylesheets.IStyleSheet'."

As IStyleSheet and its implementations are internal elements of BlazorStyled, I am also not able to add the DI object manually.

I think this is a bug?

chanan commented 4 years ago

I haven’t seen this issue. I do test both on client and server side projects.

The current master branch of the project doesn’t have that class anymore - as well as the preview release in Nuget. Could you zip up the project and attach it or post it on github and I can take a look?

TheCyberCore commented 4 years ago

I created a minimum test project. The error occurs when trying to get access to IStyled in Startup.Configure().

BlazorStyledTest.zip

chanan commented 4 years ago

Thanks! I will take a look tomorrow evening

TheCyberCore commented 4 years ago

Thank you :) I now also tested the latest preview 3.00 alpha 5 and the error still persists but changed slightly.

System.InvalidOperationException: "Cannot resolve 'BlazorStyled.IStyled' from root provider because it requires scoped service 'BlazorStyled.Internal.ScriptManager'."

chanan commented 4 years ago

Took a quick look. Remove the line in Startup.cs -> Configure method: IStyled styled = app.ApplicationServices.GetRequiredService<IStyled>();. Once you do that, you can use it in Inde.razor (or where ever) either by using the <Styled /> tag or if you want to use the API you can use: @inject BlazorStyled.IStyled Styled

chanan commented 4 years ago

@TheCyberCore were you able to get this working?

TheCyberCore commented 4 years ago

@chanan Yes, thank you. This works. But it does not really fit in my app, because the theming variables should be active before the first razor subpage loads. I also tried to initialize the theming within the razor root page/layout page, but the injection seems to work only on sub-page level.