Warm tip: This article is reproduced from stackoverflow.com, please click
asp.net-core iis autofac

Not able to host .Net Core 3.1 Web API on IIS

发布于 2020-03-29 21:03:07

I upgraded my .net core 2.2 web api to .net core 3.1. I am now able to run it from console using dotnet myapi.dll, however when I try to host it on ISS, I get following exception.

I have installed AspNetCore Module V2 and this module is throwing exception and I am not able to understand the missing part.

Autofac.Core.DependencyResolutionException: An exception was thrown while activating ?:Microsoft.Extensions.Hosting.IHostedService[] -> Microsoft.AspNetCore.Hosting.GenericWebHostService -> Microsoft.AspNetCore.Server.IIS.Core.IISHttpServer. ---> Autofac.Core.DependencyResolutionException: An exception was thrown while invoking the constructor '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

Following is my Program.cs

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

Following is my 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();
        });
    }

Call to AddNLog() is our customer implementation to override .net core provided logger.

I use paket instead of nuget so following is my paket.dependencies file.

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

I explored a bit and didn't find much help. I created a new asp.net core web api and I see that template comes with more or less similar config. What am I missing here?

Questioner
Pradeep
Viewed
75
Tratcher 2020-02-11 00:07

Remove the Microsoft.AspNetCore.* nuget packages, those no longer exist in 3.0 so you're pulling in 2.x versions. https://docs.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.1&tabs=visual-studio#remove-obsolete-package-references