【问题标题】:web service MS Word compare automation cause freezeWeb 服务 MS Word 比较自动化导致冻结
【发布时间】:2013-01-11 12:05:06
【问题描述】:

我的 WebService 使用 ms word COM 来比较 2 个 .doc 文件。当结果文件将超过 1 Mb 时 - 它会挂起。 (对于较小的文件 - 一切正常)。 当我在 IIS 上发布和运行 WebService 时会发生这种情况。 (主机在 win serv 2008 x64,IIS - 7 下) 因此,Word COM 作为 COM 引用添加到 Service。我必须在那里安装 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());
}

有人可以帮忙吗?

【问题讨论】:

标签: c# web-services ms-word asmx office-automation


【解决方案1】:

Office Automation 是为自动化桌面程序(Word、Excel 等)而开发的。它做出了许多假设,即它在桌面环境中运行。例如,它可能会在多线程进程中运行时出现问题。

它不能很好地工作,如果有的话。如果你很幸运,那么它会立即失败,你会找到另一个解决方案。如果您不走运,您会将某些东西投入生产,并开始依赖它,然后然后您将开始遇到难以解决甚至无法修复的关键问题。

不要这样做

asp.net web service using office 2010 COM

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多