簡體   English   中英

Web服務MS Word比較自動化導致凍結

[英]web service MS Word compare automation cause freeze

我的Web服務使用ms字COM來比較2個.doc文件。 當結果文件將超過1 Mb時-它掛斷了。 (對於較小的文件-一切正常)。 當我在IIS上發布並運行WebService時,就會發生這種情況。 (在win serv 2008 x64下的主機,IIS-7)因此,將Word COM添加為Service作為COM引用。 我必須在那里安裝ms word 2010 x64,否則服務會引發null ref異常。

在我的本地計算機上(在VS調試模式下),在Win 7和Office 2010 32位下,一切正常。

詳細信息:我正在使用JS來調用Web服務:

function post_to_url() {
    var path = "http://localhost:32496/Service1.asmx/runCompareService";
    var params = {'mAuthToken':'xU/fnlKCe85R25I8IIRHIQCmPc7rcajYVHLQ3JChv8w=','documentId':'1441378','origVerNum':'1','revisedVerNum':'2'};
    var method = "post"; 
    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", path);

        for(var key in params) {
            if(params.hasOwnProperty(key)) {
            var hiddenField = document.createElement("input");
            hiddenField.setAttribute("type", "hidden");
            hiddenField.setAttribute("name", key);
            hiddenField.setAttribute("value", params[key]);
            form.appendChild(hiddenField);
            }
        }

        document.body.appendChild(form);
        form.submit();
    }

C#比較方法:

 public void doCompare(String file1path, String file2path, String file3path)
    {
        Microsoft.Office.Interop.Word.Application wordApp = null;
        Microsoft.Office.Interop.Word.Document doc1 = null;
        Microsoft.Office.Interop.Word.Document doc2 = null;
        Microsoft.Office.Interop.Word.Document doc = null;

        object wordTrue = (object)true;
        object wordFalse = (object)false;
        object missing = Type.Missing;

        object fileToOpen = @file1path;
        object fileToOpen1 = @file2path;
        object fileToSave = @file3path;

        try
        {
            wordApp = new Microsoft.Office.Interop.Word.Application();
            wordApp.Visible = false;
            wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
            try
            {
                doc1 = wordApp.Documents.Open(ref fileToOpen, ref missing, ref wordFalse, ref wordFalse, ref missing,
                ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref wordTrue, ref missing,
                ref missing, ref missing, ref missing);
            }
            catch (Exception e)
            {
                throw new Exception("Failed to open approved file" + e.ToString());
            }
            try
            {
                doc2 = wordApp.Documents.Open(ref fileToOpen1, ref missing, ref wordFalse, ref wordFalse, ref missing,
                ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing);
            }
            catch (Exception e)
            {
                throw new Exception("Failed to open revised file" + e.ToString());
            }
            if ((doc1 != null) && (doc2 != null))
            {
                try
                {
                    doc = wordApp.CompareDocuments(doc1, doc2, WdCompareDestination.wdCompareDestinationOriginal, WdGranularity.wdGranularityWordLevel,
                    true, true, true, true, true, true, true, true, true, true, "", false);
                    doc.SaveAs2(fileToSave);
                    ((_Document)doc).Close();
                }
                catch (Exception e)
                {
                    throw new Exception("Failed to save compare result file" + e.ToString());
                }
            }
        }
        catch (Exception e)
        {
            throw new Exception("Failed to open MS Word Application" + e.ToString());
        }
        finally
        {
            ((_Application)wordApp).Quit();
        }
    }

響應更改為:

private void DownloadToBrowser(String filePath)
    {
        FileInfo file = new FileInfo(filePath);
        byte[] fileBytes = ReadFile(filePath);

        Context.Response.Clear();
        Context.Response.ClearHeaders();
        Context.Response.ClearContent();
        Context.Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
        Context.Response.AddHeader("Content-Length", file.Length.ToString());
        Context.Response.AddHeader("Connection", "close");
        Context.Response.ContentType = "application/msword";
        Context.Response.ContentEncoding = Encoding.UTF8;
        Context.Response.OutputStream.Write(fileBytes, 0, fileBytes.Length);
        Context.Response.Flush();
        Context.ApplicationInstance.CompleteRequest();
    }

看起來服務掛在COM比較操作上

try 
{
     doc = wordApp.CompareDocuments(doc1, doc2, WdCompareDestination.wdCompareDestinationOriginal, WdGranularity.wdGranularityWordLevel,
                    true, true, true, true, true, true, true, true, true, true, "", false);
      doc.SaveAs2(fileToSave);
      ((_Document)doc).Close();
}
 catch (Exception e)
{
       throw new Exception("Failed to save compare result file" + e.ToString());
}

有人可以幫忙嗎?

Office Automation旨在自動執行桌面程序(Word,Excel等)。 它做出許多假設,認為它在桌面環境中運行。 例如,它可能會在多線程進程中運行時出現問題。

即使有的話,它也不是很好。 如果幸運的話,它將立即失敗,您將找到另一個解決方案。 如果您不走運,您將獲得一些投入生產,並將依賴於此, 然后您將開始遇到難以解決甚至可能無法修復的關鍵問題。

不要這樣做

使用Office 2010 COM查看asp.net Web服務

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM