更新Web.config

问题描述

我想通过Windows容器中的C#更新web.config文件。

以下是在C#中更新web.config的功能

private void UpdateWebConfigFile()
{
    var configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
    var section = (System.Configuration.ConnectionStringsSection)configuration.GetSection("connectionStrings");

    var defaultConnection = section.ConnectionStrings["DefaultConnection"].ConnectionString;
    section.ConnectionStrings["DefaultConnection"].ConnectionString = Environment.ExpandEnvironmentVariables(defaultConnection);

    configuration.Save();
}

但是在configuration.Save();中发生了一个错误,我得到如下结果

Server Error in '/' Application.
Configuration Error
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.

Parser Error Message: An error occurred loading a configuration file: Access to the path 'C:\inetpub\wwwroot\tynfsgbl.tmp' is denied.

Source Error:


An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could,however,be viewed by browsers running on the local server machine.

Source File: C:\inetpub\wwwroot\web.config    Line: 0

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.7.3535.0

我的问题是How to update Web.config in the application of Windows Container? 谢谢!

解决方法

容器在设计上是不可变的,因此,默认用户设置为非管理员帐户,该帐户无权修改IIS Directoy。

尽管您可以将运行时用户修改为管理员,但是由于容器内部IIS的存储方式会出现另一个问题。更改web.config将导致IIS重置池,而docker将其视为进程崩溃并停止容器。

要么作为容器构建的一部分修改web.config,要么直接使用环境变量,而不是在容器运行时尝试将其写入web.config。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...