Configure everything before calling GetInstance.

22 views
Skip to first unread message

chrissie1

unread,
Jul 28, 2008, 10:42:54 AM7/28/08
to structuremap-users
I just found out today that you have to have everything configured in
structuremap before calling getInstance. I am using structureMap 2.4.9
and VB.Net (I'm sorry).

To test this I created.

2 interfaces.

Namespace Interfaces
Public Interface IClass1
Sub somefunction()
End Interface
End Namespace

Namespace Interfaces
Public Interface IClass2
Sub someotherfunction()
End Interface
End Namespace

And 2 Classes.

Public Class Class1
Implements Interfaces.IClass1


Public Sub somefunction() Implements
Interfaces.IClass1.somefunction
Console.WriteLine("class1")
End Sub
End Class

Public Class Class2
Implements Interfaces.IClass2

Public Sub someotherfunction() Implements
Interfaces.IClass2.someotherfunction
Console.WriteLine("class2")
End Sub
End Class

And a module to run it.

Module Module1

Sub Main()
StructureMap.StructureMapConfiguration.ForRequestedType(Of
Interfaces.IClass1).TheDefaultIsConcreteType(Of Class1)()
StructureMap.ObjectFactory.GetInstance(Of
Interfaces.IClass1).somefunction()
StructureMap.StructureMapConfiguration.ForRequestedType(Of
Interfaces.IClass2).TheDefaultIsConcreteType(Of Class2)()
StructureMap.ObjectFactory.GetInstance(Of
Interfaces.IClass2).someotherfunction()
End Sub

End Module

After getting the class1 you will get this error.

StructureMap Exception Code: 208 Requested type
ConsoleApplication3.Interfaces.IClass2 is not configured in
StructureMap

This could be a problem when you call a class-library that also uses
StructureMap without you knowing. Perhaps there is a simple solution I
don't know about yet. If there is please tell me.

Jeremy Miller

unread,
Jul 28, 2008, 1:22:38 PM7/28/08
to structuremap-users
Well, yeah, that is the way it works. I do need to add some more
documentation for this. I'm thinking about adding some defensive
checks so that if you call StructureMapConfiguration.Anything() after
ObjectFactory is baked that it throws an exception.

For multiple apps/components all using StructureMap together, my
strong suggestion is to put ALL configuration into Registry classes
and just add all the necessary Registry's to StructureMapConfiguration
at the bootstrapping. Again, something I do intend to document.

chrissie1

unread,
Jul 28, 2008, 2:05:48 PM7/28/08
to structuremap-users
Ok, thanks. I'll remember that.
Reply all
Reply to author
Forward
0 new messages