JasperFx / lamar

Fast Inversion of Control Tool and Successor to StructureMap
https://jasperfx.github.io/lamar
MIT License
568 stars 119 forks source link

Index out of range when using GetInstance(type) #186

Closed cameronmr closed 4 years ago

cameronmr commented 4 years ago

I am in the process of porting our Structuremap applications across to Lamar and having issues related to ControllerActivation in our WebAPI 2 applications. We are using the follow Service Activator that is registered during startup:

config.Services.Replace(typeof (IHttpControllerActivator), new ServiceActivator(config));
public class ServiceActivator : IHttpControllerActivator
{
  public ServiceActivator(HttpConfiguration configuration) { }

  public IHttpController Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
  {
    return ObjectFactory.Container.GetInstance(controllerType) as IHttpController;
  }
}

I am relying on the Auto Resolution of Concrete types to find the Controller types at runtime.

This pattern seems to work fine but then starts failing after a short period of time with the following stack trace:

System.IndexOutOfRangeException: Index was outside the bounds of the array.
  at System.Collections.Generic.List'1.Add(T item)
  at (ArrayClosure , Scope )
  at ServiceActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType) in ServiceActivator.cs:line 16
  at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__15.MoveNext()

This usually occurs within 15mins or less of very limited traffic and will never recover

Questions:

  1. Is my ControllerActivator wrong? Should I be using GetNestedContainer().GetInstance()?
  2. Is this an issue in Lamar that may be caused by Auto Resolution?
  3. Can you offer anything to try?

Cheers Cameron

jeremydmiller commented 4 years ago

@cameronmr There's nothing about Lamar in your stacktrace there. Lamar isn't building your controllers. I'd say to check your IoC setup and how you're applying a ControllerActivator.

jeremydmiller commented 4 years ago

No recent activity, and I don't think this is Lamar related.

kutensky commented 4 years ago

I have the same issue. The stack trace is:

at System.Collections.Generic.List`1.Add(T item)
at Lamar.IoC.Scope.TryAddDisposable(Object object)
at (Closure`3 , Scope )
at Lamar.IoC.Scope.GetInstance[T]()

My thoughts is that Lamar creates a new instace of an object during "GetInstance" call and add it to "Disposables" List. And this operation isn't thread-safe.

jeremydmiller commented 4 years ago

Got actual details finally. This should be easy.