Skip to content

Dependency injection doesn't work in Razor Components class library (former blazorlib) #7720

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

Closed
salaros opened this issue Feb 19, 2019 · 4 comments
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates

Comments

@salaros
Copy link

salaros commented Feb 19, 2019

Describe the bug

I'm building a Blazor app and want to store components in a separate class library.
I works on .NET Core 3.0 preview 2 (except the static assets are not injected #6349),
but when I try to use @inject to inject some services entire page hangs.
The same component works and gets the service injects if I move it back to the root project.

To Reproduce

Steps to reproduce the behavior:

  1. Install latest Blazor templates via dotnet new -i Microsoft.AspNetCore.Blazor.Templates::0.8.0-preview-19075-0444
  2. Create a Razor components dotnet new razorcomponents / a Blazor app dotnet new blazor
  3. Create an .NET Standard class library containing a class called SomeService
    3.1 Inject a service in Startup.ConfigureServices method using .AddTransient<SomeService>()
  4. Create a component with @inject SomeService someService tag
  5. Try to run the application - it works
  6. Create a new Razor Components class library dotnet new blazorlib -o MyComponentLib1
    6.1 Fix namespaces and versions of the dependencies on the newly created project
  7. Move previously created component into the newly created Razor Components class library
    7.1 Fix the namespaces and using statements
  8. Try to use the component on a page, it will hang the browser

Expected behavior

Components defined in separate Razor Components class libraries must be able to use dependency injection

@Eilon Eilon added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Feb 19, 2019
@chucker
Copy link

chucker commented Feb 20, 2019

I followed the steps almost to a T, and can't reproduce this.

I added a constructor to SomeService, and it gets called as soon as I navigate to the component with inject SomeService someService.

Is there something in particular your SomeService does?

@salaros
Copy link
Author

salaros commented Feb 20, 2019

Yeah, it uses JS interop. I will try to provide a sample repo in few hours

@salaros
Copy link
Author

salaros commented Feb 20, 2019

OK, I've created a sample solution and indeed it works

image

For some reason in my project service injection does not work, unless I move the component from xxx.Shared.Components into xxs.App project, but that's not an ASP.NET Core / Razor Components / Blazor problem. I'm closing this issue.

@salaros salaros closed this as completed Feb 20, 2019
@salaros
Copy link
Author

salaros commented Feb 20, 2019

OK, while describing the steps to reproduce the issue and then while building the sample repo I have oversimplified the service implementation I was having problem with.

Actually I had a service expecting another service to be injected for object->JSON serialization. My current implementation of the latter was relying on Json.NET (aka Newtonsoft.Json) which has known issue with Blazor or better with Blazor's linker.
After appending these rows to my Linker.xml

<linker>
...
  <assembly fullname="System.Core">
    <!-- This is required by Json.NET and any expression.Compile caller -->
    <type fullname="System.Linq.Expressions*" />
  </assembly>
...
</linker>

@ghost ghost locked as resolved and limited conversation to collaborators Dec 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates
Projects
None yet
Development

No branches or pull requests

3 participants