What does UseIIS() method actually do?

bhavna 106 Reputation points
2022-02-17T16:39:35.683+00:00

If it is called from within the main() method then how does it host the app inside the w3wp? does this mean the console App and Middlewares are considered two different things and UseIIS() just hosts the middlewares inside w3wp?

Developer technologies ASP.NET ASP.NET Core
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2022-02-17T23:21:45.477+00:00

    also the AspCoreModule exports entry points callable by UseIIS() via this handy code which get a handle to the aspnetcore module native code (which is available if aspnetcore model is hosting):

        [DllImport("kernel32.dll")] 
        private static extern IntPtr GetModuleHandle(string lpModuleName); 
    
        public static bool IsAspNetCoreModuleLoaded() 
        { 
            return GetModuleHandle(AspNetCoreModuleDll) != IntPtr.Zero; 
        } 
    

    https://github.com/dotnet/aspnetcore/blob/c85baf8db0c72ae8e68643029d514b2e737c9fae/src/Servers/IIS/IIS/src/NativeMethods.cs

    you should read the docs. UseIIS checks if hosted in process by aspnetcore module, and loads via DI an IServer that makes calls to aspnetcore module to stream request and response. If an IServer is not defines, the builder will inject an IServer based on Kestrel.

    the IServer is just responsible for the mapping the input and response streams to the actual host. The AspNetCore core pipeline is independent of the IServer. so its:

    <external source> --request--> <IServer> --request-> <AspNet Pipeline> --response-> <IServer> --response--> <external source>

    where external source is some stream supported by the IServer. could be network, file, service bus, etc, whatever the IServer was designed to interface to.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Sreeju Nair 12,666 Reputation points
    2022-02-17T17:12:55.32+00:00

    the UseIIS method checks the OS (whether it is windows), and check whether CLR is loaded, if yes, it adds the neccessary services to run the application under IIS.

    See the source code for UseIIS, refer : https://github.com/dotnet/aspnetcore/blob/eb080a295040da182a1b1e5e1f74379ca58a085d/src/Servers/IIS/IIS/src/WebHostBuilderIISExtensions.cs#L23

    public static IWebHostBuilder UseIIS(this IWebHostBuilder hostBuilder)
        {
            if (hostBuilder == null)
            {
                throw new ArgumentNullException(nameof(hostBuilder));
            }
    
            // Check if in process
            if (OperatingSystem.IsWindows() && NativeMethods.IsAspNetCoreModuleLoaded())
            {
                var iisConfigData = NativeMethods.HttpGetApplicationProperties();
                // Trim trailing slash to be consistent with other servers
                var contentRoot = iisConfigData.pwzFullApplicationPath.TrimEnd(Path.DirectorySeparatorChar);
                hostBuilder.UseContentRoot(contentRoot);
                return hostBuilder.ConfigureServices(
                    services =>
                    {
                        services.AddSingleton(new IISNativeApplication(new NativeSafeHandle(iisConfigData.pNativeApplication)));
                        services.AddSingleton<IServer, IISHttpServer>();
                        services.AddTransient<IISServerAuthenticationHandlerInternal>();
                        services.AddSingleton<IStartupFilter>(new IISServerSetupFilter(iisConfigData.pwzVirtualApplicationPath));
                        services.AddAuthenticationCore();
                        services.AddSingleton<IServerIntegratedAuth>(_ => new ServerIntegratedAuth()
                        {
                            IsEnabled = iisConfigData.fWindowsAuthEnabled || iisConfigData.fBasicAuthEnabled,
                            AuthenticationScheme = IISServerDefaults.AuthenticationScheme
                        });
                        services.Configure<IISServerOptions>(
                            options =>
                            {
                                options.ServerAddresses = iisConfigData.pwzBindings.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                                options.ForwardWindowsAuthentication = iisConfigData.fWindowsAuthEnabled || iisConfigData.fBasicAuthEnabled;
                                options.MaxRequestBodySize = iisConfigData.maxRequestBodySize;
                                options.IisMaxRequestSizeLimit = iisConfigData.maxRequestBodySize;
                            }
                        );
                    });
            }
    
            return hostBuilder;
        }
    }
    

    Hope this helps

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.