- 已关闭。**此问题需要debugging details。当前不接受答案。
编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
3天前关闭。
Improve this question
我有一个. NET Core 7 API。当我在本地启动它进行调试时,我收到以下错误:
无效操作异常:尝试激活""时无法解析类型""的服务。
使用以下堆栈跟踪:
"""无效操作异常:尝试激活""时无法解析类型""的服务。
Microsoft.Extensions.DependencyInjection.ActivatorUtilities+ConstructorMatcher.CreateInstance(IServiceProvider provider)
Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, object[] parameters)
Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.UseStartup(Type startupType, HostBuilderContext context, IServiceCollection services, object instance)
Microsoft.AspNetCore.Hosting.GenericWebHostBuilder+<>c__DisplayClass14_0.<UseStartup>b__0(HostBuilderContext context, IServiceCollection services)
Microsoft.Extensions.Hosting.HostBuilder.InitializeServiceProvider()
Microsoft.Extensions.Hosting.HostBuilder.Build()
KillerGaming.ServerAutomation.API.Program.Main(string[] args) in Program.cs
CreateHostBuilder(args).Build().Run();
'''
我知道这与DI有关,但我不太确定需要在代码中更改哪些内容来修复此问题?
编辑:
我的CreateHostBuilder看起来像这样:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog((context, services, logConfig) =>
logConfig.ReadFrom.Configuration(context.Configuration))
.ConfigureWebHostDefaults(webBuilder => {
webBuilder.UseStartup<Startup>();
});
}
在启动时,服务引用如下:
public Startup(IConfiguration configuration, IWebHostEnvironment env, IServiceProvider serviceProvider, ILogger<Startup> logger)
{
HostingEnvironment = env;
Configuration = configuration;
_serviceProvider = serviceProvider;
_logger = logger;
1条答案
按热度按时间erhoui1w1#
从docs:
使用通用主机(
IHostBuilder
)时,只能将以下服务类型注入启动构造函数:IWebHostEnvironment
IHostEnvironment
IConfiguration
您需要删除不允许的参数。