【发布时间】:2011-10-07 16:02:45
【问题描述】:
我在从 ASP.NET 站点打开 word 文档时遇到问题。该解决方案在 Windows 2003 Server 上运行良好,但在 Windows 2008 server x64 和 Windows 7 x64 上无法运行。
为了简化解决方案,我创建了一个 ASP.NET MVC 3 站点并尝试从那里打开 word 文档。
我拥有的环境:Windows 7 x64 和 MS Office 2010 x64
打开文档的代码如下:
public ActionResult WordTest()
{
var fullFileName = @"C:\inetpub\wwwroot\fpub\TestDocument.docx";
var impersonation = new ImpersonationManager();
impersonation.Impersonate();
try
{
var application = new Application();
try
{
Type documentsType = application.Documents.GetType();
var document =(_Document)documentsType.InvokeMember("Open", BindingFlags.InvokeMethod, null, application.Documents,
new object[] {fullFileName});
try
{
return View(new ModelData {Result = document == null ? "Bad" : "OK"});
}
finally
{
if (document != null)
{
document.Close(false);
Marshal.ReleaseComObject(document);
}
}
}
finally
{
application.Quit(false);
Marshal.ReleaseComObject(application);
}
}
finally
{
impersonation.CloseImpersonation();
}
}
首先,我模拟使用信任域用户帐户进行单词交互(ImpersonationManager 是一个自定义组件)。此用户有权打开\保存\关闭 Word 应用程序。在我的测试中,这是我自己的帐户 :)
然后我创建 Word 应用程序实例。 WINWORD进程在模拟账户下启动。
但在调用“Open”方法后,它总是返回 null。没有例外,事件查看器中没有信息。
此外,Word 进程在此之后将 CPU 加载到 100%(1 个 CPU 核心)。
如果我运行与控制台应用程序相同的代码(没有模拟),它可以正常工作。
我想知道这里有什么问题?
更新如果使用 Visual Studio 开发服务器作为站点的主机,它可以正常工作
【问题讨论】:
标签: asp.net .net ms-word office-interop