在64位系统的ASP.NET网站ApplicationClass Documents.Open返回null系统、网站、NET、ASP

2023-09-05 01:22:46 作者:烂好人°

我有一个从ASP.NET站点打开word文档中的一个问题。该解决方案工作正常的Windows 2003 Server上,但不能在Windows 2008服务器的x64和Windows工作7 x64的。

I have a problem with opening word document from ASP.NET site. The solution works fine on Windows 2003 Server, but doesn't work on Windows 2008 server x64 and Windows 7 x64.

要简化我创建了一个ASP.NET MVC 3的网站上的解决方案,并尝试从那里打开的Word文档。

To simplify the solution I've created an ASP.NET MVC 3 site and try to open word document from there.

环境,我有:Windows 7的64位和MS Office 2010 64

Environment I have: Windows 7 x64 and MS Office 2010 x64

在code打开adocument如下:

The code for opening adocument is the following:

    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应用程序。在我的测试,这是我自己的帐户:)

Firstly, I make impersonation to use trusted domain user account for word interaction (ImpersonationManager is a custom component). This user has rights to open\save\close Word Application. In my tests this is my own account :)

然后,我创建的Word应用程序实例。在模拟账户开始WINWORD过程。

Then I create Word application instance. WINWORD process is started under impersonated account.

不过,在调用打开方法后,它总是返回null。没有例外,在事件查看器中没有资料。

But after "Open" method is invoked it always returns null. No exceptions, no info in event viewer.

此外字进程加载在此之后(1核CPU)100%的CPU。

Moreover Word process loads the CPU on 100% after this (1 core of CPU).

如果我运行相同的code(不带模拟)作为控制台应用程序,它工作正常。

If I run the same code (without impersonation) as console application it works fine.

我不知道什么可能是这里的问题?

I wonder what can be the problem here?

更新它工作正常,如果Visual Studio开发服务器作为主机的网站

Update It works fine if Visual Studio Development server is used as host for the site

推荐答案

在服务器方案使用Office互操作(如ASP.NET,Windows服务等)MS不支持的 - 见的 http://support.microsoft.com/default.aspx?scid=kb;EN- US; q257757#KB2

Using Office interop in a server-scenario (like ASP.NET, Windows Service etc.) is NOT supported by MS - see http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2

此外也出现了一些与安全相关的,因为Windows Vista的改变,基本上使它真的很难做任何事情类似桌面的在Windows服务(IIS / ASP.NET是Windows服务在这方面只是一个特例)。

Additionally there have been several security-related changed since Windows Vista which basically make it really hard to do anything "desktop-like" in a Windows Service (IIS/ASP.NET is just a special case of Windows Service in this regard).

有几个库(免费的和商业)来处理Office文件(没有Office互操作)......以进一步帮助你需要描述你的目标。

There are several libraries (free and commercial) to deal with Office files (without Office Interop)... to help further you need to describe your goal.