DataPortalMethodCache.GetMethodInfo & ArgumentException "An item with the same key has already been added."

DataPortalMethodCache.GetMethodInfo & ArgumentException "An item with the same key has already been added."

Old forum URL: forums.lhotka.net/forums/t/4375.aspx


vmattila posted on Wednesday, February 20, 2008

Hello all,

I've been evaluating CSLA .NET 3.5 for a while in a test project and it seems to be promising. As soon we'd have compression enabled in WCF DataPortal, CSLA would be perfect!

Currently I have a strange error that I encounter when I fetch a business object in an ASP.NET HTTP Handler. An external system requests the handler, and the handler generally only fetches a business object (BusinessObject.GetObject() that just calls Csla.DataPortal.Fetch(Of BusinessObject)(Criteria)) whichafter the handler updates the object in a simple way.

When external requests happen to hit the handler concurrently, up to four or five requests at the same time, only one request is completed. Other requests will throw an exception, that is:

[ArgumentException: An item with the same key has already been added.]

System.ThrowHelper.ThrowArgumentException(ExceptionResource resource) +48

System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) +2668392

   Csla.Server.DataPortalMethodCache.GetMethodInfo(Type objectType, String methodName, Object[] parameters) +162

   Csla.Server.DataPortalMethodCache.GetFetchMethod(Type objectType, Object criteria) +107

   Csla.DataPortal.Fetch(Type objectType, Object criteria) +524

   Csla.DataPortal.Fetch(Object criteria) +75

I suppose this has something to do with threading, however I haven't found a solution yet how to solve it...

Well, any ideas?

Thanks,
Ville

rasupit replied on Thursday, February 21, 2008

Rocky,
This may be due to no lock in GetMethodInfo when trying to access shared/static dictionary.

vmattila replied on Thursday, February 21, 2008

The missing lock seems to be the reason. I enclosed some CSLA-related code inside a SyncLock in the handler, which solved the problem.

Maybe this kind of issues should be managed at framework level?

Best regards,
Ville

RockfordLhotka replied on Thursday, February 21, 2008

That's why it is beta code Smile [:)]

Thank you for finding this issue - I agree that it is a missing lock in the cache handling.

Copyright (c) Marimer LLC