Valuator
Valuator

Reputation: 3627

Add AspNetCore argument to IIS application using Microsoft.Web.Administration

I have a need to host multiple instances of an ASP.NET Core application on a server, which each one being configured differently via a command line argument. The web application instances are managed using a Windows Forms application with the Microsoft.Web.Administration API.

Without using the WinForms app, I was able to get this working with multiple instances by adding arguments with the Configuration Editor for each site in IIS Manager and setting "From" to ApplicationHost.config. This sets a different argument for each app instance to use, all while still running from the same ASP.NET Core .exe.

What I haven't been able to do is translate this to functionality in the WinForms application. When an instance is added via the tool, a new <location> element is added to ApplicationHost.config, and I need to add an <aspNetCore arguments="xxxx" /> child element to it.

Upvotes: 0

Views: 967

Answers (1)

Valuator
Valuator

Reputation: 3627

I was able to get this working, I had overlooked the overload of GetSection that would read from a specific location. tt was as simple as this:

var aspNetCoreConfig = site.GetWebConfiguration().GetSection("system.webServer/aspNetCore", "<appInstanceName>");

aspNetCoreConfig["arguments"] = "xxxx";

When adding the Application using ServerManager. It's important that the web.config for the application itself NOT have an arguments attribute in the <aspNetCore> section. This will override anything set in the Site's web.config or the server's ApplicationHost.config for that location since all application instances share a web.config in their root folder, but can have instance-specific settings configured in their respective <location> sections in a higher-level config.

Upvotes: 0

Related Questions