mvvm - How the unity container will resolve the registered service -


in code trying log viewmodel name on entry/exit of viewmodel.

my logger is

public interface iloggerservice : iloggerfacade {     void logmessage(string message );            void logentry();        void logexit(); }  public class loggerservice : iloggerservice {     public void logentry()     {         var trace = new stacktrace();         if (trace.framecount > 1)         {             string ns = trace.getframe(1).getmethod().declaringtype.namespace;             string typename = trace.getframe(1).getmethod().declaringtype.name;             string message = string.format("{0}.{1}.{2} entry", ns, typename,                                             trace.getframe(1).getmethod().name);             logdebug(message, defaultpriority);         }     }      //  same logexit } 

my bootstrapper

public class bootstrapper : unitybootstrapper {     protected override void configurecontainer()     {         base.configurecontainer();         container.registerinstance<iloggerservice>(_logger);     }     protected override iloggerfacade createlogger()     {         _logger = new loggerservice();         return _logger;     } } 

my viewmodel & model are

public class homeviewmodel {     private readonly iloggerservice _loggerservice;     private readonly homemodel _model;     public homeviewmodel(homemodel model, iloggerservice logger)     {         logger.logentry();         _model = model;         _loggerservice = logger;          // other stuff here.          logger.logexit();     } }  public class homemodel {     private iloggerservice _logger;     public homemodel(iloggerservice logger)     {         logger.logentry();         _logger = logger;         logger.logexit();     } } 

error got

cat[critical} sev[critical] pri[100] framework time:[ 2016-09-22  13:33:39.860] shisha.exe pid: 22220 threadid:7428   ####   exception occurred while initializing module 'mainmodule'.      - exception message was: resolution of dependency failed, type = "modules.main.views.homeview", name = "(none)". exception occurred while: calling constructor modules.main.models.homemodel(services.logging.interface.iloggerservice logger). exception is: nullreferenceexception - object reference not set instance of object. ----------------------------------------------- @ time of exception, container was:    resolving modules.main.views.homeview,(none)   resolving parameter "viewmodel" of constructor modules.main.views.homeview(modules.main.viewmodels.homeviewmodel viewmodel)     resolving modules.main.viewmodels.homeviewmodel,(none)     resolving parameter "model" of constructor modules.main.viewmodels.homeviewmodel(modules.main.models.homemodel model, microsoft.practices.prism.regions.iregionmanager regionmanager, microsoft.practices.prism.events.ieventaggregator eventaggregator, services.dialogs.interface.idialogservice dialogservice, services.logging.interface.iloggerservice logger, services.localisation.interfaces.itranslator translator)       resolving modules.main.models.homemodel,(none)       calling constructor modules.main.models.homemodel(services.logging.interface.iloggerservice logger)      - assembly module trying loaded was:modules.main, version=1.0.0.0, culture=neutral, publickeytoken=null     check innerexception property of exception more information. if exception occurred while creating object in di container, can exception.getrootexception() locate root cause of problem.       @ services.logging.loggerservice.logcritical(string message, int32 priority) in c:\projects\utilities\dotnet\services\services.logging\loggerservice.cs:line 257    @ services.logging.loggerservice.logcritical(string message) in c:\projects\utilities\dotnet\services\services.logging\loggerservice.cs:line 250    @ shisha.bootstrapper.initializemodules() in c:\projects\shisha\application\shisha\bootstrapper.cs:line 85    @ microsoft.practices.prism.unityextensions.unitybootstrapper.run(boolean runwithdefaultconfiguration)    @ shisha.app.application_startup(object sender, startupeventargs e) in c:\projects\shisha\application\shisha\app.xaml.cs:line 39    @ system.windows.application.onstartup(startupeventargs e)    @ system.windows.application.<.ctor>b__1_0(object unused)    @ system.windows.threading.exceptionwrapper.internalrealcall(delegate callback, object args, int32 numargs)    @ system.windows.threading.exceptionwrapper.trycatchwhen(object source, delegate callback, object args, int32 numargs, delegate catchhandler)    @ system.windows.threading.dispatcheroperation.invokeimpl()    @ system.windows.threading.dispatcheroperation.invokeinsecuritycontext(object state)    @ system.threading.executioncontext.runinternal(executioncontext executioncontext, contextcallback callback, object state, boolean preservesyncctx)    @ system.threading.executioncontext.run(executioncontext executioncontext, contextcallback callback, object state, boolean preservesyncctx)    @ system.threading.executioncontext.run(executioncontext executioncontext, contextcallback callback, object state)    @ ms.internal.culturepreservingexecutioncontext.run(culturepreservingexecutioncontext executioncontext, contextcallback callback, object state)    @ system.windows.threading.dispatcheroperation.invoke()    @ system.windows.threading.dispatcher.processqueue()    @ system.windows.threading.dispatcher.wndprochook(intptr hwnd, int32 msg, intptr wparam, intptr lparam, boolean& handled)    @ ms.win32.hwndwrapper.wndproc(intptr hwnd, int32 msg, intptr wparam, intptr lparam, boolean& handled)    @ ms.win32.hwndsubclass.dispatchercallbackoperation(object o)    @ system.windows.threading.exceptionwrapper.internalrealcall(delegate callback, object args, int32 numargs)    @ system.windows.threading.exceptionwrapper.trycatchwhen(object source, delegate callback, object args, int32 numargs, delegate catchhandler)    @ system.windows.threading.dispatcher.legacyinvokeimpl(dispatcherpriority priority, timespan timeout, delegate method, object args, int32 numargs)    @ ms.win32.hwndsubclass.subclasswndproc(intptr hwnd, int32 msg, intptr wparam, intptr lparam)    @ ms.win32.unsafenativemethods.dispatchmessage(msg& msg)    @ system.windows.threading.dispatcher.pushframeimpl(dispatcherframe frame)    @ system.windows.threading.dispatcher.pushframe(dispatcherframe frame)    @ system.windows.application.rundispatcher(object ignore)    @ system.windows.application.runinternal(window window)    @ system.windows.application.run(window window)    @ myapp.app.main() in c:\projects\shisha\application\shisha\obj\x86\release\app.g.cs:line 0 

i googled , debugged ( adding logs ) lot came know

"trace.getframe(1).getmethod().declaringtype.namespace;"    

that failing..

it working fine in 'debug' mode ( because of pdb file ) failing in 'release' mode.

instead of resolving logger constructor if use

resolve<iloggerservice>(),    

it working fine.

it not clear me how _container.resolve() got methodbase information , 'resolving constructor' didn't. can explain me difference between these two.

or

am missing something?

it seems compiler converting constructor inline constructor.

by using [methodimpl(methodimploptions.noinlining)] , telling compiler not inline working fine.

[methodimpl(methodimploptions.noinlining)]  public class homemodel  {    private iloggerservice _logger;    public homemodel(iloggerservice logger)    {      logger.logentry();      _logger = logger;      logger.logexit();    }  } 

Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -