StructureMap Exception 308

440 views
Skip to first unread message

Christian Hollerup Mikkelsen

unread,
Jun 28, 2013, 5:11:18 AM6/28/13
to structure...@googlegroups.com
Im experiencing rather a lot of 308 exceptions in an old system, using SM 2.6.3.

The exception looks like the one at below and the exception typically occours when in scenarios where there are running multiple threads, but completely at random as far as I can tell.
It is worth noting that the multithreading is a rather new refactor to the old code base to get better performance although stressing the DB.

StructureMap.StructureMapException: StructureMap Exception Code:  308
A configured instance interceptor has failed for Instance '0e769190-2db7-46e0-8be5-59b3cebc1a2c' and concrete type 'NHibernate.Impl.SessionImpl, NHibernate, Version=3.3.1.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4' ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at StructureMap.Pipeline.ObjectBuilder.ApplyInterception(Type pluginType, Object actualValue, BuildSession session, Instance instance) in c:\code\structuremap\Source\StructureMap\Pipeline\ObjectBuilder.cs:line 56
   --- End of inner exception stack trace ---
   at StructureMap.Pipeline.ObjectBuilder.ApplyInterception(Type pluginType, Object actualValue, BuildSession session, Instance instance) in c:\code\structuremap\Source\StructureMap\Pipeline\ObjectBuilder.cs:line 54
   at StructureMap.Pipeline.ObjectBuilder.Resolve(Type pluginType, Instance instance, BuildSession session) in c:\code\structuremap\Source\StructureMap\Pipeline\ObjectBuilder.cs:line 27
   at StructureMap.BuildSession.CreateInstance(Type pluginType, Instance instance) in c:\code\structuremap\Source\StructureMap\BuildSession.cs:line 153
   at StructureMap.BuildSession.<>c__DisplayClass3.<.ctor>b__1() in c:\code\structuremap\Source\StructureMap\BuildSession.cs:line 34
   at StructureMap.BuildSession.CreateInstance(Type pluginType) in c:\code\structuremap\Source\StructureMap\BuildSession.cs:line 192
   at StructureMap.Container.GetInstance[T]() in c:\code\structuremap\Source\StructureMap\Container.cs:line 155
   at StructureMap.ObjectFactory.GetInstance[PLUGINTYPE]() in c:\code\structuremap\Source\StructureMap\ObjectFactory.cs:line 131

The code uses nested containers in the threads in several places follow a pattern like this:

Parallel.ForEach(itemIds, itemId =>
{
using (var container = ObjectFactory.Container.GetNestedContainer())
{
using (var uow = new UnitOfWork())
{
container.GetInstance<IClassWhichHaveADependcyOnISession>().DoWorkOnId(itemId);
uow.Commit();
}
}
}

The ouw is only there to ensure a transaction and the itemIds are just that, ids, not objects loaded in another ISession on another thread.

The ISession is configured like this:

For<ISessionContext>().Singleton().Use<SessionContext>();
For<ISession>().Use(y => y.GetInstance<ISessionContext>().GetCurrentSession());

And the interesting methods on SessionContext looks like this:

public ISession GetCurrentSession()
{
if (!CurrentSessionContext.HasBind(_sessionFactory))
{
CurrentSessionContext.Bind(_sessionFactory.OpenSession());
}
return _sessionFactory.GetCurrentSession();
}

public void DisposeSession()
{
var session = GetCurrentSession();
if (session != null)
{
CurrentSessionContext.Unbind(_sessionFactory);
if (session.IsOpen)
{
session.Close();
}
session.Dispose();
}
}

NHibernate is configured to run in thread static mode:  current_session_context_class = thread_static

The thing that strikes me is that the error always refer to "at StructureMap.Pipeline.ObjectBuilder.ApplyInterception" and "A configured instance interceptor has failed for Instance".

I have no idea of what the cause of the problem is as it seems random but it always have to do with ISession.

I have looked at the code at github but it makes me none the wiser as I do not think we are doing any special interception with ISession.


Question:
Can anyone explain to me what the 308 error and interceptor is all about in this case AND if there is a way to resolve this issue?


Jeremy Miller

unread,
Jul 15, 2013, 5:16:01 PM7/15/13
to structure...@googlegroups.com
Christian,

You posted everything *but* the interceptor that's failing for you.  Look for something in the registration with EnrichWith() or ReplaceWith() maybe.  There's some kind of interceptor on the creation of the NH ISession that might not be thread safe.

- Jeremy

John Nobile

unread,
Apr 25, 2014, 1:33:50 AM4/25/14
to structure...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages