在从IIS网站加载文档时遇到OutOfMemoryException异常。

huangapple go评论53阅读模式
英文:

Getting OutOfMemoryException when load a document with Microsoft.Office.Interop from IIS website

问题

这是你提供的代码段的翻译:

我有一个.NET Core 6的Web API,它使用Office互操作来打开PPT文档并将其转换为PDF。
当我调试它或将其发布为独立应用程序并手动运行.exe时,应用程序没有问题。
但是,我需要将Web API发布到运行在Windows服务器上的IIS上,但当我这样做时,在尝试调用API端点时出现以下错误:“System.OutOfMemoryException:内存不足,无法继续执行程序”。

这是执行转换的方法,异常是由“pptApplication.Presentations.Open(originalPptPath, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse)”抛出的:

public static bool PowerPointToPdf(string originalPptPath, string pdfPath)
{
    PowerPoint.Application pptApplication = null;
    PowerPoint.Presentation pptPresentation = null;

    object unknownType = Type.Missing;
    var result = false;

    //启动PowerPoint
    pptApplication = new PowerPoint.Application();

    //打开PowerPoint文档
    pptPresentation = pptApplication.Presentations.Open(originalPptPath, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse);

    if (pptPresentation == null)
        return false;

    pptPresentation.ExportAsFixedFormat(pdfPath, PowerPoint.PpFixedFormatType.ppFixedFormatTypePDF, PowerPoint.PpFixedFormatIntent.ppFixedFormatIntentPrint);
    result = true;

    //关闭和释放文档对象。
    if (pptPresentation != null)
    {
        pptPresentation.Close();
        ReleaseObject(pptPresentation);
        pptPresentation = null;
    }

    pptApplication.Quit();
    ReleaseObject(pptApplication);
    pptApplication = null;

    return result;
}

我已经尝试更改应用程序池标识为管理员用户,但没有任何变化。我知道Microsoft不鼓励使用服务器端Office自动化,我已经评估了几个不使用Office自动化进行转换的库,但最终结果在质量上不如意。

英文:

I have a .NET Core 6 web api that use Office interop to open a ppt document and convert it to pdf.
The application does not have problems when i debug it or when i publish it as a standalone application and run the .exe manually.
I need to publish the web api on IIS on a windows server but when i do i obtain the following error when try to call the api endpoint: "System.OutOfMemoryException: Insufficient memory to continue the execution of the program".

This is the method that made the conversion, exception is thrown by "pptApplication.Presentations.Open(originalPptPath, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse)"

        public static bool PowerPointToPdf(string originalPptPath, string pdfPath)
        {
            PowerPoint.Application pptApplication = null;
            PowerPoint.Presentation pptPresentation = null;

            object unknownType = Type.Missing;
            var result = false;

            //start power point 
            pptApplication = new PowerPoint.Application();

            //open powerpoint document
            pptPresentation = pptApplication.Presentations.Open(originalPptPath, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse);

            if (pptPresentation == null)
                return false;

            pptPresentation.ExportAsFixedFormat(pdfPath, PowerPoint.PpFixedFormatType.ppFixedFormatTypePDF, PowerPoint.PpFixedFormatIntent.ppFixedFormatIntentPrint);
            result = true;


            // Close and release the Document object.
            if (pptPresentation != null)
            {
                pptPresentation.Close();
                ReleaseObject(pptPresentation);
                pptPresentation = null;
            }

            pptApplication.Quit();
            ReleaseObject(pptApplication);
            pptApplication = null;

            return result;
        }

I already tried to change the application pool identity with an admin user but nothing changed. I am aware that microsoft discourages the use of server-side office automation, i have already evaluated several libraries that don't use office automation to make the conversion, but the final result is qualitatively inferior.

答案1

得分: 2

你走在正确的道路上 - Microsoft目前不建议,也不支持从任何非交互式客户端应用程序或组件(包括ASP、ASP.NET、DCOM和NT Services)自动化Microsoft Office应用程序,因为在这种环境下运行Office可能会出现不稳定的行为和/或死锁。

如果您正在构建在服务器端上下文中运行的解决方案,您应该尝试使用已经针对无人值守执行而设计的组件。或者,您可以尝试寻找允许至少部分代码在客户端运行的替代方案。如果您从服务器端解决方案中使用Office应用程序,该应用程序将缺少成功运行所需的许多功能。此外,您将冒着整体解决方案稳定性的风险。

Microsoft强烈建议开发人员在需要开发服务器端解决方案时寻找不需要在服务器端安装Office的替代方案,并且这些替代方案可以比自动化执行更高效更快地执行大多数常见任务。在将Office作为项目中的服务器端组件之前,考虑替代方案。

如果您只处理PowerPoint中的Open XML文档,您可以考虑使用Open XML SDK来处理文档或将其转换为其他文件格式,详细信息请参阅欢迎使用Office的Open XML SDK 2.5

或者考虑使用专为服务器端执行设计的第三方组件。

英文:

You are on the right avenue - 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.

Microsoft strongly recommends that developers find alternatives to Automation of Office if they need to develop server-side solutions. Because of the limitations to Office's design, changes to Office configuration are not enough to resolve all issues. Microsoft strongly recommends a number of alternatives that do not require Office to be installed server-side, and that can perform most common tasks more efficiently and more quickly than Automation. Before you involve Office as a server-side component in your project, consider alternatives.

If you are dealing only with open XML documents in PowerPoint you may consider using the Open XML DSK for dealing with documents or converting them to another file format, see Welcome to the Open XML SDK 2.5 for Office for more information.

Or just consider using any third-party components designed for the server-side execution.

huangapple
  • 本文由 发表于 2023年2月19日 19:40:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499867.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定