簡體   English   中英

ASP.NET代碼僅適用於本地計算機

[英]ASP.NET Code works on Local Machine Only

我在建立的ASP.NET網站上遇到了一些麻煩,很難調試。

背景資料:

我的網站上有一個頁面,允許用戶上傳一個或多個Microsoft Word文檔。 然后,用戶可以按一個按鈕,並且應該使用該代碼打開文檔,對單詞進行計數,然后返回表格中的單詞數。

當我在Visual Studio中運行調試器時,此方法可以很好地工作,但是當我嘗試從另一台計算機通過Web進行調試時,出現錯誤。

這是一些代碼。 我試圖盡可能簡化它。

// List of int's to hold the number of words in each document
List<int> words = new List<int>();

// Loop through the files that the user selected
// (The files have already been uploaded, and now their path is in "lstFileBox")
for (int i = 0; i < this.lstFileBox.Items.Count; i++)
{
    try
    {
         String file = this.lstFileBox.Items[i].Text;
         // MicrosoftWordOperations is a custom class
         MicrosoftWordOperations wordOps = new MicrosoftWordOperations(file);
         String contents = wordOps.GetContents();
         int numWords = wordOps.CountWords(contents);

          // Add number of words to my list
          words.Add(numWords);

          // Delete the uploaded file, which was stored in a temporary location
          if (System.IO.File.Exists(file))
               System.IO.File.Delete(file);

          }
          catch (Exception e)
          {

          }
      }

      // ...
      // Then add number of words to a table
      // ...

MicrosoftWordOperations代碼非常基本:

public class MicrosoftWordOperations
{
    private String _file;

    public MicrosoftWordOperations(String file)
    {
        this._file = file;
    }

    public String GetContents()
    {
        object fileName = (object)this._file;
        object missing = System.Reflection.Missing.Value;

        Word.Application wordObject = new Word.Application();
        Word.Document wordDocument = wordObject.Documents.Open(
            ref fileName, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing, ref missing);
        Word.Document activeDocument = wordObject.ActiveDocument;
        String fileContents = activeDocument.Content.Text;
        wordDocument.Close(ref missing, ref missing, ref missing);

        return fileContents;
    }

    public int CountWords(String text)
    {
        MatchCollection collection = Regex.Matches(text, @"[\S]+");
        return collection.Count;
    }
}

編輯:

我能夠進行一些基本的調試,這是第一個代碼塊中捕獲的異常:

System.UnauthorizedAccessException:由於以下錯誤,檢索具有CLSID {000209FF-0000-0000-C000-000000000046}的組件的COM類工廠失敗:80070005訪問被拒絕。 (來自HRESULT的異常:0x80070005(E_ACCESSDENIED))。 在[path] \\ MicrosoftWordOperations.cs:line 26中的MicrosoftWordOperations.GetContents()中,在[path] \\ WordCounter.aspx.cs:line 69中的Content_WordCounter.CountWords()中

編輯:

MSWord已安裝在服務器上。

編輯:

第26行: Word.Application wordObject = new Word.Application();
第69行: String contents = wordOps.GetContents();

引用Microsoft MSKB 257757

Microsoft當前不建議也不支持任何無人參與的非交互客戶端應用程序或組件(包括ASP,ASP.NET,DCOM和NT Services)中的Microsoft Office應用程序自動化,因為Office可能表現出不穩定的行為和/在此環境中運行Office時出現死鎖或死鎖。

因此,您不應該這樣做。 也就是說,讓我們嘗試解決您的問題。 Web服務器上是否安裝了Word? 如果否:您需要安裝它。 如果是,請告訴我們MicrosoftWordOperations.cs哪一行是第26行(錯誤消息中提到的那一行)。

編輯:由於第26行是Word.Application的創建,因此運行Web應用程序的用戶帳戶可能沒有啟動Word所需的權限。 為了驗證這一假設,我建議您以服務器上“常規”用戶的帳戶運行Web應用程序(例如,使用web.config中的<identity ...>標記)。 讓我再次引用上面鏈接的知識庫文章:

用戶身份 :即使運行自動化啟動應用程序,Office應用程序在運行應用程序時也會假定用戶身份。 應用程序嘗試根據啟動應用程序的用戶的用戶注冊表配置單元中的設置來初始化工具欄,菜單,選項,打印機和某些外接程序。 許多服務在沒有用戶配置文件的帳戶下運行(例如SYSTEM帳戶或IWAM_ [servername]帳戶)。 因此,Office可能無法在啟動時正確初始化。 在這種情況下,Office在CreateObject函數或CoCreateInstance函數上返回錯誤。 即使可以啟動Office應用程序,如果沒有用戶配置文件,其他功能也可能無法正常工作。

因此,您的Web應用程序需要在具有用戶配置文件的Windows帳戶下運行。

正如其他人所說,在服務器上自動化Word是一個壞主意。 您是否考慮過替代解決方案?

如果可以單獨使用openxml格式(.docx),則OpenXml SDK是更好的選擇。 如果您必須以舊的.doc格式生成文檔,我建議您看一下第三方組件,例如Aspose,盡管這顯然不是免費的解決方案。

除非該遠程服務安裝了Microsoft Word,否則您將不能依賴該代碼。 該代碼在運行服務器端代碼的計算機上使用COM自動化。

暫無
暫無

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

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