I'm trying to understand Registry and ScanAssemblies. Here's what
I've put together to test with:
Web application with a simple "Hello World" type page.
Web app references Core assembly.
Core assembly contains IWidget interface containing nothing but a Name
property.
Three additional assemblies "Red", "Green", and "Blue" contain
RedWidget, BlueWidget, and GreenWidget which are very skeletal
implementations of IWidget that set the Name property to a name passed
in through the constructor.
So, each color assembly has a registry that looks like this:
public class RedRegistry : StructureMap.Configuration.DSL.Registry
{
protected override void configure()
{
A few other things I tried... For simplification, I removed the
"name" parameter so the widgets have a parameterless constructor. I
also changed the configuration method of the registries to look like
this:
Still get an error 202 from StructureMap when I try to do a
GetInstance<IWidget>() though. Even though I watched it in the
debugger run through those BuildInstancesOf calls at startup.
} On Thu, Sep 11, 2008 at 9:17 AM, jdc <jdcrutch...@spiffworks.com> wrote:
> I'm trying to understand Registry and ScanAssemblies. Here's what > I've put together to test with:
> Web application with a simple "Hello World" type page. > Web app references Core assembly. > Core assembly contains IWidget interface containing nothing but a Name > property. > Three additional assemblies "Red", "Green", and "Blue" contain > RedWidget, BlueWidget, and GreenWidget which are very skeletal > implementations of IWidget that set the Name property to a name passed > in through the constructor.
> So, each color assembly has a registry that looks like this:
> public class RedRegistry : StructureMap.Configuration.DSL.Registry > { > protected override void configure() > {
> Whenever I try to get a widget from the container, I get the "Could > not find Instance named RedWidget for plugin type CoreStuff.IWidget" > message.
> Actually, what I'd rather do is something like this to get all > assemblies that happen to contain IWidget implementations:
====================================================
PluginType Name Description
----------------------------------------------------
CoreStuff.IWidget,CoreStuff
Built by: StructureMap.Pipeline.BuildPolicy
This is confusing the hell out of me. Do I need the configuration XML
file or attributes? I was under the impression that using
ScanAssemblies with the Registry classes I wouldn't need those.
Don't ever make any calls to ObjectFactory within a Registry. I think I'm killing off that SetDefault method right now because it can only cause problems.
Give me the lunch hour and I'll respond to the ScanAssemblies question.
Jeremy D. Miller The Shade Tree Developer jeremydmil...@yahoo.com
----- Original Message ---- From: jdc <jdcrutch...@spiffworks.com> To: structuremap-users <structuremap-users@googlegroups.com> Sent: Thursday, September 11, 2008 10:46:54 AM Subject: [sm-users] Re: Please help with ScanAssemblies
Ok, I'm getting closer. If, within the Registry configure method, I use something like this:
ObjectFactory.SetDefault<IWidget, GreenWidget>()
then I can instantiate one later. It's like calling methods on StructureMapConfiguration doesn't apply to ObjectFactory.
I banged around on this a little more yesterday evening and still no
luck. I think I'm using the latest trunk (last update I pulled down
was from September 2). Maybe I'm misunderstanding what ScanAssemblies
is supposed to do?
On Thu, Sep 11, 2008 at 9:17 AM, jdc <jdcrutch...@spiffworks.com> wrote:
> I'm trying to understand Registry and ScanAssemblies. Here's what > I've put together to test with:
> Web application with a simple "Hello World" type page. > Web app references Core assembly. > Core assembly contains IWidget interface containing nothing but a Name > property. > Three additional assemblies "Red", "Green", and "Blue" contain > RedWidget, BlueWidget, and GreenWidget which are very skeletal > implementations of IWidget that set the Name property to a name passed > in through the constructor.
> So, each color assembly has a registry that looks like this:
> public class RedRegistry : StructureMap.Configuration.DSL.Registry > { > protected override void configure() > {
> Whenever I try to get a widget from the container, I get the "Could > not find Instance named RedWidget for plugin type CoreStuff.IWidget" > message.
> Actually, what I'd rather do is something like this to get all > assemblies that happen to contain IWidget implementations:
I did have some success using a scanner, but I'd rather avoid that.
What I'm looking for is some way to drop a DLL into the directory and
have the existing call to ScanAssemblies find the Registry within it
and fire off the configure method. I can actually get that far. I
step through in the debugger and see the configure method running.
The trouble is, the registrations I do within that method don't
"stick". However, if I do a StructureMapConfiguration.AddRegistry(new
TheRegistry()) for each one, then it works. Of course then I'm back to
knowing in advance what my registries are...
I suppose I could try creating an ITypeScanner that, when it finds
something assignable to IRegistry, calls
StructureMapConfiguration.AddRegistry on it. I'll probably try that
next.
I was just under the impression that ScanAssemblies was supposed to do
that already.
ScanAssemblies has to be explicitly told which assemblies to scan. I've always been leery of any kind of behavior that you wouldn't expect.
You've got a couple options, but they both involve writing your own code:
1.) Set aside an "extensions" folder of some type directly under the main application folder. In the bootstrapping code, find all the assembly files in that directory, and add each to ScanAssemblies().AddAssembly( assembly name). This is the approach that my team is going to take for our extensibility strategy, but we aren't going to work on that until at least October.
2.) Go with an Xml file at an expected location that lists the assemblies to load
ScanAssemblies will pick up any registry classes in assemblies (plus the attribute configuration, but I'd recommend the Registry approach), but it has to be told which assemblies to load and scan.
Jeremy D. Miller The Shade Tree Developer jeremydmil...@yahoo.com
----- Original Message ---- From: jdc <jdcrutch...@spiffworks.com> To: structuremap-users <structuremap-users@googlegroups.com> Sent: Friday, September 12, 2008 8:27:15 AM Subject: [sm-users] Re: Please help with ScanAssemblies
I did have some success using a scanner, but I'd rather avoid that. What I'm looking for is some way to drop a DLL into the directory and have the existing call to ScanAssemblies find the Registry within it and fire off the configure method. I can actually get that far. I step through in the debugger and see the configure method running. The trouble is, the registrations I do within that method don't "stick". However, if I do a StructureMapConfiguration.AddRegistry(new TheRegistry()) for each one, then it works. Of course then I'm back to knowing in advance what my registries are...
I suppose I could try creating an ITypeScanner that, when it finds something assignable to IRegistry, calls StructureMapConfiguration.AddRegistry on it. I'll probably try that next.
I was just under the impression that ScanAssemblies was supposed to do that already.
Thanks Jeremy. The problem I'm having though isn't that the
registries aren't being scanned. I can watch in the debugger that the
configure methods are called. However, the statements in them don't
seem to have any effect. I still get 202 errors when I try
GetInstance. It does work if I used
StructureMapConfiguration.AddRegistry on each of them though. In both
cases I can watch through the debugger and see configure getting
called on each registry, but when it's kicked off from ScanAssemblies
the configuration doesn't stick.
I also noticed that using ScanAssemblies, the actual running of the
configure methods is delayed until the first call to GetInstance.
Probably because ScanAssemblies is really just creating an
expression. Is it possible that my ObjectFactory is getting into some
state where it doesn't allow changes by the time the actual scan runs?
[mailto:structuremap-users@googlegroups.com] On Behalf Of jdc Sent: Friday, September 12, 2008 8:27 AM To: structuremap-users Subject: [sm-users] Re: Please help with ScanAssemblies
I did have some success using a scanner, but I'd rather avoid that. What I'm looking for is some way to drop a DLL into the directory and have the existing call to ScanAssemblies find the Registry within it and fire off the configure method. I can actually get that far. I step through in the debugger and see the configure method running. The trouble is, the registrations I do within that method don't "stick". However, if I do a StructureMapConfiguration.AddRegistry(new TheRegistry()) for each one, then it works. Of course then I'm back to knowing in advance what my registries are...
I suppose I could try creating an ITypeScanner that, when it finds something assignable to IRegistry, calls StructureMapConfiguration.AddRegistry on it. I'll probably try that next.
I was just under the impression that ScanAssemblies was supposed to do that already.
I was just trying to work through your example at
http://www.lostechies.com/blogs/chad_myers/archive/2008/07/17/structu....
Assuming I could get that working, I was going to try something like
what Jeremy mentioned about creating a special directory for
assemblies to be auto-scanned for registries then use some reflection
magic to add each one to the scan.
Trouble is, I can't even get the simple case to work. Scan assemblies
seems to have no trouble finding the registries and running their
configure methods. It's just that any configuration done by those
registries doesn't seem to actually happen. Only adding the registry
directly with StructureMapConfiguration.AddRegistry actually works.
On Sep 12, 9:15 am, "Chad Myers" <chad.my...@gmail.com> wrote:
----- Original Message ---- From: jdc <jdcrutch...@spiffworks.com> To: structuremap-users <structuremap-users@googlegroups.com> Sent: Friday, September 12, 2008 9:05:43 AM Subject: [sm-users] Re: Please help with ScanAssemblies
Thanks Jeremy. The problem I'm having though isn't that the registries aren't being scanned. I can watch in the debugger that the configure methods are called. However, the statements in them don't seem to have any effect. I still get 202 errors when I try GetInstance. It does work if I used StructureMapConfiguration.AddRegistry on each of them though. In both cases I can watch through the debugger and see configure getting called on each registry, but when it's kicked off from ScanAssemblies the configuration doesn't stick.
I also noticed that using ScanAssemblies, the actual running of the configure methods is delayed until the first call to GetInstance. Probably because ScanAssemblies is really just creating an expression. Is it possible that my ObjectFactory is getting into some state where it doesn't allow changes by the time the actual scan runs?
> Any chance you can send me the code? That functionality has quite a bit of unit test coverage, so I think there's something else going on.
> Jeremy D. Miller
> The Shade Tree Developer
> jeremydmil...@yahoo.com
> ----- Original Message ----
> From: jdc <jdcrutch...@spiffworks.com>
> To: structuremap-users <structuremap-users@googlegroups.com>
> Sent: Friday, September 12, 2008 9:05:43 AM
> Subject: [sm-users] Re: Please help with ScanAssemblies
> Thanks Jeremy. The problem I'm having though isn't that the
> registries aren't being scanned. I can watch in the debugger that the
> configure methods are called. However, the statements in them don't
> seem to have any effect. I still get 202 errors when I try
> GetInstance. It does work if I used
> StructureMapConfiguration.AddRegistry on each of them though. In both
> cases I can watch through the debugger and see configure getting
> called on each registry, but when it's kicked off from ScanAssemblies
> the configuration doesn't stick.
> I also noticed that using ScanAssemblies, the actual running of the
> configure methods is delayed until the first call to GetInstance.
> Probably because ScanAssemblies is really just creating an
> expression. Is it possible that my ObjectFactory is getting into some
> state where it doesn't allow changes by the time the actual scan runs?- Hide quoted text -
Jeremy was good enough to have a look at my code and point me in the
right direction. Apparently I should NOT have been doing this from
within a registry:
On Mon, Sep 15, 2008 at 1:16 PM, jdc <jdcrutch...@spiffworks.com> wrote:
> FYI for anyone having a similar problem:
> Jeremy was good enough to have a look at my code and point me in the > right direction. Apparently I should NOT have been doing this from > within a registry: