'Scope cache was already disposed.' in SignalR implementation

47 views
Skip to first unread message

Onur Ercan

unread,
Feb 2, 2015, 11:27:23 PM2/2/15
to net-http-a...@googlegroups.com
We are developing a web application based on

* .NET 4.5.1
* MVC 5.2.2
* OWIN
* WebApi 2.2
* SignalR 2.2.0 
* Castle.Windsor 3.3.0
* Wcf Integration Facility 3.3.0

For resolving the controllers we use ControllerFactory class which was described in the page below:

For resolving the dependencies we use WindsorDependencyResolver class:

public class WindsorDependencyResolver : IDependencyResolver
{
 public IWindsorContainer Container { get; private set; }

 public WindsorDependencyResolver(IWindsorContainer windsorContainer)
 {
  Container = windsorContainer;
 }

 public IDependencyScope BeginScope()
 {
  return new WindsorDependencyScope(this.Container);
 }

 public object GetService(Type serviceType)
 {
  return this.Container.Kernel.HasComponent(serviceType) ? this.Container.Resolve(serviceType) : null;
 }

 public IEnumerable<object> GetServices(Type serviceType)
 {
  return this.Container.ResolveAll(serviceType).Cast<object>().ToArray();
 }

 public void Dispose()
 {

 }
}

public class WindsorDependencyScope : IDependencyScope
{
 public IWindsorContainer Container { get; set; }
 public IDisposable Scope { get; set; }

 public WindsorDependencyScope(IWindsorContainer container)
 {
  this.Container = container;
  this.Scope = container.BeginScope();
 }

 public object GetService(Type serviceType)
 {
  return this.Container.Kernel.HasComponent(serviceType) ? this.Container.Resolve(serviceType) : null;
 }

 public IEnumerable<object> GetServices(Type serviceType)
 {
  return this.Container.ResolveAll(serviceType).Cast<object>().ToArray();
 }

 public void Dispose()
 {
  this.Scope.Dispose();
 }
}

Keep in mind we don't resolve the IHub classes of SignalR with Windsor container, they are instantiated by OWIN system in pipeline. The Startup.cs code is shown below:

public partial class Startup
{
 public void Configuration(IAppBuilder app)
 {
  ConfigureAuth(app);

  app.MapSignalR();
 }
}


All of the controllers, wcf service clients and interceptors -excepting logging classes- are registered with LifestylePerWebRequest in the project. However the classes we use for logging are singleton.

There is a setting in the Web.config below:

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
   ...
      <add name="PerRequestLifestyle" type="Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule, Castle.Windsor" />
   ...
    </modules>
  </system.webServer>
  

So when we try to resolve a wcf client -which has per web request lifestyle- in the SignalR hub we get the exception below:

System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'Scope cache was already disposed. This is most likely a bug in the calling code.'.
   at Castle.MicroKernel.Lifestyle.Scoped.ScopeCache.get_Item(Object id)
   at Castle.MicroKernel.Lifestyle.Scoped.DefaultLifetimeScope.GetCachedInstance(ComponentModel model, 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[T]()
   at UIServer.WebUI.Hubs.MailThreadHub.Broadcast(MailMessageListDto mailMessage) in c:\Development\DDD\UIServer.WebUI\Hubs\MailThreadHub.cs:line 92

I can see HttpContext in debugger window before the call Container.Resolve<T>() method. I can resolve the singleton logging classes by the way.

The interesting point is my teammate doesn't get any exception. The main difference is our OS versions. I run the code in windows 8.1 and my teammate runs it in windows 7.

We get this exception for only signalr hubs. We don't get any exception in any other place. How can we solve this problem?

Damian Hickey

unread,
Feb 3, 2015, 4:26:42 AM2/3/15
to net-http-a...@googlegroups.com

You'll have better chance of a response on Stackoverflow, aspnet forums or singlar room on jabbr.

--
You received this message because you are subscribed to the Google Groups "OWIN working group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to net-http-abstrac...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages