0

I have my web api that uploads and reads an excel file from the client app and then afterwards saves the data into the database, the application works perfect on locally server but the problem comes when the application is deployed to azure server it returns error 500 internal server error therefore i don't understand why this happens and and don't know how i can track to understand what might be the cause below are my code blocks.

My Interface Class

public interface UploadExcelInterface
{
Task UploadMultipleClients(Client obj);
} 

My Service Implementation

public class UploadExcelService : UploadExcelInterface
   {
   private readonly DbContext _connect;
   private readonly IHttpContextAccessor httpContextAccessor;
   public UploadExcelService(DbContext _connect, IHttpContextAccessor httpContextAccessor)
   {
   this._connect = _connect;
   this.httpContextAccessor = httpContextAccessor;
   }

   public async Task UploadMultipleClients(Client obj)
   {
   var file = httpContextAccessor.HttpContext.Request.Form.Files[0];
   if (file != null && file.Length > 0) 
   { 
   var folderName = Path.Combine("Datas", "ClientUPloads");
   var pathToSave = Path.Combine(Directory.GetCurrentDirectory(), folderName);
   var fileName   = Guid.NewGuid() + ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
   var fullPath   = Path.Combine(pathToSave, fileName);

   var clientsList = new List<Client>();
   using (var fileStream = new FileStream(fullPath, FileMode.Create)) 
   {
   await file.CopyToAsync(fileStream);
   FileInfo excelFile = new FileInfo(Path.Combine(pathToSave, fileName));
   ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
   using (ExcelPackage package = new ExcelPackage(excelFile))
   {
   ExcelWorksheet worksheet = package.Workbook.Worksheets[0];
   var rowcount = worksheet.Dimension.Rows;
   for (int row = 2; row <= rowcount; row++) 
   {
   var Names   = (worksheet.Cells[row,2].Value ?? string.Empty).ToString().Trim();
   var Address   = (worksheet.Cells[row,3].Value ?? string.Empty).ToString().Trim();
   var Title   = (worksheet.Cells[row,4].Value ?? string.Empty).ToString().Trim();
   var Product     = (worksheet.Cells[row,5].Value ?? string.Empty).ToString().Trim();
   var Order  = (worksheet.Cells[row,6].Value ?? string.Empty).ToString().Trim();
   var Email   = (worksheet.Cells[row,7].Value ?? string.Empty).ToString().Trim();
   var Price = (worksheet.Cells[row,8].Value ?? string.Empty).ToString().Trim();

   clientsList.Add(new Client
   {
   Names   = Names,
   Address = Address,
   Title = Title,
   Product = Product,
   Order  = Order,
   Email   = Email,
   Price = Price,
   }
   }

   //adding clients into the database
   foreach (Client client in clientsList)  
   {
   var exist = _connect.client.Any(x => x.Email == client.Email);
   if (!exist)
   {
   await _connect.client.AddAsync(client);
   }
   }  
   await _connect.SaveChangesAsync();
   }
   }
   }


My Controller Class

[HttpPost]
public async Task UploadMultipleClients([FromForm] Client obj)
{ 
await uploadExcelInterface.UploadMultipleClients(obj);
}
}

Please any help regarding this error that am getting from the server, and addition on that is it possible to get the data from the excel file without uploading it to server if yes how? because i tried adding the file to memory stream an reading it from memory but it appers not work, any suggestions thanks.

  • please post your docker file – sa-es-ir Apr 10 '22 at 07:55
  • i have no any docker file in this project and i have not used it at all –  Apr 10 '22 at 07:59
  • So how you deploy it to Azure? – sa-es-ir Apr 10 '22 at 08:01
  • am using publishing profile obtained from azure app service to deploy –  Apr 10 '22 at 08:18
  • ok the OS of azure app is Windows or Linux? – sa-es-ir Apr 10 '22 at 08:32
  • it is on windows platform –  Apr 10 '22 at 08:38
  • As far as I know, Epplus depends on ``System.Drawing`` that means if it's not on OS it won't work correctly, can you see the exception message and also see https://learn.microsoft.com/en-us/dotnet/api/system.drawing?view=net-6.0 – sa-es-ir Apr 10 '22 at 08:42
  • how can i check the exception message since the only error am getting from the server is internal server error and i don't know how i can debug that from server –  Apr 10 '22 at 09:04
  • guys any update regarding this, still am not in a position to fix this error –  Apr 11 '22 at 05:21
  • @Coder Hi Coder, I have [test EPPlus in azure](https://stackoverflow.com/a/68815018/7687666) and it works fine, it means azure web app can support `EPPlus`. Can you [enable stdout log](http://docs.lacunasoftware.com/en-us/articles/amplia/on-premises/windows/enable-stdout-log.html) to get more errot details. – Jason Pan Apr 11 '22 at 07:23

1 Answers1

0

My answer may not help you solve the problem directly, but it can locate the error step by step. After we fix the error, we should be able to solve the problem in this thread.

Suggestions

  1. Please make sure you have inclue EPPlus library in your deploy content.

  2. Enabling ASP.NET Core stdout log (Windows Server)

  3. Azure App Service - Configure Detailed Error Logging

Why

After tested, I am sure azure webapp can support EPPlus. For 500 error, as we don't have a more specific error message to refer to, we can't quickly locate the problem. Following the suggested method, you will surely see some useful information.

E.g:

  1. The class library of EPPlus was not found.
  2. Folders such as Datas are not created.
  3. The database connection string, the test environment and the production environment may be different. ...
Jason Pan
  • 15,263
  • 1
  • 14
  • 29
  • Thanks so much @Jason for your help i managed to configure Detailed Error Logging on azure server and am getting the below response Most likely causes: The root cause of this error may depend on IIS was not able to access the web.config file for the Web site or application. This can occur if the NTFS permissions are set incorrectly. IIS was not able to process configuration for the Web site or application. The authenticated user does not have permission to use this DLL. The request is mapped to a managed handler but the .NET Extensibility Feature is not installed. –  Apr 11 '22 at 13:17
  • @Coder Which kind of dll ? EPPlus? – Jason Pan Apr 11 '22 at 13:25
  • But i think the library EPPLUS is missing on the server even after several deployments but is not deployed so i don't know how i can get it published to azure and am using publishing profile obtained from azure –  Apr 11 '22 at 13:25
  • yes Epplus dill file –  Apr 11 '22 at 13:26
  • @Coder, it's very easy. Pls drag and paste it via scm site first. We need test , or we can deploy it manually first. If it works, I will update my answer, how to include this dll when build and publish. – Jason Pan Apr 11 '22 at 13:28
  • @Coder https://stackoverflow.com/questions/40108106/reference-external-dll-in-net-core-project – Jason Pan Apr 11 '22 at 14:04