I'm really stuck here.
I have a asp.net mvc application and use StructureMap 2.5.3 (SM) to inject service and repository classes in my controllers. All controller are made by a SM factory.
I also have a Linq to SQL datacontext which I wanted to cache by hybrid.
public class DBRegistry:Registry
{
public DBRegistry()
{
ForRequestedType<SharpShopDataContext>()
.CacheBy(StructureMap.Attributes.InstanceScope.Hybrid)
.TheDefault.IsThis(new SharpShopDataContext());
}
}
The caching doesn't seem to work and I get issues with the datacontext because of it.
Multiple browser request all return the same dbcontext?! In one of my repository classes I've put this code. Debug.WriteLine("db hashcode: " + db.GetHashCode()+ " "+ DateTime.Now.ToString());
where db=the datacontext I also print the hashcodes of the repository using the db and the service using the repository, here is a print of multiple requests:
service hashcode: 6238972 26-3-2009 18:59:34
repository hashcode: 21756593 26-3-2009 18:59:34
db hashcode: 7043935 26-3-2009 18:59:34
service hashcode: 59389065 26-3-2009 18:59:34
repository hashcode: 8331620 26-3-2009 18:59:34
db hashcode: 7043935 26-3-2009 18:59:34
service hashcode: 11291358 26-3-2009 18:59:38
repository hashcode: 13848497 26-3-2009 18:59:38
db hashcode: 7043935 26-3-2009 18:59:38
service hashcode: 42509361 26-3-2009 18:59:38
repository hashcode: 56101068 26-3-2009 18:59:38
db hashcode: 7043935 26-3-2009 18:59:38
as you can see 7043935 is the hashcode for the datacontext every time for each request, but the service and repository get a new instance and hashcode every time.
I get weird errors because of this, for example a dataconcurrency exception because dbcontext has an original value from 4 webrequests ago, while the database was changed by another source.