【发布时间】:2019-06-13 20:48:32
【问题描述】:
我已经阅读了许多关于同一主题的其他 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;
}
}
我在这里错过了什么?
【问题讨论】:
标签: c# dependency-injection blazor