Cannot run Asp Net Core API on IIS

Viewed 8908

I have an asp net core web api.

I can run it using dotnet command and it starts on https://localhost:5001/ (as it should) and everythings work fine.

But when i try to launch the api using IIS Express or using IIS it won't reach the page and gives me error 500.30:

Browser error

I have enabled logs in web.config and this is the result:

Application startup exception: System.InvalidOperationException: Application is running inside IIS process but is not configured to use IIS server. at Microsoft.AspNetCore.Server.IIS.Core.IISServerSetupFilter.<>c__DisplayClass2_0.b__0(IApplicationBuilder app) at Microsoft.AspNetCore.HostFilteringStartupFilter.<>c__DisplayClass0_0.b__0(IApplicationBuilder app) at Microsoft.AspNetCore.Hosting.Internal.AutoRequestServicesStartupFilter.<>c__DisplayClass0_0.b__0(IApplicationBuilder builder) at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication() crit: Microsoft.AspNetCore.Hosting.Internal.WebHost[6] Application startup exception System.InvalidOperationException: Application is running inside IIS process but is not configured to use IIS server. at Microsoft.AspNetCore.Server.IIS.Core.IISServerSetupFilter.<>c__DisplayClass2_0.b__0(IApplicationBuilder app) at Microsoft.AspNetCore.HostFilteringStartupFilter.<>c__DisplayClass0_0.b__0(IApplicationBuilder app) at Microsoft.AspNetCore.Hosting.Internal.AutoRequestServicesStartupFilter.<>c__DisplayClass0_0.b__0(IApplicationBuilder builder) at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication() Hosting environment: Production Content root path: D:_APICore Now listening on: http://localhost:5000 Application started. Press Ctrl+C to shut down.

Then i have an error in Event viewer too (it is in Italian): Event viewer error

Last, this is the Program.cs:

public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>();

Hope all these informations helps. Thanks for any help.

UPDATE

StartUp.cs:

public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

        //Authorization settings
        services.AddCors(c => c.AddPolicy("Permissions", builder => {
            builder.AllowAnyOrigin()
                   .AllowAnyMethod()
                   .AllowAnyHeader();
        }));
        services.Configure<MvcOptions>(options =>
        {
            options.Filters.Add(new CorsAuthorizationFilterFactory("Permissions"));
        });

        services.Configure<Values>(Configuration.GetSection("StoredValues"));

        //JWT setting configuration
        var appSettings = Configuration.GetSection("StoredValues");
        var key = appSettings.GetValue(typeof(string), "JWTSecret").ToString();
        services.AddAuthentication(x =>
        {
            x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
            x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
        })
        .AddJwtBearer(x =>
        {
            x.RequireHttpsMetadata = false;
            x.SaveToken = true;
            x.TokenValidationParameters = new TokenValidationParameters
            {
                ValidateIssuerSigningKey = true,
                IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(key)),
                ValidateIssuer = false,
                ValidateAudience = false
            };
        });

    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        loggerFactory.AddLog4Net();

        app.UseHttpsRedirection();
        app.UseMvc();
        app.UseCors("Permissions");
        app.UseAuthentication();
        app.UseDefaultFiles();
        app.UseStaticFiles();
    }

UPDATE 2

I have found some older version of the program and these don't work either.

So now i'm wondering if this my problem or something else ???

Maybe some update ? I don't really know.

2 Answers

Try to remove .UseKestrel()

Reference/more info: Host RestAPI inside IIS - follow the link provided in the answer. Be aware that you might need to configure further properties, which are described in that link.

Related