question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't get dependency from container

See original GitHub issue

Here is a demo repository https://gitlab.com/ittennull/bolero_di_issue/ First commit is the standard template as is, second commit adds an interface, registers it and tries to inject it:

type ISomething =
    abstract member Foo: unit -> unit
    
type Something() =
    interface ISomething with
        member _.Foo() = ()

type MyApp() =
    inherit ProgramComponent<Model, Message>()
    
    [<Inject>]
    member val Something = Unchecked.defaultof<ISomething> with get,set
let Main args =
        let builder = WebAssemblyHostBuilder.CreateDefault(args)
        builder.Services.AddSingleton<ISomething, Something>() |> ignore
        ...

I get " An unhandled exception was thrown by the application. System.InvalidOperationException: Cannot provide a value for property ‘Something’ on type ‘BoleroHostedApplication1.Client.Main+MyApp’. There is no registered service of type ‘BoleroHostedApplication1.Client.Main+ISomething’"

I tried the same thing with blazor wasm project in C# - worked fine

Any ideas?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
Tarmilcommented, Sep 3, 2020

The problem is indeed with prerendering. During prerendering, the client application’s main function isn’t called. Instead, the dependency injection is done using aspnetcore’s container. So any dependencies you need must also be injected in the server-side ConfigureServices.

If you don’t want prerendering, then you can do .AddBoleroHost(prerendering = false) in ConfigureServices. If you want it, then to avoid code duplication, you can write a function that injects everything you need and call it from both the client and the server: https://gitlab.com/Tarmil/bolero_di_issue/-/commit/22f0200b807a09ac56a81af80717518f8d8b44d0

0reactions
ittennullcommented, Sep 3, 2020

Oh, I didn’t know that thing about prerendering, I always thought it’s an optimization and didn’t care about it. Yes, it works now, thanks a lot!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Docker container can't find an installed node dependency
When I start up my docker container it can't find one of my installed dependencies. I've tried removing my build + node_modules folder, ......
Read more >
ContainerDependency - Amazon Elastic Container Service
A container can contain multiple dependencies. When a dependency is defined for container startup, for container shutdown it is reversed. Your Amazon ECS ......
Read more >
Throw/Exit when dependency can't be resolved · Issue #60
The problem is simple, I think that when dependency can't be satisfied (either it doesn't exist or one of its own dependencies wasn't...
Read more >
Dependency injection in ASP.NET Core
Learn how ASP.NET Core implements dependency injection and how to use it.
Read more >
Top 5 TypeScript dependency injection containers
Cover the top five containers for dependency injection in TypeScript, including Typed Inject, InversifyJS, TypeDI, TSyringe, and NestJS.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found