Closed wk-pl closed 8 years ago
Hi
I assume that someMethodToCreateIMyCasssObject uses somwhere inside IEntityContext.Create or IEntityContext.Load to actually create instances of the IMyClass interface.
Unfortunately, it's hard to figure it out what might be wrong here. Is there anything else between your loop that puts those instances to the myClassContainer and _externalLibraryObject.processMyClassBatch ?
From your description looks like something disposes your context causing your instances to be empty. Maybe some other thread is doing this? This actually may be the reason you find it working correctly when debugging, but it fails otherwise.
Please sched some more light, i.e. more complete piece of code so I can figure it out. I'm currently working on another private project that utilizes RomanticWeb and creazy stuff I'm doing there is far more complicated than your case thus that ust be something relatively easy to pinpoint.
Yes, the someMethodToCreateIMyClassObject uses entityContext.Create inside and creates the object based on some additional data (not coming from RomanticWeb entities).
The loop I described in the previous post was a simplification of the algorithm I'm using. To be more precise, the code looks more like this:
Class1
public void SomeBaseMethod()
{
var data = GetSomeData(...);
foreach (MyClassBase myClassBaseObject in data)
{
if (DataAvailable != null)
{
DataAvailable(this, new DataAvailableEventArgs<MyClassBase>(myClassBaseObject));
}
}
}
Class2
internal void OnDataAvailable(object sender, DataAvailableEventArgs<MyClassBase> e)
{
var entityContext = (...);
// the someMethodToCreateIMyClassObject method is in fact an extension
// method for the MyClassBase class
IMyClass myClassObject = e.MyClassBase.someMethodToCreateIMyClassObject(entityContext);
if (
myClassObject != null
&& !string.IsNullOrEmpty(myClassObject.SomeIdentifier)
&& myClassObject.SomeCollection.Any()
&& !string.IsNullOrEmpty(myClassObject,SomeCollection.FirstOrDefault().SomeStringField
)
{
myClassContainer.Add(myClassObject);
}
if (myClassContainer.Count >= batchSize)
{
_externalLibraryObject.ProcessMyClassBatch(myClassContainer);
}
(... some other logic ...)
entityContext.Dispose();
(... some other logic ...)
}
So as you can see, the container is populated in a loop, that fires the above event. Inside the event the IMyClass object is created, using a context created from a context factory and at the end of the event it's being disposed. Just to make it clear, everything works fine only when I peek the data I need to persist (either while debugging when stopped on a breakpoint or when the data I need is checked, like in the if clause containing myClassContainer population). So it doesn't have to be while debugging - with the above code it works always, but the solution itself is unacceptable.
I'd very much like to use a proper solution and to understand why exactly the problem is occurring.
It is exactly what I was thinking it is.
Your event handler OnDataAvailable creates a context and disposes it at the end. The batch processing sometimes happens when the myClassContainer.Count hits the batchSize, but the objects in myClassContainer has the context already disposed.
You can't dispose it if the instance will be used later on, thus it implies that the context needs to be disposed once the batch processing does it's job, i.e. when removing items from collection.
Hope it helps - feel free to ask more questions if it doesn't help you!
Of course you were right :)
I managed to get rid of the ugly 'if' workaround.
Thanks for your fast response!
You're welcome - glad to help!
Hi,
I'm working on a project that utilizes RomanticWeb and stumbled upon a problem. I have an interface (IMyClass) implementing RomanticWeb.Entities.IEntity.
and the IEntity also implements RomanticWeb.Entities.IEntity.
After I create the object and pass it to another class library to be processed further everything's working fine.
However, I needed to do the same processing of IMyClass objects in a batch, passing a List to the library, like below:
In this case I'm getting NullReferenceExceptions inside the external library, when trying to use the fields of IMyClass objects inside the list. When I debug the solution it occurs that almost all the fields in IMyClass objects in the list are null.
The crazy part is that when I was debugging and peeked inside of the IMyClass object before adding to the list all the fields were correctly filled, but if I didn't stop to see those fields - they were all passed with null values. Alternatively, I added a quick check whether the specific fields needed later in the other library are not null and not empty and only then adding the IMyClass object to list - everything was fine (see below code).
Such a solution, however, is of course unacceptable in the long run.
Do you have any idea what can I do for the fields to remain filled?