简体   繁体   中英

OfficeConverter issue when deploying to Azure App Service

I have a web API which simply

  • clone a.docx file
  • convert that cloned.docx to a.pdf format
using DocumentFormat.OpenXml.Packaging;


[HttpPost("clone")]
public IActionResult CloneBillFromTemplate()
{
     var templateFilePath = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "Bill", "PaymentTempl.docx");
     var clonedFilePath = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "Bill", "ClonedBill.docx");
     var pdfFilePath = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "Bill", "FinalBill.pdf");
     
     using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(templateFilePath, true))
     {
         var clonedDoc = wordDoc.Clone(clonedFilePath);
         System.Threading.Thread.Sleep(500);
         clonedDoc.Save();
         clonedDoc.Close();
     }

     using (var converter = new OfficeConverter.Converter())
     {
         converter.Convert(clonedFilePath, pdfFilePath);
     }

     return Ok();
}

Everything works fine when debugging (for sure:3) and also on IIS

But when I deploy to Azure App service, I got this type of error (stack trace + exception message).

Could not read registry to check Word version

Could not find registry key Word.Application\CurVer at OfficeConverter.Word..ctor() at OfficeConverter.Converter.get_Word() at OfficeConverter.Converter.Convert(String inputFile, String outputFile, Stream logStream) at....

Could you guys help me on this? Thanks all!!!

** feel free to ask for more information you need to detect this issue

Update Looks like this is the issue with the pdf converter pacakge I'm using, not the OpenXML

It seems your application is relying on the Windows registry in a way that is not supported. If you are running on a Linux App Service, that would be the first thing to swap out, though my guess is that you are already running on Windows.

Apps have read-only access to much (though not all) of the registry of the virtual machine they are running on. In practice, this means registry keys that allow read-only access to the local Users group are accessible by apps. One area of the registry that is currently not supported for either read or write access is the HKEY_CURRENT_USER hive.

Write-access to the registry is blocked, including access to any per-user registry keys.

https://docs.microsoft.com/en-us/azure/app-service/operating-system-functionality#registry-access

If you can't refactor your code to not rely on such dependencies, I would suggest you put your application inside a Windows docker container . If you can run that locally, it should run on App Service as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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