我有一个ASP.Net Core应用程序,可在IIS Express下以开发人员模式运行。我已经发布到Windows 10 IIS网站,但是出现以下错误:
An error occurred while starting the application.
ArgumentNullException: Value cannot be null.
Parameter name: implementationInstance
Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddSingleton<TService>(IServiceCollection services, TService implementationInstance)
ArgumentNullException: Value cannot be null. Parameter name: implementationInstance
Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddSingleton<TService>(IServiceCollection services, TService implementationInstance)
NSG.WebSrv.Startup.ConfigureServices(IServiceCollection services) in Startup.cs
-
// services.Configure<AuthSettings>(Configuration.GetSection("AuthSettings"));
AuthSettings _authSettings = new AuthSettings();
_authSettings = Options.Create<AuthSettings>(
Configuration.GetSection("AuthSettings").Get<AuthSettings>()).Value;
services.AddSingleton<AuthSettings>(_authSettings);
//
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
// Add MediatR from MediatR.Extensions.Microsoft.DependencyInjection package
Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services)
Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()
Microsoft.AspNetCore.Hosting.Internal.WebHost.Initialize()
Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
Show raw exception details
.NET Core 4.6.28207.03 X64 v4.0.0.0 | Microsoft.AspNetCore.Hosting version 2.2.7-servicing-10089 | Microsoft Windows 10.0.18362 | Need help?
错误发生在以下行:
services.Configure<CookiePolicyOptions>(options =>
使用无托管代码配置IIS站点App-Pool。
已部署/翻译的 web.config 如下:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<!-- For more info see https://go.microsoft.com/fwlink/?linkid=838655 -->
<!-- -->
<system.webServer>
<handlers>
<remove name="aspNetCore" />
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<!-- https://scottsauber.com/2017/04/10/how-to-troubleshoot-an-error-occurred-while-starting-the-application-in-asp-net-core-on-iis/ -->
<aspNetCore processPath="dotnet" arguments=".\NSG.WebSrv.dll" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" hostingModel="InProcess">
<environmentVariables>
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
</environmentVariables>
</aspNetCore>
</system.webServer>
<!-- -->
</configuration>
<!--ProjectGuid: bcfdd09f-8928-4e80-a804-82728c9bbe16-->
如果我注释掉它出错的代码块,那么它将在下一行代码中出错。
启动的开始如下:
public class Startup
{
//
public Startup(IConfiguration configuration, ILoggerFactory loggerFactory)
{
Configuration = configuration;
LoggerFactory = loggerFactory;
}
//
/// <summary>
/// IConfiguration, common configuration provider.
/// </summary>
public IConfiguration Configuration { get; }
//
/// <summary>
/// The logger factory
/// </summary>
public static ILoggerFactory LoggerFactory = null;
//
/// <summary>
/// The configured logger (console).
/// </summary>
public static ILogger<ConsoleLoggerProvider> AppLogger = null;
//
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
//
services.Configure<IISServerOptions>(options =>
{
// do not use windows authentication
options.AutomaticAuthentication = false;
});
// Configure logging
// Unable to resolve service for type 'Microsoft.Extensions.Logging.ILogger' while attempting to activate 'NSG.WebSrv.Infrastructure.Notification.NotificationService'
services.AddLogging(builder => builder
.AddConfiguration(Configuration.GetSection("Logging"))
.AddConsole()
.AddDebug()
);
AppLogger = LoggerFactory.CreateLogger<ConsoleLoggerProvider>();
services.TryAdd(ServiceDescriptor.Singleton<ILoggerFactory, LoggerFactory>());
services.TryAdd(ServiceDescriptor.Singleton(typeof(ILogger<>), typeof(Logger<>)));
// Add and configure email/notification services
services.Configure<MimeKit.NSG.EmailSettings>(Configuration.GetSection("EmailSettings"));
services.Configure<ServicesSettings>(Configuration.GetSection("ServicesSettings"));
services.Configure<ApplicationSettings>(Configuration.GetSection("ApplicationSettings"));
// services.Configure<AuthSettings>(Configuration.GetSection("AuthSettings"));
AuthSettings _authSettings = new AuthSettings();
_authSettings = Options.Create<AuthSettings>(
Configuration.GetSection("AuthSettings").Get<AuthSettings>()).Value;
services.AddSingleton<AuthSettings>(_authSettings);
//
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
...
}
...
}
完整的解决方案在GitHub上。
答案 0 :(得分:0)
您是否已为IIS安装了.NetCore HostingBundle?
答案 1 :(得分:0)
我创建了一个新的解决方案并将其发布,并且模板可以正常工作。然后,我添加了我的 Program.cs 和一些我的 Startup.cs 文件,但失败了。它在 _authSettings 上失败,因为我没有安装正确的 appsettings.json ,但它基本上指出了以前的任何失败都可能导致此问题的事实。然后,当我对日志一无所知时,我发现了一段很早就添加的代码,删除该行,现在可以了:
services.TryAdd(ServiceDescriptor.Singleton(typeof(ILogger<>), typeof(Logger<>)));
这在 IISExpress 下没有失败,但是在 w3wp 下确实引起了问题。
注意:这是我的第一个.Net Core Web应用程序。