0

I have the following code, which works perfect in local machine:

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

After deploying to Azure Web App this code started to generate Exceptions:

    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)

Is Azure Web Apps filesystem is in read only mode by default? There are ways to fix it without changing a server?

1 Answers1

0

From error trace this looks like the issue is with path.

As it is working fine locally, we need to make sure it is accessible from the webapp and include necessary configuration for it.

From the code, use forward slash (/) instead of back slash, because “/” recognizes by Windows and Linux, In some cases backslash will raise few errors while passing the path in code.

Also, Try to use Path.Combine Method to pass the path to a variable. As it is intended to concatenate individual strings into a single string that represents a file path.

Example: Path.Combine(currentDirectory, "..", "wwwroot")
SaiKarri-MT
  • 1,174
  • 1
  • 3
  • 8