How to configure IIS to run Asp.Net Core applications with MS PowerPoint

377 views Asked by At

I have an Asp.Net Core Web Api application. I am using the Microsoft.Office.Interop.PowerPoint library to work with presentations.

I have a code that opens a presentation at a specified path. before opening I check if the file exists in the given directory:

ApplicationClass pptApplication = new ApplicationClass();
        
var path = _webHostEnvironment.ContentRootPath;

string pathForPresentation = $"{path}\\{fileName}";
FileInfo fileInfoPresentation = new FileInfo(pathForPresentation);
if (fileInfoPresentation.Exists)
   Logger.Info($"{path}\\{fileName} - file is available" );

Microsoft.Office.Interop.PowerPoint.Presentation pptPresentation = pptApplication.Presentations.Open($"{path}\\{fileName}", 
      MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse);

Everything works correctly when launching the application from Visual Studio. But after publishing the application to the iis webserver, I get an error when opening the presentation. The log shows that the file exists. But I get an error while opening the presentation. Error stack:

System.Runtime.InteropServices.COMException (0x80004005): Error HRESULT E_FAIL has been returned from a call to a COM component.
   at Microsoft.Office.Interop.PowerPoint.Presentations.Open(String FileName, MsoTriState ReadOnly, MsoTriState Untitled, MsoTriState WithWindow)
   at App.Controllers.Api.FilesController.ConvertSlideToImage(String fileName) in E:\App\Controllers\Api\FilesController.cs:line 190
   at App.Controllers.Api.FilesController.PostUploadFileAsApplication() in E:\App\Controllers\Api\FilesController.cs:line 137
   at lambda_method80(Closure , Object )
   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

Obviously a problem with access rights to office applications.

1

There are 1 answers

0
Eugene Astafiev On

The Considerations for server-side Automation of Office article states the following:

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution.

As a possible workaround, if you deal with open XML documents, you may consider using the Open XML SDK, see Working with presentation slides (Open XML SDK) for more information. Otherwise, you may consider using third-party components designed for the server-side execution.