0

My WebService uses ms word COM to compare 2 .doc files. When result file is going to be more than 1 Mb - it's hangs up. (for smaller files - everything is ok). It's happens when I publish&run WebService on IIS. (Host under win serv 2008 x64, IIS - 7) So, Word COM added to Service as COM reference. I had to install there ms word 2010 x64, otherwise service throws null ref exception.

On my local comp (in VS debug mode), under win 7 and office 2010 32 bit, everything is ok.

Details: I'm using JS to call web service:

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# compare method:

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

response is changed to:

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

it's look like service hangs on COM compare operation

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

Can someone help?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Aksndr
  • 1
  • 2
  • possible duplicate of [asp.net web service using office 2010 COM](http://stackoverflow.com/questions/7382704/asp-net-web-service-using-office-2010-com) – John Saunders Jan 11 '13 at 03:57

1 Answers1

1

Office Automation was developed to automate desktop programs (Word, Excel, etc). It makes many assumptions that it is operating in a desktop environment. For instance, it will likely have problems running in a multi-threaded process.

It doesn't work well, if at all. If you're lucky, then it will fail immediately and you'll find another solution. If you're not lucky, you'll get something into production, will come to depend on it, and then you'll start getting critical problems which are difficult to troubleshoot and maybe impossible to fix.

Don't Do This

See asp.net web service using office 2010 COM

Community
  • 1
  • 1
John Saunders
  • 160,644
  • 26
  • 247
  • 397