Skip to content
Ian Johnson edited this page Jan 25, 2017 · 3 revisions

The following exceptions can be thrown when calling Locate or LocateAll. They all inherit from a base LocateException.

MissingDependencyException

This exception is thrown when a dependency can't be found, usually this means that something didn't get registered.

Below is an example unit test and the message generated

[Fact]
public void LocateException_Thrown_When_Dependency_Missing()
{
  var container = new DependencyInjectionContainer();

  Assert.Throws<LocateException>(() => container.Locate<DependentService<IBasicService>>());
}

Could not locate Type Grace.Tests.Classes.Simple.IBasicService
1 Importing Grace.Tests.Classes.Simple.DependentService`1[Grace.Tests.Classes.Simple.IBasicService] 
2 Importing Grace.Tests.Classes.Simple.IBasicService  for constructor parameter value

CircularDependencyDetectedException

This exception is thrown when a circular dependency is detected. A circular dependency is defined as any time you nesting depth goes above 50. If for some reason you need to construct an object graph with a max depth deeper than 50 (i.e. a root object has structure that looks like objectA.PropB.PropC.PropD ... PropAX) then you can can up the limit on the IInjectionContext, though I would recommend changing your object graph first.

Below is an example of circular dependency exception message from one of Grace's unit tests

Circular dependency detected see stack for error below
1 Locate by type IDependencyA in scope 'RootScope' 
2 Activating type DependencyA 
3 Importing  constructor parameter dependencyB using type IDependencyB
4 Activating type DependencyB 
5 Importing  constructor parameter dependencyA using type IDependencyA
6 Activating type DependencyA 
7 Importing  constructor parameter dependencyB using type IDependencyB
8 Activating type DependencyB 
9 Importing  constructor parameter dependencyA using type IDependencyA
10 Activating type DependencyA 
11 Importing  constructor parameter dependencyB using type IDependencyB
12 Activating type DependencyB 
13 Importing  constructor parameter dependencyA using type IDependencyA
14 Activating type DependencyA 
15 Importing  constructor parameter dependencyB using type IDependencyB
16 Activating type DependencyB 
17 Importing  constructor parameter dependencyA using type IDependencyA
18 Activating type DependencyA 
19 Importing  constructor parameter dependencyB using type IDependencyB
20 Activating type DependencyB 
21 Importing  constructor parameter dependencyA using type IDependencyA
22 Activating type DependencyA 
23 Importing  constructor parameter dependencyB using type IDependencyB
24 Activating type DependencyB 
25 Importing  constructor parameter dependencyA using type IDependencyA
26 Activating type DependencyA 
27 Importing  constructor parameter dependencyB using type IDependencyB
28 Activating type DependencyB 
29 Importing  constructor parameter dependencyA using type IDependencyA
30 Activating type DependencyA 

Dropped 42 entries 

73 Importing  constructor parameter dependencyA using type IDependencyA
74 Activating type DependencyA 
75 Importing  constructor parameter dependencyB using type IDependencyB
76 Activating type DependencyB 
77 Importing  constructor parameter dependencyA using type IDependencyA
78 Activating type DependencyA 
79 Importing  constructor parameter dependencyB using type IDependencyB
80 Activating type DependencyB 
81 Importing  constructor parameter dependencyA using type IDependencyA
82 Activating type DependencyA 
83 Importing  constructor parameter dependencyB using type IDependencyB
84 Activating type DependencyB 
85 Importing  constructor parameter dependencyA using type IDependencyA
86 Activating type DependencyA 
87 Importing  constructor parameter dependencyB using type IDependencyB
88 Activating type DependencyB 
89 Importing  constructor parameter dependencyA using type IDependencyA
90 Activating type DependencyA 
91 Importing  constructor parameter dependencyB using type IDependencyB
92 Activating type DependencyB 
93 Importing  constructor parameter dependencyA using type IDependencyA
94 Activating type DependencyA 
95 Importing  constructor parameter dependencyB using type IDependencyB
96 Activating type DependencyB 
97 Importing  constructor parameter dependencyA using type IDependencyA
98 Activating type DependencyA 
99 Importing  constructor parameter dependencyB using type IDependencyB
100 Activating type DependencyB 
101 Importing  constructor parameter dependencyA using type IDependencyA
102 Activating type DependencyA 

NullValueProvidedException

This exception is thrown when null is returned from a factor or instance.

Below is an example unit test and the exception message thrown.

[Fact]
public void NoArgFactory_Null_Return_Throws_Exception()
{
  var container = new DependencyInjectionContainer();

  container.Configure(c => c.ExportFactory<IBasicService>(() => null));

  Assert.Throws<NullValueProvidedException>(() => container.Locate<DependentService<IBasicService>>());
}

Null value provided from factory
1 Importing Grace.Tests.Classes.Simple.DependentService`1[Grace.Tests.Classes.Simple.IBasicService] 
2 Importing Grace.Tests.Classes.Simple.IBasicService  for constructor parameter value