I'm convering from old structure map to a new one.. 2. something to 3.1.6.186...
I'm trying to define a default instance for IWebAccess as WinFormAccess...when run it I get this error:
SetUp : StructureMap.StructureMapConfigurationException : No default Instance is registered and cannot be automatically determined for type 'JCDCTools.Core.Utilities.Interfaces.IWebAccess'
There is no configuration specified for JCDCTools.Core.Utilities.Interfaces.IWebAccess
1.) Container.GetInstance(JCDCTools.Core.Utilities.Interfaces.IWebAccess)
at StructureMap.SessionCache.GetDefault(Type pluginType, IPipelineGraph pipelineGraph) in c:\BuildAgent\work\a395dbde6b793293\src\StructureMap\SessionCache.cs: line 63 at StructureMap.Container.GetInstance(Type pluginType) in c:\BuildAgent\work\a395dbde6b793293\src\StructureMap\Container.cs: line 339 at StructureMap.Container.GetInstance() in c:\BuildAgent\work\a395dbde6b793293\src\StructureMap\Container.cs: line 202 at _Test_DAL.BaseTest.TestFixtureSetup() in BaseTest.cs: line 22
Here is my code
public DefaultRegistry()
{
Scan(
scan =>
{
scan.AssemblyContainingType<IWebAccess>(); // JCDCTools.Core
scan.LookForRegistries();
scan.WithDefaultConventions();
});
For<IWebAccess>()
.LifecycleIs<HybridLifecycle>() //why isn't this creating a default instance?
.Use<WinFormAccess>();
}
I've googled and dug, and I dont' get why For.use isn't creating a default instance... it looked simple,but I can't get this working..
Can anyone help me understand what I'm doing wrong? either code examples, or documentation that explains better than the official docs on Github (http://structuremap.github.io.)
As it turns out, I was initializing the container as shown above, and therefor ObjectFactory was not initialized, at all, it had 0 object scanned into it...
Let me repeat that, because it took me a stupidly long time to Grok it... and I'd hate for you to suffer like I did...
In StructureMap 3, you can use ObjectFactory, Xor container...initing the container leaves ObjectFactory dead in the water. If you want you can roll your own ObjectFactory (as below) or have a global static container somewhere that gets set at init time.
look at the 2nd code section here to see examples of each way to init Structure map: http://structuremap.github.io/registration/
(If anyone knows how to init the Object factory using the container as it's input I'd love to know , that way refactoring can be a gradual process, not a full stop till this I is done.)
If I replace my ObjectFactory with a container style call, it works. To make my life easier I made this class based on another SO questions answer, with my own twist of adding a GetInstance method that uses the container.
Since we only use GetInstance and GetNamedInstance, I just need to add a GetNamedInstance, method working, and then global search & replace ObjectFactory with JCDCObjectfactory, and I'm up and running...
I just need to come back later and refactor it all to use Constructor parameters instead. The nice thing about this is I can do that gradually, not all in one fell swoop.