Linq to SharePoint throws a null-reference exception

In our SharePoint 2010 project, we use Linq for SharePoint to get a list of ConfigurationItems. In our test environments, we never had a problem getting data from this list. In our production environment, we sometimes sometimes (we can’t find a template right now), getting an exception with a null reference when going through elements in the list.

The following is an exception to 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 is 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()
    };
}

Does anyone have any ideas on how to track this down to what causes this exception?

UPDATE 05/31/2011:

We found a pattern in which we can reproduce this behavior in our production environment. And also in our test environment, we also had this problem, from which we extracted Crash Dump files using AdPlus.

We see that this happens after restarting the application pool. The only way to fix this error is to run a full IISreset.

crashdump , : : 0xC0000005 : , .

, - ?

+5
3

, LINQ to SharePoint. SQL (Linq to SQL), SharePoint CAML.

+2

Linq Sharepoint, . , , , , , IISReset.

, . , , , -. :

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

:

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

catch, , , , .

+1

. :

  • dataContext SPMetal , .
  • ( DLL GAC)
0

All Articles