5

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.

15
  • yes i have installed it Commented Jun 13, 2019 at 9:21
  • Yes this is what i did, but it gives me an error when i run it using IIS Express in Visual Studio too. Commented Jun 13, 2019 at 9:31
  • Which version of dotnet are you using? my app is 2.1 and I just need "WebHost .CreateDefaultBuilder(args).UseStartup<Startup>();" to get it to work both in Kestrel and behind IIS. The default builder should be setting enough things for IIS hosting. Commented Jun 13, 2019 at 9:34
  • 1
    I'm using 2.2, other Uses are needed for run it in IIS and Kestrel and btw i run the app using ISS Express yesterday and worked but now it does not work anymore Commented Jun 13, 2019 at 9:36
  • 2
    Just remove lines .UseKestrel() and .UseIISIntegration() and everything should work fine Commented Jun 13, 2019 at 10:35

2 Answers 2

4

I recommend you to call ConfigureKestrel instead of UseKestrel:

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?view=aspnetcore-2.2#how-to-use-kestrel-in-aspnet-core-apps

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .ConfigureKestrel((context, options) =>
        {
            // Set properties and call methods on options
        });
Sign up to request clarification or add additional context in comments.

1 Comment

And the problem isn't Kestrel but IIS
1

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.

5 Comments

So updated answer works on IIS but still not working on ISS Express
@RiccardoRaffini Is it the same error as mentioned in the original post, that you receive with IIS Express after removing .UseKestrel()? And can you please post your ConfigureServices() from your Startup-class in the original post? The reason can also be spelling errors in appsettings.json and/or launchsettings.json
I have added strtup file
@RiccardoRaffini my last guess is to put app.UseMvc(); to the very last line of Configure method.
Does not seem to work... but in chrome i have ERR_CONNECTION_RESET error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.