0

My 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());
}

誰か助けてくれませんか?

4

1 に答える 1

1

Office Automation は、デスクトップ プログラム (Word、Excel など) を自動化するために開発されました。デスクトップ環境で動作していることを前提としています。たとえば、マルチスレッド プロセスで実行すると問題が発生する可能性があります。

あったとしても、うまくいきません。運が良ければ、すぐに失敗し、別の解決策が見つかります。運が悪ければ、何かを本番環境に導入し、それに依存するようになり、トラブルシューティングが困難で修正が不可能な重大な問題が発生し始めます。

これをしないでください

Office 2010 COM を使用した asp.net Web サービスを参照してください。

于 2013-01-11T04:02:00.123 に答える