asp.net mvc - No default Instance is registered and cannot be automatically determined for type -
the definition of interface follows:
public interface iapplicationsettings { string loggername { get; } string numberofresultsperpage { get; } string emailaddress { get; } string credential { get; } }
the implementation of interface given below:
public class webconfigapplicationsettings : iapplicationsettings { public string loggername { { return configurationmanager.appsettings["loggername"]; } } public string numberofresultsperpage { { return configurationmanager.appsettings["numberofresultsperpage"]; } } public string emailaddress { { return configurationmanager.appsettings["emailaddress"]; } } public string credential { { return configurationmanager.appsettings["credential"]; } } }
i created factory class obtain instance of concrete implementation of webconfigsettings follows:
public class applicationsettingsfactory { private static iapplicationsettings _applicationsettings; public static void initializeapplicationsettingsfactory( iapplicationsettings applicationsettings) { _applicationsettings = applicationsettings; } public static iapplicationsettings getapplicationsettings() { return _applicationsettings; } }
then resolved dependency follows:
public class defaultregistry : registry { public defaultregistry() { scan( scan => { scan.thecallingassembly(); scan.withdefaultconventions(); scan.with(new controllerconvention()); }); for<iapplicationsettings>().use<webconfigapplicationsettings>(); applicationsettingsfactory.initializeapplicationsettingsfactory (objectfactory.getinstance<iapplicationsettings>()); } }
now when running application throw me following exception:
exception has been thrown target of invocation.
and inner exception
no default instance registered , cannot automatically determined type 'shoppingcart.infrastructure.configuration.iapplicationsettings'\r\n\r\nthere no configuration specified shoppingcart.infrastructure.configuration.iapplicationsettings\r\n\r\n1.) container.getinstance(shoppingcart.infrastructure.configuration.iapplicationsettings)\r\n
i using structuremap mvc5
the reason code isn't working because when call objectfactory.getinstance<iapplicationsettings>()
, registry hasn't been registered , thus, structuremap's configuration incomplete.
i believe you're trying following (tested , works):
public class applicationsettingsfactory { public applicationsettingsfactory(webconfigapplicationsettings applicationsettings) { _applicationsettings = applicationsettings; } private static iapplicationsettings _applicationsettings; public iapplicationsettings getapplicationsettings() { return _applicationsettings; } }
with registry configured this:
public defaultregistry() { scan(scan => { scan.thecallingassembly(); scan.withdefaultconventions(); scan.with(new controllerconvention()); }); this.for<iapplicationsettings>().use(ctx => ctx.getinstance<applicationsettingsfactory>().getapplicationsettings()); }