Home > Mobile >  EPPLUS package works fine locally but returns Internal server error when deployed to azure server
EPPLUS package works fine locally but returns Internal server error when deployed to azure server

Time:04-11

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.

CodePudding user response:

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. ...
  • Related