This project is read-only.

Occasional System.InvalidOperationException: WCF scopes can not be nested (

Jun 17, 2013 at 11:29 PM
Edited Jun 17, 2013 at 11:30 PM
I am using SimpleInjector.Integration.Wcf and I get this error frequently but not always when calling my service. I saw that the previous version said it fixed an bug concerning this error. Here is the stack trace..

Handling an exception. Exception details: System.InvalidOperationException: WCF scopes can not be nested.
at SimpleInjector.Integration.Wcf.WcfOperationScopeManager.BeginScope()
at SimpleInjector.SimpleInjectorWcfExtensions.BeginWcfOperationScope(Container container)
at SimpleInjector.Integration.Wcf.SimpleInjectorInstanceProvider.GetInstance(InstanceContext instanceContext)
at SimpleInjector.Integration.Wcf.SimpleInjectorInstanceProvider.GetInstance(InstanceContext instanceContext, Message message)
at System.ServiceModel.InstanceContext.GetServiceInstance(Message message)
at System.ServiceModel.Dispatcher.InstanceBehavior.EnsureServiceInstance(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)</Description>

My registrations look like this..
        // Register a channel factory for the Client to get connections from
        container.RegisterSingle(new ChannelFactory<IMessageExchange<PolarisMessage>>(Endpoints<PolarisMessage>.SendEndpoint(new Uri(serviceBusBaseAddress, Descriptions.ApiInstanceTopic.Path))));

        // Register the cache we will use to get responses
        container.Register(() => new DataCache(Azure.Common.Constants.Cache.DefaultCacheName));

        // Register a new Client for every call
        container.Register<IMessageExchange<Message>, Client<Message>>();

        // Register an object to query the cache
        container.RegisterPerWcfOperation<IGet<Message>, GetFromCache<Message>>();

        // Register an object to query the cache
        container.RegisterPerWcfOperation<IPut<Message>, PutInCache<Message>>();

        // Register an object to do both
        container.RegisterPerWcfOperation<IGetAndPut<Message>, GetAndPutInCache<Message>>();

        // Register the rating service which will make calls for us
        container.RegisterPerWcfOperation<IRatingService, RatingService>();

        // Set the container

Basically, my service method sends a message to an Azure queue (using wcf), writes to a cache, and returns a guid. Not complicated stuff, and it seems to work 80%ish of the time.

Jun 18, 2013 at 12:37 AM
Ok. Semms it doesnt like my binding. Specifically I am using this binding, and adding the "establishSecurityContext="false"" bit seems to have cured it...
    <binding maxReceivedMessageSize="1310720" messageEncoding="Text" maxBufferPoolSize="13107200">
      <security mode="TransportWithMessageCredential">
        <message clientCredentialType="UserName" establishSecurityContext="false"  />
      <readerQuotas maxDepth="16" maxStringContentLength="1310720" maxArrayLength="1310720"
                    maxBytesPerRead="1310720" maxNameTableCharCount="1310720" />
Jun 18, 2013 at 9:49 AM
This issue has been reported before. Some configurations WCF seem to trigger a second call to InstanceProvider.GetInstance which causes this exception. The issue unfortunately never got fixed, because I didn't really understand the cause of the problem. Unfortunately my WCF knowledge is not deep enough. But perhaps the solution is to simply detect if we're already running inside the context of a WCF scope and in that case ignore.

If I provide you with a beta NuGet package, would you be able to check if the solution works for you?
Jun 18, 2013 at 9:51 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Jun 18, 2013 at 9:56 AM
Absolutely. I would be more than happy to...


Jun 18, 2013 at 4:11 PM
2.3.0-beta1 is online. Please let me know if this works for you.
Marked as answer by dot_NET_Junkie on 3/2/2014 at 10:44 AM