Bruno Lopes - 118 - Mar 23, 2014

If I have a class that implements IDisposable, and register it on Funq [1] with  ReuseScope.Request, shouldn’t the Dispose method be called on it when the request ends?
It feels like dispose is not being called on it, at least on 4.0.13.


[1] - container.Register(c => new Something()).ReusedWithin(ReuseScope.Request);

Bruno Lopes:

I think I found the issue.
I was using this dependency on a service with async methods. It looks like the Request reuse scope stores the items on a ThreadStatic field, which might get lost in the async calls. 

A test case for this would be:
- Register a IDisposable on Fun with ReuseScope.Request.
- Use it on a service with no async methods, it is disposed.
- Use it on a service with an async method, it is not disposed.

The workaround for now seems to be to not use async on services with per request dependencies (which in our case is almost all services).

Thanks Bruno, I’ll investigate, IDisposable dependencies should be disposed once the request ends sync or async. 

I’m guessing you’re only seeing this issue with HttpListener hosts right?

Anyway this change ended up being a little disruptive where I had to change to use Remoting’s CallContext LogicalData API’s instead of ThreadStatics and moved RequestContext from SS.Client into SS. 

I’ve added new IOC tests for async methods in this commit: 
https://github.com/ServiceStack/ServiceStack/commit/3d1111ed1e9c6306b8f20657bc9d00c63246d781
Which are now all passing.

This change is now available on MyGet:
https://github.com/ServiceStack/ServiceStack/wiki/MyGet

Bruno Lopes:

Sorry, still seeing the issue (confirmed that I’m using latest by checking that RequestContext is on ServicesStack namespace).

I think I’m using an AspNetHost.
From an immediate window:
>HostContext.IsAspNetHost
true
>HostContext.IsHttpListenerHost
false

Hi Bruno, is this for an ASP.NET host i.e. through IIS? (BTW RequestContext was always in the ServiceStack namespace, it’s just now in the ServiceStack.dll instead of ServiceStack.Client.dll)

Bruno Lopes:

Yap, asp.net host, via IIS. I’ve been testing on express, but it should be the same.

(Oops, sorry, misread your comment then. Still, I think I’ve got the correct version, it didn’t use a threadstatic to store instances).

ok I’ll look into it for ASP.NET. BTW ASP.NET never used thread statics, i.e. it always used System.Web.HttpContext.Current.Items

Bruno Lopes:

Gah, must have missed that, sorry.
Just to try and make sure I wasn’t doing anything else wrong, I’ve isolated the issue on this repo: https://github.com/brunomlopes/ss-i18n-contentpages  , after updating to the latest version.

The ping service at https://github.com/brunomlopes/ss-i18n-contentpages/blob/master/WebApplication1/Services/PingService.cs increases the disposed count on the Disposed class, but the async ping service does not.

Hi Bruno, this should now be resolved on MyGet: https://github.com/ServiceStack/ServiceStack/wiki/MyGet

Bruno Lopes:

Thanks Demis, just checked and it seems like the issue is gone :slight_smile: