0

I participate to websites developments in asp.net core on visual studio. We have created a website, which should be divided in multiple instances. I have only one code and one publishing profile for this code (I don't want to have multiples publishing profiles, only one parameter change).

All of these websites share the same database connection, client.json and web.config. I differentiate all of these websites by their Instances. I don't want to put this instance in the client.json because it is shared. All these websites are supported by one application pool. I don’t know if it is a good idea, we have followed this procedure for helps maintainability (only one pool to restart in case of updates for example).

The solution I have found is to add an environment variable to each of this websites into iis (see Publish to IIS, setting Environment Variable). As said by NickAb, I override my client.json with the value of added to the environment variable (system.webServer/aspNetCore into Configuration editor on iis). It works.

My problem is: I have one code for multiples websites published on multiples servers. I don't want to do this update for each websites and servers manually!

I have developed a middleware, which create and maintain websites on multiple servers in C# using Microsoft.Web.Administration.

Therefore, I search how to add an environment variable to these websites using this library.

I have already tried this way: Environment Variables

Two issues: - I have IIS 10 and 8 (this solution only works for iis 10 apparently) - The code works (I mean, I have no exception), but I cannot see the result in the websites system.webServer/aspNetCore Configuraton editor in iis.

Is this solution the good one? If it is, what have I missed?

Thank you in advance for your help.

S. Pineau
  • 71
  • 2
  • 7
  • https://stackoverflow.com/questions/31049152/publish-to-iis-setting-environment-variable – geekfreak.xyz Apr 08 '18 at 15:07
  • 1
    "I cannot see the result in the websites" What can you see then? Besides, I have my personal opinions in a post, https://blog.lextudio.com/whats-microsoft-web-administration-and-the-horrible-facts-you-should-know-b82f2c974da6 – Lex Li Apr 08 '18 at 17:32
  • Hi @LexLi. I have implemented the [Environment Variables](https://learn.microsoft.com/en-us/iis/configuration/system.applicationhost/applicationpools/add/environmentvariables/) code on a middleware located on our servers. This code doesn't throw any exception and seems to work (I cannot see the result directly). When I look to the website via iis, to the `Configuration Editor` / `system.webServer/aspNetCore` I can't found my environment variable. I see a blank tab. Thanks for the link. – S. Pineau Apr 09 '18 at 07:22

1 Answers1

2

After more research, I have found my way.

As show below, I have found how to add a new environment variables to my website (here test.fr) into ApplicationHost.config file. This link helps me to understood how Microsoft.Web.Administration works on this subject.

IIS Configuration Editor / system.webServer/aspNetCore

Here the code I am using :

using Microsoft.Web.Administration;
using System;
using System.Collections.Generic;

internal static class Sample
{
    internal static void AddEnvironmentVariablesToWebSite(this ServerManager serverManager, KeyValuePair<string, string> environmentVariable, string siteName)
    {
        if (serverManager == null) throw new ArgumentNullException(nameof(serverManager));

        var config = serverManager.GetApplicationHostConfiguration();
        var aspNetCoreSection = config.GetSection("system.webServer/aspNetCore", siteName);
        var environmentVariablesCollection = aspNetCoreSection.GetCollection("environmentVariables");

        var addElement1 = environmentVariablesCollection.CreateElement(); // CreateElement() is equivalent to CreateElement("add")
        addElement1["name"] = environmentVariable.Key;
        addElement1["value"] = environmentVariable.Value;
        environmentVariablesCollection.Add(addElement1);
        serverManager.CommitChanges();
    }
}

I use this methods after the commitChanges() that create my site and pool into iis and add it into the ApplicationHost.config file.

Thanks to @Lexli blog, it is important to wait after a commitChanges() because it returns before the changes take efects. This was my second issue.

using(var serverManager = new ServerManager())
{
    //[Create site and pool]
    serverManager.CommitChanges();
    Thread.Sleep(100);

    serverManager.AddEnvironmentVariablesToWebSite(serverManager, environmentVariable, siteName);
    serverManager.CommitChanges();
}

Hope it helps .

S. Pineau
  • 71
  • 2
  • 7