简体   繁体   中英

How to get .net core server name and options

I use c# .net core on linux. I want to see what server options and what server name I use. I know server name must be Kestrel and options must be something like this:

https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.server.kestrel.kestrelserveroptions?view=aspnetcore-1.1

But how to get this info in runtime?

You can get the Server information by injecting the IServer service. Which will be the actual server instance running. From there you can access the KestrelServerOptions in the Options property. Here is a sample code snippet where I inject it in a controller and get the options.

public WeatherForecastController(ILogger<WeatherForecastController> logger, IServer webServer)
{
   _logger = logger;
   KestrelServer kestrelServer = webServer as KestrelServer;
   if (kestrelServer == null)
   {
      throw new Exception($"Not running inside Kestrel server. The current server type is {webServer.GetType().FullName}");
   }
   else
   {
      KestrelServerOptions kestrelServerOptions = kestrelServer.Options;
      // do something with the options
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM