开发者

ApplicationClass Documents.Open returns null for ASP.NET site on 64bit system

开发者 https://www.devze.com 2023-04-11 06:12 出处:网络
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.

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.

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

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

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

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 :)

Then I create Word application instance. WINWORD process is started under impersonated开发者_开发知识库 account.

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

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

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

I wonder what can be the problem here?

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


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

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).

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


Regardless the accepted answer some people need to support legacy code. The solution to the problem can be found here: http://social.msdn.microsoft.com/Forums/en-US/0f5448a7-72ed-4f16-8b87-922b71892e07/word-2007-documentsopen-returns-null-in-aspnet

0

精彩评论

暂无评论...
验证码 换一张
取 消