Thursday, August 15, 2013

Intermittent error: The type initializer for '' threw an exception

I had built a web application to consume SharePoint 2010 Word Automation Services to perform PDF rendition. From time to time, I noticed this exception would be thrown:

System.TypeInitializationException: The type initializer for '' threw an exception. ---> System.TypeInitializationException: The type initializer for '' threw an exception. ---> .ModuleLoadException: The C++ module failed to load while attempting to initialize the default appdomain.
 ---> System.ArgumentException: Invalid token for impersonation - it cannot be duplicated.
   at System.Security.Principal.WindowsIdentity.CreateFromToken(IntPtr userToken)
   at System.Security.Principal.WindowsIdentity..ctor(SerializationInfo info)
   at System.AppDomain.get_Id()

After Googling around I arrived at the conclusion that somehow, the user account I am impersonating have its token expire after prolonged use, and was refused by the Word Automation Services. I'm too lazy to list references and explanations so let's dive into a simple workaround straightaway!



First create a reference to the SharePoint site and web using the webUrl passed in.
Then get the system user token to open a new site with elevated privileges (so that a lot of system level functions could be performed). Of course, you could opt to do that with practically any user you wish to impersonate.

Make the first attempt to consume the services with the system user token.
Add a try...catch block and make the second attempt to consume the services with an alternative user token.

This might not be the most perfect of solutions, but it works pretty well for me. Sample code:

      SPSite s = new SPSite(webUrl);  
      SPWeb w = site.OpenWeb();  
      // rest of code, including using(SPSite siteCol = new SPSite(webUrl, sysToken))  
      try  
     {  
       queueForConversion(siteCol, sysToken, fileToConvert, convertTo);  
     }  
     catch (Exception ex)  
     {  
       // Intermittent exception - Invalid Token for impersonation  
       output += "First conversion attempt failed, trying again as workflow account. ";  
       SPUserToken userToken = w.AllUsers[@"DOMAIN\alternateUser"].UserToken;  
       using (SPSite backup = new SPSite(webUrl, userToken))  
       {  
         queueForConversion(backup, userToken, fileToConvert, convertTo);  
       }  
     }  
      //rest of code  
      //Remember to dispose  
      w.Close();  
      w.Dispose();  
      s.Close();  
      s.Dispose();  
     private void queueForConversion(SPSite site, SPUserToken token, string fileToConvert, string convertTo)  
     {  
       ConversionJob job = null;  
       job = new ConversionJob("Word Automation Services");  
       job.UserToken = token;  
       job.Settings.UpdateFields = true;  
       job.Settings.OutputSaveBehavior = SaveBehavior.AlwaysOverwrite;  
       job.Settings.OutputFormat = SaveFormat.PDF;  
       job.AddFile(fileToConvert, convertTo);  
       job.Start();  
     }  
Related Posts Plugin for WordPress, Blogger...