You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure App Service .NET Core 3.1导出文件运行异常问题咨询

问题描述

我编写了如下代码,在本地机器上运行完全正常:

string filePath = env.ContentRootPath + "\wwwroot\" + fileName;
using (MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection(constring))
{
    using (MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand())
    {
        using (MySqlBackup mb = new MySqlBackup(cmd))
        {
            cmd.Connection = conn;
            conn.Open();
            mb.ExportToFile(filePath);
            conn.Close();
        }
    }
}

将项目部署到Azure Web App后,该代码运行时抛出如下异常:

2021-09-28T12:53:42.595008982Z: [INFO]  Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1]
2021-09-28T12:53:42.595074782Z: [INFO]        An unhandled exception has occurred while executing the request.
2021-09-28T12:53:42.595100582Z: [INFO]  System.IO.IOException: Invalid argument
2021-09-28T12:53:42.596337082Z: [INFO]     at Interop.ThrowExceptionForIoErrno(ErrorInfo errorInfo, String path, Boolean isDirectory, Func`2 errorRewriter)
2021-09-28T12:53:42.596351282Z: [INFO]     at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String path, OpenFlags flags, Int32 mode)
2021-09-28T12:53:42.596364382Z: [INFO]     at System.IO.FileStream.OpenHandle(FileMode mode, FileShare share, FileOptions options)
2021-09-28T12:53:42.602248082Z: [INFO]     at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
2021-09-28T12:53:42.602267082Z: [INFO]     at System.IO.StreamWriter.ValidateArgsAndOpenPath(String path, Boolean append, Encoding encoding, Int32 bufferSize)
2021-09-28T12:53:42.602282282Z: [INFO]     at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding)
2021-09-28T12:53:42.602285982Z: [INFO]     at MySql.Data.MySqlClient.MySqlBackup.ExportToFile(String filePath)
2021-09-28T12:53:42.602289682Z: [INFO]     at ServerMonitoring3._1.Helpers.BackUpCreationHelper.CreateAndManageBackups(IGenericRepository`1 ProjectTypeRepo, IConfiguration configuration, IWebHostEnvironment env, IGenericRepository`1 backupFIleTypeRepo, IBackupFileRepository backupFileRepository, ILogger logger) in C:\Users\NazariiSvyryd\source\repos\monitoring_net\Helpers\BackUpCreationHelper.cs:line 51
2021-09-28T12:53:42.602294482Z: [INFO]     at ServerMonitoring3._1.Controllers.BackupCreationController.CreateBackups() in C:\Users\NazariiSvyryd\source\repos\monitoring_net\Controllers\BackupCreationController.cs:line 56
...后续栈日志省略

我想知道:Azure Web Apps的文件系统是否默认处于只读模式?是否存在无需修改服务器配置即可修复该问题的方案?


问题解答

1. Azure Web App文件系统默认状态说明

Azure Web App的文件系统不是默认只读,默认开放应用部署目录下wwwroot文件夹的读写权限,仅限制系统级目录的访问权限。你遇到的报错和只读权限无关,核心原因是路径格式不兼容部署环境。

本地是Windows环境,支持反斜杠\作为路径分隔符,但如果你的Azure Web App用的是Linux/容器运行环境,路径分隔符要求为正斜杠/,硬编码Windows风格的分隔符会生成非法路径,触发Invalid argument类IO异常,和你给出的报错栈完全匹配。

2. 无需修改服务器配置的修复方案

仅需要修改路径拼接逻辑,使用.NET内置的Path.Combine方法自动适配不同运行环境的路径分隔符即可,不需要调整任何服务器配置:

修复后代码示例

// Path.Combine会自动根据运行环境选择正确的路径分隔符
string filePath = Path.Combine(env.ContentRootPath, "wwwroot", fileName);
using (MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection(constring))
{
    using (MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand())
    {
        using (MySqlBackup mb = new MySqlBackup(cmd))
        {
            cmd.Connection = conn;
            conn.Open();
            mb.ExportToFile(filePath);
            conn.Close();
        }
    }
}

额外排查点

如果修改路径后仍然报错,确认两个细节即可:

  • 部署包中保留了wwwroot目录(如果目录为空,发布时可能被自动忽略,可在目录下放一个空白占位文件避免该问题)
  • 备份文件名不包含:、*、?等跨系统不兼容的特殊字符

内容的提问来源于stack exchange,提问作者Nazar Svyryd nazarllf

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 09:45:05