Hi Fabrice, 

Your use of concurrent collection is fine. It allows you to establish 
per-company scope in a thread safe manner, and I see nothing wrong with it.

However, since within a scope you can observe non-tread safe access (multiple 
calls to your WCF service from a single company) you will need to also build a 
thread safe lifetime scope.

The DefaultLifetimeScope is built to not be thread safe. This is the relevant 
piece of code:
public Burden GetCachedInstance(ComponentModel instance, 
ScopedInstanceActivationCallback createInstance)
{
var burden = scopeCache[instance];
if (burden == null)
{
scopeCache[instance] = burden = createInstance(onAfterCreated);
}
return burden;
}


As you can see if two threads enter the method, between first and third line 
the problem you're experiencing may occur.

HTH
-- 
Krzysztof Kozmic


On Friday, 15 February 2013 at 12:04 AM, Fabrice wrote:

> 
> Hi!
> 
> I'm using a custom lifestyle scope in order to create component based on a 
> value stored on the thread level.
> Typically, each call to my WCF service require a user authentication, and 
> user is linked to a customer. And I want 1 component for each customer, no 
> matter how many users (linked to this customer) are connected.
> 
> So I created the PerCompanyScopeAccessor which is quite simple (see 
> attachment)
> 
> Problem:
> If I've several concurrent request on the service, which call the Resolve 
> method for this component, I've this crash:
> 
> System.ArgumentException: An item with the same key has already been added.
>    at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
>    at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, 
> Boolean add)
>    at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
>    at Castle.MicroKernel.Lifestyle.Scoped.ScopeCache.set_Item(Object id, 
> Burden value)
>    at 
> Castle.MicroKernel.Lifestyle.Scoped.DefaultLifetimeScope.GetCachedInstance(ComponentModel
>  instance, ScopedInstanceActivationCallback createInstance)
>    at 
> Castle.MicroKernel.Lifestyle.ScopedLifestyleManager.Resolve(CreationContext 
> context, IReleasePolicy releasePolicy)
>    at Castle.MicroKernel.Handlers.DefaultHandler.ResolveCore(CreationContext 
> context, Boolean requiresDecommission, Boolean instanceRequired, Burden& 
> burden)
>    at Castle.MicroKernel.Handlers.DefaultHandler.Resolve(CreationContext 
> context, Boolean instanceRequired)
>    at Castle.MicroKernel.Handlers.AbstractHandler.Resolve(CreationContext 
> context)
>    at Castle.MicroKernel.DefaultKernel.ResolveComponent(IHandler handler, 
> Type service, IDictionary additionalArguments, IReleasePolicy policy)
>    at 
> Castle.MicroKernel.DefaultKernel.Castle.MicroKernel.IKernelInternal.Resolve(Type
>  service, IDictionary arguments, IReleasePolicy policy)
>    at Castle.MicroKernel.DefaultKernel.Resolve(Type service, IDictionary 
> arguments)
>    at Castle.Windsor.WindsorContainer.Resolve(Type service)
> 
> 
> In my PerCompanyScopeAccessor I try to handle concurrent access using a 
> ConcurrentCollection, but it seems the crash come from the ScopeCache ?
> 
> Does someone know what happen there ?
> 
> Thank you for any help,
> Fabrice
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Castle Project Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected] 
> (mailto:[email protected]).
> To post to this group, send email to [email protected] 
> (mailto:[email protected]).
> Visit this group at http://groups.google.com/group/castle-project-users?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>  
>  
> 
> Attachments: 
> - PerCompanyScopeAccessor.cs
> 


-- 
You received this message because you are subscribed to the Google Groups 
"Castle Project Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/castle-project-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to