JasperFx / lamar

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

Lamar Crash while using classes without namespace #270

Closed maxisoft closed 3 years ago

maxisoft commented 3 years ago

Hello, Lamar 4.4.0 doesn't handle scanning assemblies containing any classes without namespace. It crash with a NullReferenceException.

I know it's a bad practice to not use a namespace. But i guess crashing on this case shouldn't be the expected behavior in Lamar. Or at least it should crash in a better documented way :)

For some context: I built a simple plugin system using Roslyn and Lamar. This issue prevent one to write quick and dirty plugins classes (eg without namespace), also it took me some time to figure out why the app was crashing.

To reproduce I use this simple container:

using var container = new Container(r =>
{
    r.Scan(scanner =>
    {
        scanner.TheCallingAssembly();
        scanner.AddAllTypesOf<IService>();
        scanner.IncludeNamespaceContainingType<IService>();
    });
});

Gist with minimal sample Program.cs to reproduce crash

Here's the stack trace

   at LamarCodeGeneration.Util.TypeExtensions.IsInNamespace(Type type, String nameSpace)
   at Lamar.Scanning.Conventions.AssemblyScanner.<>c__DisplayClass28_0.<IncludeNamespace>b__0(Type type)
   at LamarCodeGeneration.Util.CompositePredicate`1.<>c__DisplayClass4_1.<Add>b__4(Func`2 predicate)
   at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate)
   at LamarCodeGeneration.Util.CompositePredicate`1.<Add>b__4_1(T x)
   at LamarCodeGeneration.Util.CompositePredicate`1.MatchesAny(T target)
   at LamarCodeGeneration.Util.CompositeFilter`1.Matches(T target)
   at Lamar.Scanning.Conventions.AssemblyScanner.<Start>b__52_0(Type type)
   at System.Linq.Enumerable.WhereEnumerableIterator`1.ToArray()
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at BaselineTypeDiscovery.TypeSet.FindTypes(TypeClassification classification)
   at Lamar.Scanning.Conventions.FindAllTypesFilter.ScanTypes(TypeSet types, ServiceRegistry services)
   at Lamar.Scanning.Conventions.AssemblyScanner.ApplyRegistrations(ServiceRegistry services)
   at Lamar.Scanning.Conventions.ScanningExploder.ExplodeSynchronously(IServiceCollection services)
   at Lamar.ServiceGraph..ctor(IServiceCollection services, Scope rootScope)
   at Lamar.IoC.Scope..ctor(IServiceCollection services, PerfTimer timer)
   at Lamar.Container..ctor(IServiceCollection services)
   at Lamar.Container..ctor(Action`1 configuration)
   at LamarCrashOnNoNamespace.Program.Main(String[] args) in Program.cs:line 12
jeremydmiller commented 3 years ago

Sorry for the long delay here. The little fix for this will be in the next Lamar release. How do you even manage that? That code came straight over from StructureMap, so it's been in use since about '08 w/o anybody hitting this.