【问题标题】:ASP.NET Code works on Local Machine OnlyASP.NET 代码仅适用于本地计算机
【发布时间】:2011-06-10 04:02:03
【问题描述】:

我在建立一个 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:检索 COM 类工厂 对于具有 CLSID {000209FF-0000-0000-C000-000000000046} 的组件失败 由于以下错误:80070005 访问被拒绝。 (例外来自 HRESULT:0x80070005(E_ACCESSDENIED))。在 MicrosoftWordOperations.GetContents() 在 [路径]\MicrosoftWordOperations.cs:第 26 行 [路径]\WordCounter.aspx.cs:第 69 行中的 Content_WordCounter.CountWords()

编辑:

MSWord 已安装在服务器上。

编辑:

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

【问题讨论】:

  • 可能要添加错误信息...
  • 不要在服务器中自动化 Word。
  • @AUSteve,添加了例外。

标签: c# asp.net permissions ms-word access-denied


【解决方案1】:

除非远程服务安装了 Microsoft Word,否则您不能依赖该代码。该代码正在运行您的服务器端代码的机器上使用 COM 自动化。

【讨论】:

  • 不确定我是否完全遵循。 MS Word 已安装在服务器上。
【解决方案2】:

引用微软MSKB 257757:

Microsoft 当前不推荐也不支持任何无人值守、非交互式客户端应用程序或组件(包括 ASP、ASP.NET、DCOM 和 NT 服务)的 Microsoft Office 应用程序自动化,因为 Office 可能表现出不稳定Office 在此环境中运行时出现的行为和/或死锁。

所以,你不应该这样做。也就是说,让我们尝试解决您的问题。 Web 服务器上是否安装了 Word?如果否:您需要安装它。如果是,请告诉我们MicrosoftWordOperations.cs 的哪一行是第26 行(错误信息中提到的那一行)。

编辑:由于第 26 行是 Word.Application 的创建,运行您的 Web 应用程序的用户帐户可能没有启动 Word 所需的权限。为了验证这个假设,我建议您在服务器上的“普通”用户帐户下运行 Web 应用程序(例如,使用 web.config 中的 &lt;identity ...&gt; 标记)。让我再次引用上面链接的知识库文章:

用户身份:Office 应用程序在运行应用程序时假定用户身份,即使自动化启动应用程序时也是如此。应用程序尝试根据启动应用程序的用户的用户注册表配置单元中的设置来初始化工具栏、菜单、选项、打印机和一些加载项。许多服务在没有用户配置文件的帐户下运行(例如 SYSTEM 帐户或 IWAM_[servername] 帐户)。因此,Office 可能无法在启动时正确初始化。在这种情况下,Office 在 CreateObject 函数或 CoCreateInstance 函数上返回错误。即使可以启动 Office 应用程序,如果不存在用户配置文件,其他功能也可能无法正常工作。

因此,您的网络应用程序需要在具有用户配置文件的 Windows 帐户下运行。

【讨论】:

  • 编辑了 OP。是的,它已安装。
  • 抱歉我的无知,这是我第一次接触网络应用程序。我似乎在 web.config 中没有身份标签。看来我需要做更多的研究。如果有什么不同,我在我的 OP 中生成异常时,我以“常规”用户身份运行它。
  • @Eric:这与运行 Web 浏览器的用户无关。这是关于 IIS 内部使用的用户帐户。那是不同的东西。我的回答中&lt;identity ...&gt; 后面的链接应该提供有关该主题的一些介绍性信息。
  • 感谢您提供的所有信息。很明显,我需要做更多的阅读,感谢您为我指明了正确的方向。对于这个特定的应用程序,我想我会探索非 MSWord 的替代方案。谢谢。
【解决方案3】:

正如其他人所说,在服务器上自动化 Word 是个坏主意。您是否考虑过替代解决方案?

如果您可以独占使用 openxml 格式 (.docx),OpenXml SDK 是更好的选择。如果您必须以较旧的 .doc 格式生成文档,我建议您查看像 Aspose 这样的第 3 方组件,尽管这显然不是免费的解决方案。

【讨论】:

  • 感谢您的参考。我可能只能使用 .docx。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-16
  • 2016-01-28
  • 2018-11-16
  • 2017-02-25
  • 2023-04-05
相关资源
最近更新 更多