views:

74

answers:

1

In the C# language, using StructureMap 2.5.4, targeting .NET Framework 3.5 libraries.

I've taken the step to support multiple Profiles in a structure map DI setup, using ServiceLocator model with Bootstrapper activation. First setup was loading default registry, using the scanner.

Now I like to determine runtime what Registry configuration I like to use. Scanning and loading multiple assemblies with registries.

Seems it's not working for the actual implementation (Getting the 202, default instance not found), but a stripped test version does work. The following setup.

  • Two assemblies containing Registries and implementations
  • Scanning them in running AppDomain, providing the shared Interface, and requesting Creation Of Instance, using the interfaces in constructor (which get dealt with thanx to the profile on Invokation)

Working code sample below (same structure for other setup, but with more complex stuff, that get's a 202):

What type of couses are possible for a 202, specifically naming the System.Uri type, not being handles by a default type?? (uri makes no sense)

// let structure map create instance of class tester, that provides the registered
// interfaces in the registries to the constructor of tester.

   public class Tester<TPOCO>
                {
                    private ITestMe<TPOCO> _tester;

                    public Tester(ITestMe<TPOCO> some)
                    {
                        _tester = some;
                    }

                    public string Exec()
                    {
                        return _tester.Execute();
                    }
                }

     public static class Main {
            public void ExecuteDIFunction() {
            ObjectFactory.GetInstance<Tester<string>>().Exec();
            }
        }



    public class ImplementedTestMe<TSome> : ITestMe<TSome>
        {
            public string Execute()
            {
                return "Special Execution";
            }
        }

    public class RegistryForSpecial : Registry
        {
            public RegistryForSpecial()
            {
                CreateProfile("Special",
                              gc =>
                                  {
                                      gc.For(typeof(ITestMe<>)).UseConcreteType(typeof(ImplementedTestMe<>));

                                  });
            }
        }

EDIT: It seemed the missing interface was actually the one being determined runtime. So here is the next challange (and solved):

I provided a default object whenever StructureMap needs to create the object. Like:

x.ForRequestedType<IConnectionContext>()
                  .TheDefault.Is.Object(new WebServiceConnection());

This way I got rid of the 202 error, because now a real instance could be used whever structure map needed the type.

Next was the override on runtime. That did not work out at first using the ObjectFactory.Configure method. Instead I used the ObjectFactory.Inject method to overide the default instance. Works like a charm.

ObjectFactory.Inject(typeof(IConnectionContext), context);

Loving the community effort.

+2  A: 

Error code 202 means a default instance could not be built for the requested type. Your test code is apparently not equal to your real code that fails. If you are getting an error about Uri, you likely have a dependency that requires a Uri in its constructor. It may not be the class you are asking for - it may be one of that classes dependendencies - or one of the dependencies dependencies... somewhere down the line someone is asking StructureMap to resolve a Uri, which it cannot do, without some help from you.

Joshua Flanagan
Seems to be an interface missing being passed to one of the top interfaces. Definitly holding an Uri property type. Thanx for getting me back on track!
Hans