c# - Blazor:尝试激活 yyy 时无法解析类型 xx 的服务
问题描述
我已经阅读了许多关于同一主题的其他 SO 问题,但我找到的答案都不适用于我的案例。
我已经在我的 Startup.cs 中成功添加了 4 个服务,并且之前运行良好。然后我添加了第 5 个,现在我意识到有些东西坏了 - 没有任何服务工作。即使我完全删除了第 5 个,其他的现在也因相同的错误而损坏。
尝试激活时无法解析类型 xx 的服务
这是我的 Startup.cs ConfigureServices.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddStorage();
services.AddSingleton<IMyLocalStorage, MyLocalStorage>();
services.AddSingleton<IFrontEndService, FrontEndService>();
services.AddSingleton<ISystemProvider, SystemProviderService>();
services.AddSingleton<IAuthenticationService, AuthenticationService>();
}
这是我注意到错误的最后一个 AuthenticationService,但即使是以前工作的旧服务现在也失败了。
public interface IAuthenticationService
{
// ...
}
public class AuthenticationService : IAuthenticationService
{
private readonly FrontEndService frontEndService;
private readonly MyLocalStorage myLocalStorage;
public AuthenticationService(FrontEndService frontEndService, MyLocalStorage myLocalStorage)
{
this.frontEndService = frontEndService;
this.myLocalStorage = myLocalStorage;
}
// ...
}
服务很简单;一个接口,该接口的一个实现,然后在 Startup.cs 中添加。我无法弄清楚它为什么停止工作。
因此,如果我删除 IAuthenticationService,则错误会显示在 FrontEndService 中,然后抱怨 MyLocalStorage:
public interface IFrontEndService
{
Task<T> GetAsync<T>(string requestUri);
}
public class FrontEndService : IFrontEndService
{
private readonly HttpClient client;
private readonly MyLocalStorage myLocalStorage;
public FrontEndService(HttpClient client, MyLocalStorage myLocalStorage)
{
// ...
}
}
和
public class MyLocalStorage : IMyLocalStorage
{
public MyLocalStorage(LocalStorage storage)
{
this.storage = storage;
}
}
我在这里想念什么?
解决方案
当您调用IServiceCollection
诸如 之类的方法时.AddSingleton<IFrontEndService, FrontEndService>()
,您是在对容器说:“每当您看到IFrontEndService
依赖项时,就注入 . 的实例FrontEndService
。” 现在,如果你看看你的AuthenticationService
:
public class AuthenticationService : IAuthenticationService
{
private readonly FrontEndService frontEndService;
private readonly MyLocalStorage myLocalStorage;
public AuthenticationService(FrontEndService frontEndService, MyLocalStorage myLocalStorage)
{
this.frontEndService = frontEndService;
this.myLocalStorage = myLocalStorage;
}
// ...
}
请注意您是如何传递 and 的依赖项的FrontEndService
,MyLocalStorage
而不是您注册的接口。这意味着容器无法识别它们,因此它不知道如何实现依赖图。
您需要更改服务以依赖接口,因为这些是您在容器中注册的内容:
public class AuthenticationService : IAuthenticationService
{
private readonly IFrontEndService frontEndService;
private readonly IMyLocalStorage myLocalStorage;
public AuthenticationService(IFrontEndService frontEndService, IMyLocalStorage myLocalStorage)
{
this.frontEndService = frontEndService;
this.myLocalStorage = myLocalStorage;
}
// ...
}
推荐阅读
- javascript - 在 HTML 中查找 React 组件的位置
- r - 将行乘以 R 中的某列
- python - 我的方法是蛮力搜索还是线性搜索?
- javascript - 响应式菜单未在 IE 11 上打开
- javascript - ReactDOM.unstable_renderSubtreeIntoContainer() 和 ReactDOM.createPortal() 方法有什么区别?
- java - 带毕加索的倒带按钮
- facebook - 有没有办法让谁喜欢我在 Facebook 上的帖子,就像我们在 YouTube 上一样,作为 API?
- angular7 - 如何将 Angular 版本从 7 升级到 11,是否建议升级?
- javascript - 如何在 html 中使用外部 javascript 类?
- python - Python让我在类外访问受保护的实例属性