5

In our SharePoint 2010 project we are using Linq to SharePoint to get a list of ConfigurationItems. On our testing environments we never had issues fetching data from this list. On our production environment we are now sometimes (we cannot find a pattern right now) getting a null reference exception when looping through the items in the list.

Below is the exception whe are getting thrown from the Linq to SharePoint code:

Object reference not set to an instance of an object. StackTrace: 
at Microsoft.SharePoint.Linq.FieldRef.GetHashCode()
at Microsoft.SharePoint.Linq.FieldRef.FieldRefEqualityComparer.GetHashCode(FieldRef obj)
at System.Linq.Set`1.InternalGetHashCode(TElement value)  at System.Linq.Set`1.Find(TElement value, Boolean add)  at System.Linq.Enumerable.d__7a`1.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)  at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)  at Microsoft.SharePoint.Linq.SelectMappingInfo.GetDistinctMappedFields()
at Microsoft.SharePoint.Linq.Rules.PushDownProcessor.SelectWithInfoOp.PushDownSelect(Context ctx)
at Microsoft.SharePoint.Linq.Rules.PushDownProcessor.SelectWithInfoOp.Process(Context ctx)
at Microsoft.SharePoint.Linq.Rules.GuardedRule`4.c__DisplayClass7.b__6(TSourceBase src, TContext ctx)
at Microsoft.SharePoint.Linq.Rules.RewriteRule`2.Apply(TNode src, TContext ctx)  at Microsoft.SharePoint.Linq.Rules.CacheRule`3.Apply(TSource src, TContext ctx)  
at Microsoft.SharePoint.Linq.Rules.PushDownProcessor.b__0(Expression e, Context ctx)
at Microsoft.SharePoint.Linq.Rules.ChildRule`2.Apply(TNode src, TContext ctx)  at Microsoft.SharePoint.Linq.Rules.PushDownProcessor.b__3(Expression e, Context ctx)
at Microsoft.SharePoint.Linq.Rules.RewriteRule`2.Apply(TNode src, TContext ctx)  at Microsoft.SharePoint.Linq.Rules.CacheRule`3.Apply(TSource src, TContext ctx)  
at Microsoft.SharePoint.Linq.SPLinqProvider.Rewrite(Expression expression, List`1& assumptions)
at Microsoft.SharePoint.Linq.SPLinqProvider.RewriteAndCompile[T](Expression expression, List`1& assumptions)
at Microsoft.SharePoint.Linq.LinqQuery`1.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)  
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)  
at Common.Configuration.ConfigurationRepository.GetConfiguration(String siteUrl) InnerException:

Source: Microsoft.SharePoint.Linq TargetSite: Int32 GetHashCode()

And here the code we use in our GetConfiguration method.

using (SpDataContext dataContext = new SpDataContext(siteUrl))
{
    result = new ConfigurationModel()
    {
        Configurations = (from item in dataContext.GasportConfiguration
                          select new ConfigurationItem()
                          {
                              Key = item.Key,
                              Value = item.Value,
                              Environment = (Environment)Enum.Parse(typeof(Environment), item.Environment.ToString(), true)
                          }).ToList()
    };
}

Anyone having ideas on how to track this down to what's causing this exception?

UPDATE 31-05-2011:

We found a pattern in which way we can reproduce this behaviour on our production environment. And also on our test environment we had this issue too, from which we extracted some Crash Dump files by using AdPlus.

We see this behaviour occuring after the application pool is recycled. The only way to fix this error is to perform a full IISreset.

In the crashdump analysis I found an exception message that states: Exception Code: 0xC0000005 Exception Information: The thread tries to read from or write to a virtual address for which it does not have the appropriate access.

Hope that someone could give me some more information on this exception?

5
  • Did you solve the problem here? We are getting the same thing. May 25, 2011 at 0:37
  • No, we did not solve our problem. The strange thing is that only one of our frontends is having this issue. The other one is working perfect. We found out some more information about this issue about how to reproduce. The problems start to occur after an recycle of the app-pool. May 25, 2011 at 5:32
  • I have had random issues of issues where the context is not fully reloaded and only partially unavailable. This as well was after an application reset without IIS reset as well. Due to this we have not spent any time recompiling but simply performing an IIS reset when this occurs to resolve the issue. Not sure if this helps or not. We are current on all versions as well as most hotfixes. May 31, 2011 at 14:12
  • That's our workaround for now too. Although I'am not happy with this workaround. Performing a application pool recycle would cause less unavailibility to the user. May 31, 2011 at 14:38
  • 1
    One other thing that may be worth noting - we had the issue after passwords got reset on the user account running the app pool. Using the Set-SPManagedAccount powershell command to update the account may have fixed our issue - it has not happened again so far. Jun 10, 2011 at 4:21

3 Answers 3

2

Unfortunately we haven't found a solution for this issue and decided to move away from LINQ to SharePoint. For a few lists we have changed it to SQL tables (Linq to SQL) and for the SharePoint Lists we moved back to CAML.

1

In our project we use Linq to Sharepoint heavily and were having this problem come up intermittently, and only on production. The only thing that seemed to work when it happened was an IISReset.

We dug into the problem a little deeper and still don't know what causes it to happen. But we found that you can automatically fix it when it occurs by clearing some private cache variables. Here's our code:

public static class SharePointless
{
    public static void Reset()
    {
        var assembly = Assembly.GetAssembly(typeof(EntityList<>));
        var providerType = assembly.GetType("Microsoft.SharePoint.Linq.SPLinqProvider");
        var singleton = providerType.GetField("Singleton", BindingFlags.Static | BindingFlags.Public);
        if (singleton == null) throw new Exception("Expected field doesn't exist in SPLinqProvider");
        singleton.SetValue(null, Activator.CreateInstance(providerType));

        var itemMappingInfoType = assembly.GetType("Microsoft.SharePoint.Linq.SPItemMappingInfo");
        var cachedMappings = itemMappingInfoType.GetField("cachedMappings", BindingFlags.Static | BindingFlags.NonPublic);
        if (cachedMappings == null) throw new Exception("Expected field doesn't exist in SPItemMappingInfo");
        cachedMappings.SetValue(null, null);
    }
}

Example usage:

public static void MyMethod(bool retry)
{
    try
    {
        using (var db = new MyDataContext())
        {
            DoStuff(db);
        }
    }
    catch (NullReferenceException ex)
    {
        if (retry && ex.StackTrace.Contains("FieldRef.GetHashCode"))
        {
            SharePointless.Reset();
            MyMethod(false);
        }
    }
}

In our case we also had the catch code send us an email so we knew that the problem happened and the fix worked.

0

We came across the same thing. The following steps solved it:

  • Regeneration of the dataContext class via SPMetal on the server causing the problem
  • Reapply the class to your project and rebuild
  • Redeploy the solution (copying the DLL to the GAC worked)
4
  • Do you have the ability to compare the datacontext that was generated on your dev environment and the server that's causing the problem? I thought that there should be no difference between the contexts on both servers. May 25, 2011 at 5:34
  • We did compare both generated files - there were subtle differences - version number was slightly different too in comments at the top, and a few lines here and there were added. May 25, 2011 at 11:04
  • Ok, but if I hear you than there where no real differences that could have caused this behaviour from occuring. I will see if I have an spmetal instance on our production environment and generate the datacontext so I can compare them. Do you have lookup fields in the list that's causing the errors to start? May 25, 2011 at 11:26
  • yep there are lookup fields in the list. The above steps seemed to fix - but it's only been a day since the occurance, so not sure if it'll happen again, especially if it's not spmetal related and an more app pool issue. May 25, 2011 at 23:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.