【问题标题】:Set environment variables associated to a website on iis在 iis 上设置与网站关联的环境变量
【发布时间】:2018-09-18 01:05:10
【问题描述】:

我参与了 Visual Studio 上的 asp.net core 中的网站开发。我们创建了一个网站,该网站应该分为多个实例。我只有一个代码和这个代码的一个发布配置文件(我不想有多个发布配置文件,只更改一个参数)。

所有这些网站共享相同的数据库连接、client.json 和 web.config。我通过它们的实例来区分所有这些网站。我不想将此实例放在 client.json 中,因为它是共享的。 所有这些网站都由一个应用程序池支持。我不知道这是否是一个好主意,我们已经按照这个程序来帮助可维护性(例如,在更新的情况下只重启一个池)。

我找到的解决方案是将每个网站的环境变量添加到 iis 中(请参阅Publish to IIS, setting Environment Variable)。正如 NickAb 所说,我用添加到环境变量中的值覆盖了我的 client.json(system.webServer/aspNetCore 在 iis 上变为 Configuration editor)。它有效。

我的问题是:我有一个代码用于在多个服务器上发布的多个网站。我不想手动为每个网站和服务器进行此更新!

我开发了一个中间件,它使用 Microsoft.Web.Administration 在 C# 中的多个服务器上创建和维护网站。

因此,我搜索了如何使用该库向这些网站添加环境变量。

我已经尝试过这种方式:Environment Variables

两个问题: - 我有 IIS 10 和 8(这个解决方案显然只适用于 iis 10) - 代码有效(我的意思是,我也不例外),但我在 iis 中的网站 system.webServer/aspNetCore Configuraton editor 中看不到结果。

这个解决方案好不好?如果是,我错过了什么?

提前感谢您的帮助。

【问题讨论】:

  • "我在网站上看不到结果" 那你能看到什么?另外,我在一个帖子里有我的个人意见,blog.lextudio.com/…
  • 嗨@LexLi。我在位于我们服务器上的中间件上实现了Environment Variables 代码。此代码不会引发任何异常并且似乎可以工作(我无法直接看到结果)。当我通过 iis 查看网站时,Configuration Editor / system.webServer/aspNetCore 找不到我的环境变量。我看到一个空白标签。感谢您的链接。

标签: c# iis asp.net-core .net-core environment-variables


【解决方案1】:

经过更多研究,我找到了自己的方法。

如下所示,我找到了如何将新的环境变量添加到我的网站(此处为 test.fr)到 ApplicationHost.config 文件中。这个link 帮助我了解Microsoft.Web.Administration 在这个主题上的工作原理。

这是我正在使用的代码:

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();
    }
}

我在commitChanges() 之后使用此方法,将我的站点和池创建到iis 中并将其添加到ApplicationHost.config 文件中。

感谢@Lexli blog,在commitChanges() 之后等待很重要,因为它会在更改生效之前返回。这是我的第二个问题。

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

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

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 2017-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-15
    • 1970-01-01
    • 2012-10-01
    • 2023-03-13
    相关资源
    最近更新 更多