stepozer
stepozer

Reputation: 1191

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://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.server.kestrel.kestrelserveroptions?view=aspnetcore-1.1

But how to get this info in runtime?

Upvotes: 2

Views: 3617

Answers (1)

Aleš Doganoc
Aleš Doganoc

Reputation: 12082

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
   }
}

Upvotes: 3

Related Questions