locked
SharePoint 2010 Excel Services says "Safe handle has been closed" RRS feed

  • Question

  • I am trying to programmatically access Excel web services from a SharePoint 2010 virtual machine (single server). To connect to Excel services, I am direct linking to the Microsoft.Office.Excel.Server.WebServices.dll. The code I am using looks something like this:

    Status[] status;
    ExcelService service = new ExcelService();
    string id = service.OpenWorkbook("http://server/ExcelDocLib/MyDoc.xlsx", "en-US", "en-US", out status);

    The call to OpenWorkbook always works on the first request, but then throws an exception on all subsequent requests. The exception states "Safe handle has been closed". Here is the callstack:

       at System.StubHelpers.StubHelpers.SafeHandleC2NHelper(Object pThis, IntPtr pCleanupWorkList)
       at Microsoft.Win32.Win32Native.GetTokenInformation(SafeTokenHandle TokenHandle, UInt32 TokenInformationClass, SafeLocalAllocHandle TokenInformation, UInt32 TokenInformationLength, UInt32& ReturnLength)
       at System.Security.Principal.WindowsIdentity.GetTokenInformation(SafeTokenHandle tokenHandle, TokenInformationClass tokenInformationClass, UInt32& dwLength)
       at System.Security.Principal.WindowsIdentity.get_User()
       at System.Security.Principal.WindowsIdentity.GetName()
       at System.Security.Principal.WindowsIdentity.get_Name()
       at Microsoft.Office.Excel.Server.WebServices.ApiShared.ExecuteServerSessionMethod(Boolean hasSessionId, String sessionId, CoreServerSessionMethod coreWebMethod, String name)
       at Microsoft.Office.Excel.Server.WebServices.ExcelService.OpenWorkbookInternal(String workbookPath, Boolean editingMode, String uiCultureName, String dataCultureName, Boolean newWorkbook, Status[]& status)
       at Microsoft.Office.Excel.Server.WebServices.ExcelService.OpenWorkbook(String workbookPath, String uiCultureName, String dataCultureName, Status[]& status)
       at ExcelSessionConnection.Open()

    From what I've seen on other forums, it sounds like this may have something to do with IIS 7 running in integrated mode, which is a requirement for SharePoint 2010. This exact same code works fine running under SharePoint 2007 with IIS in classic mode. I would appreciate any advice.

    Tuesday, June 29, 2010 3:54 PM

Answers

  • How are you authenticating with the WFE code? As part of what Sharepoint request is the code running?

    As a workaround can you try cloning the windowidentity and reset it on the thread before each call and then restoring.

    WindowsPrincipal cache = System.Threading.Thread.CurrentPrincipal;
    WindowIdentity wiClone = new WindowIdentity (System.Security.Principal.WindowsIdentity.Token)
    System.Threading.Thread.CurrentPrincipal = new WindowsPrincipal(wiClone);

    try
    {
    // run excel api call

    }
    finally
    {
    System.Threading.Thread.CurrentPrincipal = cache;
    wiClone.Dispose();
    }

     

    Please let us know.


    Fred Ellis - MSFT
    Wednesday, July 14, 2010 4:30 PM

All replies

  • Hi Geojason,

    DirectLinking only works in the context of a SharePoint application (i.e. WebPart, ASPX etc).Not in other environments (console app, workflow etc.)

    In what environment are you trying to use this?

    s


    Shahar Prish Blog: http://blogs.msdn.com/CumGranoSalis Professional Excel Services: http://www.amazon.com/Professional-Excel-Services-Programmer/dp/0470104864
    Tuesday, July 6, 2010 8:52 PM
  • It is a SharePoint application. The code base we are using to create the connection is shared between our WSS 3.0 and SharePoint 2010 solutions, and it works in WSS 3.0. This leads me to believe that it is a setup/configuration problem on our part, but maybe not. Does this specific error message hold any meaning for you?
    Friday, July 9, 2010 2:34 PM
  • Saturday, July 10, 2010 6:45 AM
  • How are you authenticating with the WFE code? As part of what Sharepoint request is the code running?

    As a workaround can you try cloning the windowidentity and reset it on the thread before each call and then restoring.

    WindowsPrincipal cache = System.Threading.Thread.CurrentPrincipal;
    WindowIdentity wiClone = new WindowIdentity (System.Security.Principal.WindowsIdentity.Token)
    System.Threading.Thread.CurrentPrincipal = new WindowsPrincipal(wiClone);

    try
    {
    // run excel api call

    }
    finally
    {
    System.Threading.Thread.CurrentPrincipal = cache;
    wiClone.Dispose();
    }

     

    Please let us know.


    Fred Ellis - MSFT
    Wednesday, July 14, 2010 4:30 PM
  • Sorry for the long delay, I was out of town. Fred El's suggestion appears to have worked. Could you please explain a little more about what you think the problem is, and how this workaround fixes it?

    Also, do you have any suggestions as to how to fix the problem without needing this workaround? Do you think the problem is with my code, or is it an environmental issue with my SharePoint setup? Any advice would be appreciated.

    Thanks a lot for your help.

    Tuesday, July 27, 2010 8:30 PM
  • I am not aware of the particulars but this issue is known about internally and being investigated currently by our Excel Services team.  This is the only workaround currently. I will try to post more about this here as it becomes available.
    Fred Ellis - MSFT
    Wednesday, July 28, 2010 1:03 PM
  • Alright, please do let me know if you get any more information about this issue. In the mean time, thanks again for your help.
    Wednesday, July 28, 2010 8:02 PM
  • I am getting "You do not have permissions to open this file." errors.  I have tried both using a console app and a visual web part.  I am trying everythign and can't get anything to work.  I have tried using impersonation and credentials stored in the secure store service.  I have also tried setting the credentials to defaultcredentials, and passing in my windows credentials.  I am trying your code, but I am getting compile errors.

    Error 3 An object reference is required for the non-static field, method, or property 'System.Security.Principal.WindowsIdentity.Token.get'
    Error 1 Cannot implicitly convert type 'System.Security.Principal.IPrincipal' to 'System.Security.Principal.WindowsPrincipal'. An explicit conversion exists (are you missing a cast?)
    Error 2 The type or namespace name 'WindowIdentity' could not be found (are you missing a using directive or an assembly reference?)

    Here is my thread if you would like to take a look:

    http://social.technet.microsoft.com/Forums/en-US/sharepoint2010programming/thread/e013cbc2-91a1-4297-8c9b-7bf691618617

    Tuesday, October 4, 2011 6:57 PM