Reputation: 8752
I'm having this exception:
System.InvalidOperationException: 'Unable to resolve service for type 'System.Collections.Generic.IAsyncEnumerable`1[MyProject.Interfaces.IFooReader]' while attempting to activate 'MyProject.Services.FooService'.'
while trying to resolve IFooService
with var fooService = provider.GetRequiredService<IFooService>();
.
Here is the FooService
class:
public class FooService : IFooService
{
private readonly IAsyncEnumerable<IFooReader> _foosReaders;
public PriceService(IAsyncEnumerable<IFooReader> foosReaders)
{
this._foosReaders = foosReaders;
}
}
Here is the DI container:
services.AddScoped<IFooReader, FooServiceA>();
services.AddScoped<IFooReader, FooServiceB>();
services.AddScoped<IFooReader, FooServiceC>();
services.AddScoped<IFooService, FooService>(); //assuming stateless
It looks like the native .net-core DI container does not support injection of IAsyncEnumerable
, any way to resolve this?
Upvotes: 2
Views: 2361
Reputation: 1249
Use of IEnumerable instead of IAsncEnumerable should resolve this issue.
public class FooService : IFooService
{
private readonly IEnumerable<IFooReader> _foosReaders;
public PriceService(IEnumerable<IFooReader> foosReaders)
{
this._foosReaders = foosReaders;
}
}
Upvotes: 3