Azure应用程序已从对COM组件的调用中返回HRESULT E_FAIL

本文关键字:返回 HRESULT FAIL 调用 应用程序 组件 COM Azure | 更新日期: 2023-09-27 17:59:15

我有一个基于websocket的文本编辑器,是为一个大学项目编写的。在我的本地计算机上运行良好,但当部署到Azure时,我会收到一个错误。

问题出现在将html转换为rtf时,我使用了这里和这里的混合代码。这需要在单线程公寓中使用windows窗体组件,这有点令人头疼,但它确实有效。。。直到我迁移到Azure。

在Visual studio中调试时,我收到错误消息:

System.Runtime.InteropServices.COMException was unhandled
  ErrorCode=-2147467259
  HResult=-2147467259
  Message=Error HRESULT E_FAIL has been returned from a call to a COM component.
  Source=System.Windows.Forms
  StackTrace:
       at System.Windows.Forms.UnsafeNativeMethods.IWebBrowser2.Navigate2(Object& URL, Object& flags, Object& targetFrameName, Object& postData, Object& headers)
       at System.Windows.Forms.WebBrowser.PerformNavigate2(Object& URL, Object& flags, Object& targetFrameName, Object& postData, Object& headers)
       at System.Windows.Forms.WebBrowser.set_Url(Uri value)
       at System.Windows.Forms.WebBrowser.set_DocumentStream(Stream value)
       at System.Windows.Forms.WebBrowser.set_DocumentText(String value)
       at RealTimeTextEditor.HtmlRtfConvertor.ConvertHtmltoRtf(Object obj) in C:'DissertationProjectGitRepo'RealTimeTextEditor'RealTimeTextEditor'HtmlRtfConvertor.cs:line 30
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart(Object obj)
  InnerException: 

在这里可以找到有问题的代码,异常发生在"tempBrowser.DocumentText=data.html;"行

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Web;
using System.Windows.Forms;
namespace RealTimeTextEditor
{
    public class HtmlRtfConvertor
    {
        public void ThreadConvertor(string inputpath, string inputhtml)
        {
            var t = new Thread(ConvertHtmltoRtf);
            var dataForConversion = new ConvertorData { path = inputpath, html = inputhtml };
            t.SetApartmentState(ApartmentState.STA);
            t.Start(dataForConversion);
            t.Join();
        }
        public static void ConvertHtmltoRtf (object obj)
        {
            var data = obj as ConvertorData;
            using (WebBrowser tempBrowser = new WebBrowser())
            {   
                tempBrowser.CreateControl();
                tempBrowser.DocumentText = data.html;
                while (tempBrowser.DocumentText != data.html)
                {
                    Application.DoEvents();
                }
                tempBrowser.Document.ExecCommand("SelectAll", false, null);
                tempBrowser.Document.ExecCommand("Copy", false, null);
                using (RichTextBox rtb = new RichTextBox())
                {
                    rtb.Paste();
                    rtb.SaveFile(data.path);
                }
            }
        }
        public class ConvertorData
        {
            public string path { get; set; }
            public string html { get; set; }
        }
    }
}

调用html转换器类的控制器代码:

        HtmlRtfConvertor convertor = new HtmlRtfConvertor();
        convertor.ThreadConvertor(path, html);

Azure应用程序已从对COM组件的调用中返回HRESULT E_FAIL

进程外COM的访问在Web App沙盒中受到限制。

有关这方面的更多信息,请参阅我的另一个答案—https://stackoverflow.com/a/38622209/4148708