无法在IIS上托管.Net Core 3.1 Web API

3

我将我的 .net core 2.2 web api 升级到了 .net core 3.1。现在我可以使用 dotnet myapi.dll 命令在控制台中运行它,但是当我尝试在 ISS 上托管它时,出现以下异常。

我已经安装了 AspNetCore Module V2 模块,但是该模块抛出异常,我无法理解缺失的部分。

Autofac.Core.DependencyResolutionException: 在激活 ?:Microsoft.Extensions.Hosting.IHostedService[] -> Microsoft.AspNetCore.Hosting.GenericWebHostService -> Microsoft.AspNetCore.Server.IIS.Core.IISHttpServer 时抛出异常。 ---> Autofac.Core.DependencyResolutionException: 在调用构造函数 'Void .ctor(Microsoft.AspNetCore.Server.IIS.Core.IISNativeApplication, Microsoft.Extensions.Hosting.IHostApplicationLifetime, Microsoft.AspNetCore.Authentication.IAuthenticationSchemeProvider, Microsoft.Extensions.Options.IOptions``1[Microsoft.AspNetCore.Builder.IISServerOptions], Microsoft.Extensions.Logging.ILogger

以下是我的 Program.cs 文件。

Host.CreateDefaultBuilder(args)
                .UseServiceProviderFactory(new AutofacServiceProviderFactory())
                .ConfigureWebHostDefaults(webHostBuilder =>
                {
                    webHostBuilder
                        .UseStartup<Startup>();
                })
                .Build()
                .Run();

下面是我的Startup.cs
public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }


    public ILifetimeScope AutofacContainer { get; private set; }

    public void ConfigureContainer(ContainerBuilder builder)
    {
        // Register your own things directly with Autofac, like:
        ...couple of my Ioc registrations
    }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();
        services.AddNLog();
        services.Configure<IISServerOptions>(options =>
        {
            options.AutomaticAuthentication = false;
        });
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseRouting();

        app.UseEndpoints(endpoints => {
            endpoints.MapControllers();
        });
    }

AddNLog() 的调用是我们客户自己实现的重写 .net core 提供的日志记录器。

我使用 paket 而不是 nuget,因此以下是我的 paket.dependencies 文件。

references: strict
source https://api.nuget.org/v3/index.json
framework: auto-detect

nuget Autofac 5.0.0
nuget Autofac.Extensions.DependencyInjection 6.0.0
nuget Autofac.Extras.DynamicProxy 4.5.0
nuget AutoFixture 4.5.0
nuget AutoFixture.AutoMoq 4.5.0
nuget AutoFixture.Xunit2 4.5.0
nuget AutoMapper >= 8.0.0
nuget AutoMapper.Extensions.Microsoft.DependencyInjection
nuget bootstrap 3.3.7 prerelease
nuget Castle.Core 4.3.1 prerelease
nuget Common.Logging 3.4.1
nuget Common.Logging.Core 3.4.1
nuget DnsClient 1.2
clitool dotnet-xunit 2.3.1
nuget Fare 2.1.1
nuget FileHelpers 3.3.0
nuget FluentAssertions 5.4.1
nuget Flurl 2.8.2
nuget JetBrains.Annotations 2019.1.3
nuget Microsoft.AspNet.Cors 5.2.7
nuget Microsoft.AspNet.WebApi 5.2.7
nuget Microsoft.AspNet.WebApi.Client 5.2.7
nuget Microsoft.AspNet.WebApi.Core 5.2.7
nuget Microsoft.AspNet.WebApi.Cors 5.2.7
nuget Microsoft.AspNet.WebApi.WebHost 5.2.7
nuget Microsoft.Azure.AppService.ApiApps.Service 0.9.64
nuget Microsoft.CodeDom.Providers.DotNetCompilerPlatform 2.0.0
nuget Microsoft.DotNet.PlatformAbstractions >= 3.1.1
nuget Microsoft.Extensions.Caching.Abstractions >= 3.1.1
nuget Microsoft.Extensions.Caching.Memory >= 3.1.1
nuget Microsoft.Extensions.Configuration >= 3.1.1
nuget Microsoft.Extensions.Configuration.Abstractions >= 3.1.1
nuget Microsoft.Extensions.Configuration.Binder >= 3.1.1
nuget Microsoft.Extensions.Configuration.EnvironmentVariables >= 3.1.1
nuget Microsoft.Extensions.Configuration.FileExtensions >= 3.1.1
nuget Microsoft.Extensions.Configuration.Json >= 3.1.1
nuget Microsoft.Extensions.DependencyInjection >= 3.1.1
nuget Microsoft.Extensions.DependencyInjection.Abstractions >= 3.1.1
nuget Microsoft.Extensions.DependencyModel >= 3.1.1
nuget Microsoft.Extensions.FileProviders.Abstractions >= 3.1.1
nuget Microsoft.Extensions.FileProviders.Physical >= 3.1.1
nuget Microsoft.Extensions.FileSystemGlobbing >= 3.1.1
nuget Microsoft.Extensions.Logging >= 3.1.1
nuget Microsoft.Extensions.Logging.Abstractions >= 3.1.1
nuget Microsoft.Extensions.Logging.Configuration >= 3.1.1
nuget Microsoft.Extensions.Logging.Console >= 3.1.1
nuget Microsoft.Extensions.Logging.Debug >= 3.1.1
nuget Microsoft.Extensions.Options >= 3.1.1
nuget Microsoft.Extensions.Primitives >= 3.1.1
nuget Microsoft.NET.Test.Sdk 16.5.0
nuget Microsoft.NETCore.DotNetAppHost 3.1.1
nuget Microsoft.NETCore.DotNetHostPolicy 3.1.1
nuget Microsoft.NETCore.Platforms 3.1
nuget Microsoft.Web.Infrastructure 1.0.0
nuget Microsoft.Win32.Primitives 4.3.0
nuget MongoDB.Bson 2.10.1
nuget MongoDB.Bson.signed 2.10.1
nuget MongoDB.Driver 2.10.1
nuget MongoDB.Driver.Core 2.10.1
nuget Moq 4.10.1
nuget Moq.AutoMock 1.1.0.96
nuget MSBuild.Microsoft.VisualStudio.Web.targets 14.0.0.3
nuget NETStandard.Library 2.0.3
nuget Newtonsoft.Json 12.0.3
nuget NLog 4.6.8
nuget NodaTime 3.0.0-beta01
nuget NodaTime.Testing 3.0.0-beta01
nuget Polly 7.2.0
nuget protobuf-portable-net 2.0.0.668
nuget Purplelight.Framework.Autofac.Integration.WebApi 0.2.2
nuget Remotion.Linq 2.2.0
nuget RestSharp 106.10.1

nuget ShellProgressBar >= 4.2.0
nuget SqlBulkTools.NetStandard 2.1.18
nuget SSH.NET 2016.0.0
nuget StackExchange.Redis 2.0.519
nuget StackExchange.Redis.Extensions.Jil 4.0.5
nuget Swashbuckle 5.6.0
nuget Swashbuckle.Core 5.6.0
nuget System.AppContext 4.3.0
nuget System.Buffers 4.5.0
nuget System.Collections 4.3.0
nuget System.Collections.Concurrent 4.3.0
nuget System.Collections.Immutable 1.7.0
nuget System.ComponentModel 4.3.0
nuget System.ComponentModel.Annotations 4.7.0
nuget System.Configuration.ConfigurationManager 4.7.0
nuget System.Console 4.3.1
nuget System.Diagnostics.Debug 4.3.0
nuget System.Diagnostics.DiagnosticSource 4.7.0
nuget System.Diagnostics.Tools 4.3.0
nuget System.Diagnostics.Tracing 4.3.0
nuget System.Globalization 4.3.0
nuget System.Globalization.Calendars 4.3.0
nuget System.Interactive.Async 3.2.0
nuget System.IO 4.3.0 prerelease
nuget System.IO.Compression 4.3.0
nuget System.IO.Compression.ZipFile 4.3.0
nuget System.IO.FileSystem 4.3.0
nuget System.IO.FileSystem.Primitives 4.3.0
nuget System.Linq 4.3.0
nuget System.Linq.Expressions 4.3.0
nuget System.Memory >= 4.5.2
nuget System.Net.Primitives 4.3.0
nuget System.Net.Sockets 4.3.0
nuget System.ObjectModel 4.3.0
nuget System.Reflection 4.3.0
nuget System.Reflection.Extensions 4.3.0
nuget System.Reflection.Primitives 4.3.0
nuget System.Resources.ResourceManager 4.3.0
nuget System.Runtime 4.3.0 prerelease
nuget System.Runtime.Caching 4.5.0
nuget System.Runtime.CompilerServices.Unsafe >= 4.5.1
nuget System.Runtime.Extensions 4.3.0
nuget System.Runtime.Handles 4.3.0
nuget System.Runtime.Numerics 4.3.0
nuget System.Security.AccessControl 4.7.0
nuget System.Security.Permissions 4.7.0
nuget System.Security.Principal.Windows 4.7.0
nuget System.ServiceModel.Duplex >= 4.7.0
nuget System.ServiceModel.Http >= 4.7.0
nuget System.ServiceModel.Primitives >= 4.7.0
nuget System.ServiceModel.NetTcp >= 4.7.0
nuget System.ServiceModel.Security >= 4.7.0
nuget System.Text.Encoding >= 4.3.0
nuget System.Text.Encoding.Extensions >= 4.3.0
nuget System.Text.RegularExpressions >= 4.3.0
nuget System.Threading >= 4.3.0
nuget System.Threading.Tasks >= 4.3.0
nuget System.Threading.Timer >= 4.3.0
nuget System.ValueTuple >= 4.5.0
nuget System.Xml.ReaderWriter >= 4.3.0
nuget System.Xml.XDocument >= 4.3.0
nuget WebActivatorEx 2.2.0
nuget WebGrease 1.6.0
nuget xBehave
nuget xunit 2.4.0
nuget xunit.abstractions 2.0.2
nuget xunit.analyzers 0.10.0
nuget xunit.assert 2.4.0
nuget xunit.core 2.4.0
nuget xunit.extensibility.core 2.4.0
nuget xunit.extensibility.execution 2.4.0
nuget xunit.runner.console 2.4.0
nuget xunit.runner.visualstudio 2.4.0
nuget ZeroFormatter 1.6.4
nuget NLog.Web.AspNetCore
nuget NLog.Extensions.Logging
nuget Dapper 2.0.30
nuget Dapper.Contrib 2.0.30
nuget IdentityModel 4.1.1
nuget System.IdentityModel.Tokens.Jwt 5.6.0
nuget Microsoft.IdentityModel.Tokens 5.6.0
nuget Microsoft.IdentityModel.JsonWebTokens 5.6.0
nuget Microsoft.EntityFrameworkCore 3.1.1
nuget Microsoft.EntityFrameworkCore.Analyzers 3.1.1
nuget Microsoft.EntityFrameworkCore.InMemory 3.1.1
nuget Microsoft.EntityFrameworkCore.SqlServer 3.1.1
nuget System.Drawing.Common 4.7.0
nuget NEventStore 7.0.0
nuget NEventStore.Persistence.Sql 7.2.0
nuget NEventStore.Serialization.Json 7.0.0
nuget NEventStore.PollingClient 7.0.0
nuget Microsoft.AspNetCore.Hosting
nuget Microsoft.AspNetCore.Mvc
nuget Microsoft.AspNetCore.Mvc.Core
nuget Microsoft.AspNetCore.Mvc.Formatters.Json
nuget Microsoft.AspNetCore.Diagnostics
nuget Microsoft.AspNetCore.HttpsPolicy
nuget Microsoft.AspNetCore

我进行了一些探索,没有找到很多帮助。我创建了一个新的asp.net core web api,发现该模板具有更或多或少相似的配置。这里我错过了什么?


这是一个Autofac异常,因此必须使用它的标签。 - Lex Li
@L.Trabacchin 异常是 Autofac.Core.DependencyResolutionException。它出现的原因是在设置此 API 时出现了问题。我无法弄清楚问题出在哪里,因为 outprocess(kestrel)托管正常工作,但 inprocess 托管却不行。 - Pradeep
这是一个重新抛出的异常,它明确表示在调用“Microsoft.AspNetCore.Server.IIS.Core.IISHttpServer”的构造函数时捕获了一个异常,我们需要那个内部异常。 - L.Trabacchin
移除 Microsoft.AspNetCore.* nuget 包,因为它们在 3.0 中已经不存在,所以你正在使用 2.x 版本。https://learn.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.1&tabs=visual-studio#remove-obsolete-package-references - Tratcher
你的 web.config 长什么样子? - salli
显示剩余5条评论
1个回答

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接