[英]How to cast object of type 'X.PagedList.StaticPagedList` to type 'System.Collections.Generic.IAsyncEnumerable`
[英]Unable to resolve service for type System.Collections.Generic.IAsyncEnumerable
我有這個例外:
System.InvalidOperationException:“嘗試激活“MyProject.Services.FooService”時,無法解析類型“System.Collections.Generic.IAsyncEnumerable`1[MyProject.Interfaces.IFooReader]”的服務。”
在嘗試使用var fooService = provider.GetRequiredService<IFooService>();
解決IFooService
時 .
這是FooService
class:
public class FooService : IFooService
{
private readonly IAsyncEnumerable<IFooReader> _foosReaders;
public PriceService(IAsyncEnumerable<IFooReader> foosReaders)
{
this._foosReaders = foosReaders;
}
}
這是 DI 容器:
services.AddScoped<IFooReader, FooServiceA>();
services.AddScoped<IFooReader, FooServiceB>();
services.AddScoped<IFooReader, FooServiceC>();
services.AddScoped<IFooService, FooService>(); //assuming stateless
看起來本機 .net-core DI 容器不支持注入IAsyncEnumerable
,有什么辦法解決這個問題嗎?
使用IEnumerable而不是IAsncEnumerable應該可以解決此問題。
public class FooService : IFooService
{
private readonly IEnumerable<IFooReader> _foosReaders;
public PriceService(IEnumerable<IFooReader> foosReaders)
{
this._foosReaders = foosReaders;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.