Home > Software engineering >  How to make IIS ignore one of two instances of ASP Net Core Host in the application?
How to make IIS ignore one of two instances of ASP Net Core Host in the application?

Time:04-20

I have two instances of ASP Net Core Host on two different ports in a single application:

  1. Normal WebHost serving requests for external clients.
  2. Internal gRPC Host for communication with other instances and components of the application on an internal network.

This works as intended when the application is self-hosted. However when the application is hosted by IIS, all of the requests end up being handled by the second host and clients who attempt to communicate with the first host always receive 404. gRPC communication still works correctly though (it appears the second host is the only one functional).

This whole situation came up as I tried to migrate the application from the native gRPC.Core server to the newer Grpc.AspNetCore package. There are many reasons we need to keep the two hosts separate - they are owned by entirely different sections of the application, have different lifecycles and have wildly different policies.

Is it possible to make IIS ignore the gRPC Host entirely? It doesn't need the features of IIS at all - it would ideally continue be fully controlled by the back-end as it was when it used the Grpc.Core server.

CodePudding user response:

I eventually managed to figure this out - Kestrel hosts don't get registered into IIS on their own, but the ConfigureWebHostDefaults extension method used in most all examples does so, as mentioned in the documentation:

The ConfigureWebHostDefaults method:

Loads host configuration from environment variables prefixed with ASPNETCORE_.

Sets Kestrel server as the web server and configures it using the app's hosting configuration providers. For the Kestrel server's default options, see Configure options for the ASP.NET Core Kestrel web server.

Adds Host Filtering middleware. Adds Forwarded Headers middleware if ASPNETCORE_FORWARDEDHEADERS_ENABLED equals true.

Enables IIS integration. For the IIS default options, see Host ASP.NET Core on Windows with IIS.

The solution is to use ConfigureWebHost extension methods instead and to do the necessary initialization manually - I had to call UseKestrel and to configure logging manually. I advise to see the implementation of ConfigureWebHostDefaults to see what exactly it initializes.

  • Related