繁体   English   中英

Azure App Service .NET Core 3.1 导出到文件异常

[英]Export To File Exception in 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
2021-09-28T12:53:42.604776982Z: [INFO]     at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
2021-09-28T12:53:42.604794482Z: [INFO]     at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
2021-09-28T12:53:42.604810282Z: [INFO]     at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
2021-09-28T12:53:42.604816482Z: [INFO]     at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)2021-09-28T12:53:42.604822082Z: [INFO]     at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
2021-09-28T12:53:42.604826382Z: [INFO]     at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
2021-09-28T12:53:42.604830582Z: [INFO]     at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
2021-09-28T12:53:42.604834482Z: [INFO]     at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
2021-09-28T12:53:42.604848482Z: [INFO]     at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
2021-09-28T12:53:42.604852882Z: [INFO]     at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
2021-09-28T12:53:42.604856882Z: [INFO]     at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
2021-09-28T12:53:42.604860782Z: [INFO]     at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
2021-09-28T12:53:42.604864682Z: [INFO]     at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
2021-09-28T12:53:42.604868882Z: [INFO]     at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
2021-09-28T12:53:42.604872682Z: [INFO]     at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
2021-09-28T12:53:42.605359082Z: [INFO]     at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
2021-09-28T12:53:42.605373582Z: [INFO]     at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)

Azure Web 应用程序文件系统默认处于只读模式吗? 有什么方法可以在不更改服务器的情况下修复它?

从错误跟踪看来,问题出在路径上。

由于它在本地运行良好,我们需要确保它可以从 webapp 访问并包括必要的配置。

从代码中,使用正斜杠 (/) 而不是反斜杠,因为“/”被 Windows 和 Linux 识别,在某些情况下,反斜杠在代码中传递路径时会引发一些错误。

另外,尝试使用Path.Combine方法将路径传递给变量。 因为它旨在将单个字符串连接成表示文件路径的单个字符串。

Example: Path.Combine(currentDirectory, "..", "wwwroot")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM