New Posts New Posts RSS Feed: NullReferenceException on accessing navigation property
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

NullReferenceException on accessing navigation property

 Post Reply Post Reply
Author
jmpinchot View Drop Down
Newbie
Newbie
Avatar

Joined: 07-May-2010
Location: Florida
Posts: 22
Post Options Post Options   Quote jmpinchot Quote  Post ReplyReply Direct Link To This Post Topic: NullReferenceException on accessing navigation property
    Posted: 11-Jun-2010 at 7:56am
I seem to randomly get null reference exceptions when accessing a navigation property of an entity. It doesn't occur reliably, nor always on the same property of the same entity, and can seemingly occur on any query regardless of how long the application has been running.
 
I've tried to disable application pool recycling as mentioned in the Troubleshooting pdf in the learning resources, as well as upgrading to 6.0.3.1 that was released yesterday.
 
Any ideas?
 
System.NullReferenceException: Object reference not set to an instance of an object.
   at IdeaBlade.EntityModel.NavigationListEntityProperty`2.GetValueForSerialization(EntityWrapper ew)
   at IdeaBlade.EntityModel.NavigationListEntityProperty`2.GetValue(TInstance instance)
   at TradePMR.eCustody.Data.Models.Core.DbLoginImage.get_Users()
   at WriteDbLoginImageToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , ClassDataContract )
   at System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle)
   at WriteDbUserToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , ClassDataContract )
   at System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, Boolean verifyKnownType, RuntimeTypeHandle declaredTypeHandle)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiType(XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle objectTypeHandle, Type objectType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle, Type declaredType)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle)
   at WriteSessionBundleToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , ClassDataContract )
   at System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
   at System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer, Object graph)
   at System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator writer, Object graph)
   at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer, Object graph)
   at System.Runtime.Serialization.XmlObjectSerializer.WriteObject(XmlDictionaryWriter writer, Object graph)
   at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeParameterPart(XmlDictionaryWriter writer, PartInfo part, Object graph)
   at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeParameters(XmlDictionaryWriter writer, PartInfo[] parts, Object[] parameters)
   at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeBody(XmlDictionaryWriter writer, MessageVersion version, String action, MessageDescription messageDescription, Object returnValue, Object[] parameters, Boolean isRequest)
   at System.ServiceModel.Dispatcher.OperationFormatter.OperationFormatterMessage.OperationFormatterBodyWriter.OnWriteBodyContents(XmlDictionaryWriter writer)
   at System.ServiceModel.Channels.BodyWriter.WriteBodyContents(XmlDictionaryWriter writer)
   at System.ServiceModel.Channels.BodyWriterMessage.OnWriteBodyContents(XmlDictionaryWriter writer)
   at System.ServiceModel.Channels.Message.OnWriteMessage(XmlDictionaryWriter writer)
   at System.ServiceModel.Channels.Message.WriteMessage(XmlDictionaryWriter writer)
   at System.ServiceModel.Channels.BufferedMessageWriter.WriteMessage(Message message, BufferManager bufferManager, Int32 initialOffset, Int32 maxSizeQuota)
   at System.ServiceModel.Channels.BinaryMessageEncoderFactory.BinaryMessageEncoder.WriteMessage(Message message, Int32 maxMessageSize, BufferManager bufferManager, Int32 messageOffset)
   at System.ServiceModel.Channels.HttpOutput.SendAsyncResult.SendWithoutChannelBindingToken()
   at System.ServiceModel.Channels.HttpOutput.BeginSend(TimeSpan timeout, AsyncCallback callback, Object state)
   at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.SendWebRequest()
   at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.BeginSendRequest(Message message, TimeSpan timeout)
   at System.ServiceModel.Channels.RequestChannel.BeginRequest(Message message, TimeSpan timeout, AsyncCallback callback, Object state)
   at System.ServiceModel.Dispatcher.RequestChannelBinder.BeginRequest(Message message, TimeSpan timeout, AsyncCallback callback, Object state)
   at System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.StartSend(Boolean completedSynchronously)
Back to Top
GregD View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 374
Post Options Post Options   Quote GregD Quote  Post ReplyReply Direct Link To This Post Posted: 11-Jun-2010 at 2:31pm
You're probably trying to reference them before they've been retrieved. (Remember, all data retrieval is asynchronous in Silverlight.)

You have two choices:

  1. Preload the data you'll need for the navigation properties if you can anticipate what it is

  2. Write a handler for the PendlingEntityResolved event of the property's containing entity so you can delay your use of the navigation property until its data has actually come back

Option 1 is a lot less complicated if you can do it.
Back to Top
kimj View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 1391
Post Options Post Options   Quote kimj Quote  Post ReplyReply Direct Link To This Post Posted: 12-Jun-2010 at 9:51am
Are you accessing DbLoginImage.Users from the client when the error occurs, or issuing a query?  I assume this stack trace is from the server's debug log, and the exception during serialization is definitely a concern to us.
 
When you upgraded to 6.0.3.1 did you re-save your entity model?  The change between 6.0.3.0 and 6.0.3.1 was in code generation.  These changes are needed for both .NET projects and linked SL projects, so be sure to re-build too.
 
If you've done all this and are still seeing this problem, can you provide more information on you configuration, and also if you've added any custom properties, and if inheritance or an injected base type is used.
Back to Top
jmpinchot View Drop Down
Newbie
Newbie
Avatar

Joined: 07-May-2010
Location: Florida
Posts: 22
Post Options Post Options   Quote jmpinchot Quote  Post ReplyReply Direct Link To This Post Posted: 14-Jun-2010 at 6:10am
The error only occurs when issuing a query. Our code never directly accesses the value in the Users property of the DbLoginImages (from the stack trace). But again the problem is not limited to that one entity.
 
We do not use inheritance or inject a base type. However our DbUser entity does serve as our IPrincipal and IIdentity implementation and as such there are a number of custom properties on it. We chose this route since the entity itself contains all the information we need to perform our 2-step authentication process: first username/password and then ensuring the select the proper image.
 
For the moment DbUser remains our only class with custom properties on them, however I expect that to change in the very near future.
 
Our partial class of the DbUser object looks like this:
 
public partial class DbUser : IbEm.Entity, IPrincipal, IIdentity
{
    #region IPrincipalMembers
 
    public IIdentity
    {
        get { return this; }
    }
 
    public bool IsInRole(string role)
    {
         // We dont support roles
         return false;
    }
 
    #endregion
 
    #region IIdentity Members
 
    public string AuthenticationType
    {
        get { return "Forms"; }
    }
 
    [DataMember]
    public bool IsAuthenticated { get; set; }
 
    public string Name
    {
        get { return FirstName + " " + LastName; }
    }
 
    #endregion
 
    [DataMember]
    public bool SkipAuthentication { get; set; }
}
Back to Top
kimj View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 1391
Post Options Post Options   Quote kimj Quote  Post ReplyReply Direct Link To This Post Posted: 15-Jun-2010 at 7:35pm
We haven't been able to reproduce this problem.  I want to double-check that your model was re-generated with version 6.0.3.1 - you can spot this by the inclusion of a static constructor in the EntityRelations class:
 
public partial class EntityRelations : IbEm.IEntityRelations {
  static EntityRelations() {}
....
 
If the generated EntityRelations class does contain this constructor, then a few more questions for you -
 - When the error occurs for a query, has that query been seen before by the server?
 - For a query which fails, will it succeed if run again?
 - Do you have multiple EDMX in your solution?
 
 
Back to Top
jmpinchot View Drop Down
Newbie
Newbie
Avatar

Joined: 07-May-2010
Location: Florida
Posts: 22
Post Options Post Options   Quote jmpinchot Quote  Post ReplyReply Direct Link To This Post Posted: 16-Jun-2010 at 2:13pm
I was able to resolve this issue by simply moving the IPrincipal and IIdentity stuff out to a custom subclass of your UserBase. After doing that, I've never seen it fail again.

However just in case you want to continue exploring this error, I can answer you questions.

I want to double-check that your model was re-generated with version 6.0.3.1 - you can spot this by the inclusion of a static constructor in the EntityRelations class


I was able to see this in my generated code file

When the error occurs for a query, has that query been seen before by the server?


The easiest way I found to reproduce this error was to continue running one of our search queries again and again with different search strings. Usually it would throw the exception within 5-10 attempts. The search query is rebuilt each time though with the only value changing being the search text for the Where clause. So I believe the answer here is no.

For a query which fails, will it succeed if run again?


Sometimes. But as I mentioned above, it wasn't the same instance of the query, so this may be moot.


Do you have multiple EDMX in your solution?


Nope.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down