c# - Structuremap 3+ (3.1.6.186) how to specify a default instance? -
i'm convering old structure map new one.. 2. 3.1.6.186...
i'm trying define default instance iwebaccess winformaccess...when run error:
setup : structuremap.structuremapconfigurationexception : no default instance registered , cannot automatically determined type 'jcdctools.core.utilities.interfaces.iwebaccess'
there no configuration specified 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 @ structuremap.container.getinstance(type plugintype) in c:\buildagent\work\a395dbde6b793293\src\structuremap\container.cs: line 339 @ structuremap.container.getinstance() in c:\buildagent\work\a395dbde6b793293\src\structuremap\container.cs: line 202 @ _test_dal.basetest.testfixturesetup() in basetest.cs: line 22
here code
public defaultregistry() { scan( scan => { scan.assemblycontainingtype<iwebaccess>(); // jcdctools.core scan.lookforregistries(); scan.withdefaultconventions(); }); for<iwebaccess>() .lifecycleis<hybridlifecycle>() //why isn't creating default instance? .use<winformaccess>(); }
i've googled , dug, , dont' why for.use isn't creating default instance... looked simple,but can't working..
can me understand i'm doing wrong? either code examples, or documentation explains better official docs on github (http://structuremap.github.io.)
as turns out, initializing container shown above, , therefor objectfactory not initialized, @ all, had 0 object scanned it...
let me repeat that, because took me stupidly long time grok it... , i'd hate suffer did...
in structuremap 3, can use objectfactory, xor container...initing container leaves objectfactory dead in water. if want can roll own objectfactory (as below) or have global static container somewhere gets set @ init time.
look @ 2nd code section here see examples of each way init structure map: http://structuremap.github.io/registration/
(if knows how init object factory using container it's input i'd love know , way refactoring can gradual process, not full stop till done.)
if replace objectfactory container style call, works. make life easier made class based on questions answer, own twist of adding getinstance method uses container.
public static class jcdcobjectfactory { private static readonly lazy<container> _containerbuilder = new lazy<container>( defaultcontainer, lazythreadsafetymode.executionandpublication ); public static t getinstance<t>() { return container.getinstance<t>(); } public static icontainer container { { return _containerbuilder.value; } } private static container defaultcontainer() { return new container( x => { // default config, have call initializer here, if want can remove lazy stuff, , make container property , set ever init structuremap, me that's ioc.cs - ewb } ); } }
since use getinstance , getnamedinstance, need add getnamedinstance, method working, , global search & replace objectfactory jcdcobjectfactory, , i'm , running...
i need come later , refactor use constructor parameters instead. nice thing can gradually, not in 1 fell swoop.
Comments
Post a Comment