RonC
RonC

Reputation: 33771

Get IIS version or other IIS Server variables in Asp.Net 5?

In Asp.Net Core 2.x I could obtain IIS information via Server Vars like so:

  IServerVariablesFeature serverVars = HttpContext.Features.Get<IServerVariablesFeature>();
  string iis_version = serverVars["SERVER_SOFTWARE"];
  string app_pool_id = serverVars["APP_POOL_ID"];

This approach worked even when using Kestrel as a reverse proxy. However, accessing IIS server variables this ways appears to have stopped working in Asp.Net Core 3.x. Starting at that time and now also in Asp.Net 5.0 the IServerVariablesFeature no longer exists and when requesting it null is returned.

How can I get access to the IIS server variables (ie. https://learn.microsoft.com/en-us/previous-versions/iis/6.0-sdk/ms524602(v=vs.90)?redirectedfrom=MSDN) in Asp.Net 5.x?

Upvotes: 2

Views: 2267

Answers (2)

RonC
RonC

Reputation: 33771

After doing a more research it turns out the reason I do not have access to the IServerVariablesFeature feature is because I run my web application OutOfProcess.

Starting in .NetCore 3.0 the IServerVariablesFeature feature is only available when running in process in IIS. Otherwise it returns null. This seems like a feature regression given that the feature worked even when running out of process in Asp.Net Core 2.2.

Upvotes: 1

Bruce Zhang
Bruce Zhang

Reputation: 3042

In fact, My test shows that this code can still work in Asp.net 5, and IIS variables can be obtained. enter image description here Here is the code

using Microsoft.AspNetCore.Http.Features;

 IServerVariablesFeature serverVars = HttpContext.Features.Get<IServerVariablesFeature>();
  string iis_version = serverVars["SERVER_SOFTWARE"];
  string app_pool_id = serverVars["APP_POOL_ID"];
  string runtime = System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription;

The Asp.net core version information for IServerVariablesFeature is also mentioned in the document.

Applies to ASP.NET Core 5.0 3.1 3.0 2.2

So I think you should carefully check the other code in the application, whether it may be a misspelling when referencing variables, or a problem with the installation of Asp.net Core 5.

Upvotes: 3

Related Questions