How Cai I Registered to Iis Again Under Logianalytuics

In this post I describe how to configure the URLs your awarding binds to when using the Kestrel or WebListener HTTP servers that come with ASP.NET Core. I'll besides encompass how to set the url when you are developing locally with Visual Studio using IIS Express, and how this relates to Kestrel and WebListener.

Background

In ASP.NET Core the hosting model has completely changed from ASP.NET 4.x. Previously your application was inextricably bound to IIS and System.Web, but in ASP.NET Cadre, your application is substantially merely a console app. You lot then create and configure your own lightweight HTTP server inside your application itself. This provides a larger range of hosting options than just hosting in IIS - in particular self-hosting in your own process.

ASP.Cyberspace Cadre comes with ii HTTP servers which you can plug directly in out of the box. If you accept been following the development of ASP.ENT Core at all, you will no doubt have heard of Kestrel, the new high performance, cantankerous-platform web server built specifically for ASP.NET Core.

The other server is WebListener. Kestrel go's all the attending, and probably rightly so given it's performance and cross-platform credentials, just spider web listener is actually more than fully featured, in detail regarding platform specific features such equally Windows-Authentication.

While you can directly cocky-host your ASP.Internet Core applications using Kestrel or WebListener, that's generally non the recommended approach when you come to deploy your application. Co-ordinate to the documentation:

If you lot intend to deploy your application on a Windows server, you should run IIS every bit a reverse proxy server that manages and proxies requests to Kestrel. If deploying on Linux, you should run a comparable reverse proxy server such as Apache or Nginx to proxy requests to Kestrel.

For self-hosting scenarios, such equally running in Service Fabric, we recommend using Kestrel without IIS. However, if y'all crave Windows Hallmark in a cocky-hosting scenario, you should choose WebListener.

Using a reverse-proxy mostly brings a whole raft of advantages. IIS can for case handle restarting your app if information technology crashes, it tin manage the SSL layer and certificates for yous, it tin filter requests, too equally handle hosting multiple applications on the same server.

Configuring Urls in Kestrel and WebListener

Now we have some background on where Kestrel and WebListener fit, we'll swoop into the mechanics of configuring the servers to listen at the correct urls. To exist articulate, we are talking about setting the url to which our awarding is bound e.g. http://localhost:5000, or http://myfancydomain:54321, which you would navigate to in your browser to view your app.

There is an first-class mail service from Ben Foster which shows the nigh common ways to configure Urls when yous are using Kestrel as the web server. It'southward worth noting that all these techniques apply to WebListener too.

These methods assume you are working with an ASP.NET Core awarding created using one of the standard templates. In one case created, there will be a file Plan.cs which contains the static void main() entry indicate for your application:

                      public            form            Program            {            public            static            void            Main            (            string            [            ]            args)            {            var            host            =            new            WebHostBuilder            (            )            .            UseContentRoot            (Directory.            GetCurrentDirectory            (            )            )            .            UseKestrel            (            )            .            UseIISIntegration            (            )            .                          UseStartup              <              Startup              >                        (            )            .            Build            (            )            ;            host.            Run            (            )            ;            }            }                  

It is the WebHostBuilder class which configures your application and HTTP server (in the example, Kestrel), and starts your app listening for requests with host.Run().

UseUrls()

The outset, and easiest, option to specify the binding URLs is to hard code them into the WebHostBuilder using AddUrls():

                      var            host            =            new            WebHostBuilder            (            )            .            UseKestrel            (            )            .            UseContentRoot            (Directory.            GetCurrentDirectory            (            )            )            .            UseUrls            (            "http://localhost:5100"            ,            "http://localhost:5101"            ,            "http://*:5102"            )            .            UseIISIntegration            (            )            .                          UseStartup              <              Startup              >                        (            )            .            Build            (            )            ;                  

Simply calculation this one line volition allow you to telephone call your application at any of the provided urls, and fifty-fifty at the wildcard host. However, hard-coding the urls never feels like a peculiarly clean or extensible solution. Luckily, you can likewise load the urls from an external configuration file, from environment variables, from control line arguments, or whatsoever source supported by the Configuration organization.

External file - hosting.json

To load from a file, create hosting.json, in the root of your project, and set the server.urls central as appropriate, separating each url with a semicolon. You tin really employ any name now, the name hosting.json is no longer assumed, but it's probably best to continue to use information technology by convention.

                      {            "server.urls"            :            "http://localhost:5100;http://localhost:5101;http://*:5102"            }                  

Update your WebHostBuilder to load hosting.json equally part of the initial configuration. It's important to set the base path so that the ConfigurationBuilder knows where to expect for your hosting.json file.

                      var            config            =            new            ConfigurationBuilder            (            )            .            SetBasePath            (Directory.            GetCurrentDirectory            (            )            )            .            AddJsonFile            (            "hosting.json"            ,            optional:            truthful            )            .            Build            (            )            ;            var            host            =            new            WebHostBuilder            (            )            .            UseConfiguration            (config)            .            UseContentRoot            (Directory.            GetCurrentDirectory            (            )            )            .            UseKestrel            (            )            .            UseIISIntegration            (            )            .                          UseStartup              <              Startup              >                        (            )            .            Build            (            )            ;                  

Annotation that the ConfigurationBuilder we apply here is distinct from the ConfigurationBuilder typically used to read appsettings.json etc as role of your Startup.cs configuration. It is an instance of the aforementioned class, simply the WebHostBuilder and app configuration are built separately - values from hosting.json will non pollute your appsettings.json configuration.

Command line arguments

As mentioned previously, and as shown in the previous snippet, you can configure your WebHostBuilder using any mechanism available to the ConfigurationBuilder. If you adopt to configure your awarding using control line arguments, add together the Microsoft.Extensions.Configuration.CommandLine package to your project.json and update your ConfigurationBuilder to the following:

                      var            config            =            new            ConfigurationBuilder            (            )            .            AddCommandLine            (args)            .            Build            (            )            ;                  

You can then specify the urls to utilize at runtime, again passing in the urls separated past semicolons:

          > dotnet run            --server.urls            "http://localhost:5100;http://localhost:5101;http://*:5102"                  

Environment Variables

At that place are a couple of subtleties to using environment variables to configure your WebHostBuilder. The showtime approach is to merely fix your own environment variables and load them equally usual with the ConfigurationBuilder. For example, using PowerShell y'all could set the variable "MYVALUES_SERVER.URLS" using:

                      [Surround]::SetEnvironmentVariable(            "MYVALUES_SERVER.URLS"            ,            "http://localhost:5100"            )                  

which can be loaded in our configuration builder using the prefix "MYVALUES_", allowing united states to once again set the urls at runtime:

                      var            config            =            new            ConfigurationBuilder            (            )            .            AddEnvironmentVariables            (            "MYVALUES_"            )            .            Build            (            )            ;                  

ASPNETCORE_URLS

The other option to be aware of is the special surround variable, "ASPNETCORE_URLS". If this is gear up, it volition overwrite whatsoever other values that have been set by UseConfiguration, whether from hosting.json or control line arguments etc. The only way (that I plant) to overwrite this value is with an explicit UseUrls() phone call.

Using the ConfigurationBuilder approach to configuring your server gives the almost flexibility at runtime for specifying your urls, and so I would definitely encourage you to use this approach any fourth dimension you detect the need to specify your urls. However, yous may well find the need to configure your Kestrel/WebListener urls at all surprisingly rare…

Configuring IIS Express and Visual Studio

In the previous department I demonstrated how to configure the urls used by your ASP.NET Core hosting server, whether Kestrel or WebListener. While yous might directly expose those self-hosted servers in some cases (the docs cite Service Fabric as a possible use case), in almost cases you will be reverse-proxied behind IIS on Windows, or Apache/Nginix on Linux. This means that the urls you take configured will not be the actual urls exposed to the outside world.

You can meet this event for yourself if you are developing using Visual Studio and IIS. When you lot create a new ASP.Net Core project in Visual studio 2015 (Update three), a launchSettings.json file is created inside the Backdrop folder of your project. This file contains settings for when you host your application in IIS:

                      {            "iisSettings"            :            {            "windowsAuthentication"            :            fake            ,            "anonymousAuthentication"            :            true            ,            "iisExpress"            :            {            "applicationUrl"            :            "http://localhost:55862/"            ,            "sslPort"            :            0            }            }            ,            "profiles"            :            {            "IIS Express"            :            {            "commandName"            :            "IISExpress"            ,            "launchBrowser"            :            true            ,            "environmentVariables"            :            {            "ASPNETCORE_ENVIRONMENT"            :            "Development"            }            }            ,            "ExampleWebApplication"            :            {            "commandName"            :            "Project"            ,            "launchBrowser"            :            true            ,            "launchUrl"            :            "http://localhost:5000"            ,            "environmentVariables"            :            {            "ASPNETCORE_ENVIRONMENT"            :            "Development"            }            }            }            }                  

This file contains two sections, iisSettings and profiles. When running in visual studio, profiles provides the hooks required to launch and debug your application using F5. In this instance nosotros have 2 profiles; IIS Express, which adequately obviously runs the application using IIS Limited; and ExampleWebApplication, the name of the spider web project, which runs the application using dotnet run.

the F5 menu for running the application

These two profiles run your application in two distinct ways. The project profile, ExampleWebApplication, runs your application equally though you had run the application directly using dotnet run from the control line - it fifty-fifty opens the panel window that is running the awarding. In contrast, IIS Express hosts your awarding, acting as a reverse-proxy in the aforementioned wasy as IIS would in product.

If you were post-obit along with updating your urls, using the methods described in the previous section and running using F5, you may have institute that things weren't running as smoothly as expected.

When running using IIS Express or IIS, the urls you configure equally function of Kestrel/WebListener are substantially unused - it is the url configured in IIS that is publicly exposed and which y'all tin navigate to in your browser. Therefore when developing locally with IIS Express, you must update the iisSettings:applicationUrl key in launchSettings.json to alter the url to which yous are jump. You can too update the url in the Debug tab of Properties (Right click your project -> Properties -> Debug).

How to change the IIS express url using properties tab

In dissimilarity, when y'all run the project profile, the urls you lot configure using the previous section are directly exposed. Withal, by default the profile opens a browser window - which requires a url - then a default url of http://localhost:5000 is specified in the launchSettings setting. If you are running straight on Kestrel/Weblistener and are customising your urls don't forget to update this setting to load the correct url, or set "launchBrowser": false!

Summary

This post gave a cursory summary of the changes to the server hosting model in ASP.Net Cadre. Information technology described the various ways in which you can specify the binding urls when self-hosting your application using the Kestrel or WebListener servers. Finally it described things to look out for related to changing your urls when developing locally using Visual Studio and IIS Express.

thompsonscame1964.blogspot.com

Source: https://andrewlock.net/configuring-urls-with-kestrel-iis-and-iis-express-with-asp-net-core/

0 Response to "How Cai I Registered to Iis Again Under Logianalytuics"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel