Scanning for types with AssembliesFromPath

1,158 views
Skip to first unread message

Kristoffer Ahl

unread,
Jan 29, 2010, 3:42:45 AM1/29/10
to structuremap-users
I'm working on an application where we are using StructureMap 2.5.4 to
scan a directory like below.

ObjectFactory.Configure(scanner => scanner.Scan(x =>
{
x.AssembliesFromPath(settings.Directory);
x.AddAllTypesOf(typeof(IScannerConvention)).NameBy(n => n.Name);
}));

For some reason the types implementing IScannerConvention are not
registered. If we change AddAllTypesOf to typeof(object) we can see
that the types we are looking for are registered with the container
but that is obviously not what we want to do. We have also tried
moving the assemblies containing types implementing IScannerConvention
to the application base directory and use
x.AssembliesFromApplicationBaseDirectory(). This works and the
expected types are registered. But again, this is not a solution that
we can use as it will cause conflicts moving all assemblies to the
application base directory. At this point I really have no ide on what
could be wrong.

Any ideas are most welcome!

/ Kristoffer

Frank L. Quednau

unread,
Jan 29, 2010, 4:30:20 AM1/29/10
to structuremap-users
I don't know how you deploy your stuff, but if you use a private
assembly that contains the interface you are looking for, and have it
in both paths, app path and path where you have your things deployed,
you will see that you have that assembly loaded twice. The code with
"AddAllTypesOf" uses the interface from assembly 1, while the class
implementing the interface obtains it from the same assembly, but a
different path. Hence, technically, your implementation does not
implement said interface, because it comes from a different assembly.
Now, if you remove the assembly containing your interface from the
directory you are scanning, the interface assembly will be loaded only
once and it appears that the type can then match your interface
implementation - you should be able to pick it up.

Kristoffer Ahl

unread,
Jan 29, 2010, 4:39:12 AM1/29/10
to structuremap-users
Frank,
The directory I'm scanning does not contain the assembly that has the
interface. We tried having it that way that but it made no difference.

/ Kristoffer

Frank L. Quednau

unread,
Jan 29, 2010, 5:22:34 AM1/29/10
to structuremap-users
I just see now, the difference is in fact that I used the trunk code,
not the 2.5.4 version. With the trunk version it works. Unfortunately,
there is no official release yet of the trunk code.

Just for reference, here my setup:
I tried with a solution with 3 projects:
Main - contains the scan code, references Dll1.
Dll1 - contains the interface
Dll2 - contains the implementation.

Built the solution, deleted dll1 in the bin/debug from dll2.
In Main I use an absolute path to bin/debug from Dll2.

With the trunk code, it will pick up the implementation, with 2.5.4
not.

Kristoffer Ahl

unread,
Jan 29, 2010, 6:40:27 AM1/29/10
to structuremap-users
Frank,
Thanks for taking the time to help!

Your setup is basically the same as mine. I downloaded the source from
the trunk and built it.
Tried running my app again but with the same result as before. The
types I'm looking for aren't registered when scanning with
x.AddAllTypesOf(typeof(IScannerConvention)).NameBy(n => n.Name) or
x.AddAllTypesOf<IScannerConvention>().NameBy(n => n.Name).
And as before, changing typeof(IScannerConvention) to typeof(object)
will find and register the implementations of IScannerConvention
(along with all other types in that assembly). Any other ideas?

/ Kristoffer

Kristoffer Ahl

unread,
Jan 29, 2010, 7:21:57 AM1/29/10
to structuremap-users
Frank,
Could you perhaps create a zip-file of your solution that is working
and send it to me at kristoffer.ahl [at] spinit.se?
Maybe I can identify something in there that we're doing differently.

/ Kristoffer

Kristoffer Ahl

unread,
Jan 29, 2010, 1:02:51 PM1/29/10
to structuremap-users
First off, thatnks for sending me your solution. I got it working and
then I started to change a few things within my own solution that we
weren't doing the same way. Still the same result though. Started
digging through all my code and eventually found the problem:

It seems you were correct the first time around! The directory I was
scanning did not contain the assembly with the interface in it but
there was a second directory in play where it had sneaked itself into.
Thanks a lot for all your help Frank!!! I would be nowhere without
your help on this.

All the best / Kristoffer

mohammad azad

unread,
Dec 19, 2014, 12:07:20 PM12/19/14
to structure...@googlegroups.com

Hi,Kristoffer  I have a problem same as you.can you send me your  solution file
Reply all
Reply to author
Forward
0 new messages