NF_
NF_

Reputation: 33

Azure app gives HRESULT E_FAIL has been returned from a call to a COM component

I have a websocket-based text editor that I wrote for a college project. Works fine on my local machine, but when deployed to Azure I get an error.

The issue arises with converting html to rtf, which I did using a mixture of code from here and here. This required using windows form components in a single threaded apartment and was a bit of a headache, but it did work...until I migrated to Azure.

Debugging in Visual studio, I got the error message:

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: 

The offending code is found here, exception occurs at the line " 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; }
        }

    }
}

The controller code that calls the html convertor class:

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

Upvotes: 3

Views: 1828

Answers (1)

evilSnobu
evilSnobu

Reputation: 26324

Access to out-of-process COM is restricted in the Web App sandbox.

See my other answer for more on this — https://stackoverflow.com/a/38622209/4148708

Upvotes: 2

Related Questions