I'm want to start using LightInject in ASP.NET application (some .asmx services). But now when method GetInstance<> invoked first time I got error

Unable to cast object of type 'LightInject.PerScopeLifetime' to type 'System.Object[]'.

There you can see stacktrace

at DynamicMethod(Object[] )
at LightInject.ServiceContainer.<>c__DisplayClass2a.<WrapAsFuncDelegate>b__29() in c:\GitHub\LightInject\NuGet\Build\Net45\LightInject\LightInject.cs:line 2894
at LightInject.PerScopeLifetime.CreateScopedInstance(Scope scope, Func`1 createInstance) in c:\GitHub\LightInject\NuGet\Build\Net45\LightInject\LightInject.cs:line 4708
at LightInject.PerScopeLifetime.<>c__DisplayClass1.<GetInstance>b__0(Scope s) in c:\GitHub\LightInject\NuGet\Build\Net45\LightInject\LightInject.cs:line 4693
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at LightInject.PerScopeLifetime.GetInstance(Func`1 createInstance, Scope scope) in c:\GitHub\LightInject\NuGet\Build\Net45\LightInject\LightInject.cs:line 4693
at DynamicMethod(Object[] )
at LightInject.ServiceContainer.GetInstance(Type serviceType) in c:\GitHub\LightInject\NuGet\Build\Net45\LightInject\LightInject.cs:line 2400
at LightInject.ServiceContainer.GetInstance[TService]() in c:\GitHub\LightInject\NuGet\Build\Net45\LightInject\LightInject.cs:line 2450
at MyProject.Core.DependencyResolver.GetInstance[T]() in C:\Work\Repos\MyProject\src-dev\Server\MyProject\Core\DependencyResolver.cs:line 11
at WebServices.General..ctor()
Invoke protected readonly ISomeService SomeService = DependencyResolver.GetInstance<ISomeService>();

DependencyResolver my custom wrap, thet loooks like this

public static class DependencyResolver
{
  public static T GetInstance<T>()
  {
      return IoCConfigurator.PerWebRequestScopeLifetimeContainer.GetInstance<T>();
  }

  public static T GetInstance<TArg, T>(TArg arg)
  {
      return IoCConfigurator.PerWebRequestScopeLifetimeContainer.GetInstance<TArg, T>(arg);
  }
}

There IoC configuration

public static class IoCConfigurator
{
 private static ServiceContainer _perWebRewuestScopeLifetimeContainer;

 public static IServiceContainer PerWebRequestScopeLifetimeContainer
 {
     get { return _perWebRewuestScopeLifetimeContainer }
 }


 public static void ConfigureWebLiftime()
 {
     _perWebRewuestScopeLifetimeContainer = ConfigureContainer(() => new PerScopeLifetime(), true);
 }

  private static ServiceContainer ConfigureContainer(Func<ILifetime> lifetimeFactory, bool usePerRequestScope = false)
{
      var container = new ServiceContainer();
      container.RegisterAssembly(typeof(IoCConfigurator).Assembly, lifetimeFactory);
      //Custom types registration
      container.Register<ISqlCache, IMyprojectEntities>((factory, value) => new MyprojectEntities(value));
      container.Register(typeof(IAbstractCache<,>), typeof(ConcurrentDictionaryCache<,>), lifetimeFactory.Invoke());     
      if (usePerRequestScope)
      {
          container.EnablePerWebRequestScope();
      }
      return container;     
  }      
}

IoCConfigurator.ConfigureWebLiftime() invoked in first string of Application_Start in Global.asax

Related posts

Recent Viewed