c# - Getting values of ApplicationData.Current.LocalSettings in static context -


i have next code:

public static class appuser     {         static appuser()         {             restorepreviewuserstate();         }          private static void restorepreviewuserstate()         {             var storedtoken = settings.authentification.sessiontoken; //here gets settings             var storeduserid = settings.authentification.currentuserid;               if (storedtoken == null || storeduserid == default(int)) return;             authtoken = storedtoken;             currentuserid = storeduserid;         }          public static bool existauthdata         {                         {                 return currentuserid != default(int) && authtoken != null;             }         }          private static string _authtoken;         public static string authtoken         {             { return _authtoken; }             set             {                 _authtoken = value;                 settings.authentification.sessiontoken = _authtoken;                 authheader = new authheader(_authtoken);             }         }          private static int _currentuserid;         public static int currentuserid         {             { return _currentuserid; }             set             {                 _currentuserid = value;                 settings.authentification.currentuserid = _currentuserid;             }         }     }       public class localsettings : ilocalsettings     {         public t getvalue<t>(string key)         {             if (applicationdata.current.localsettings.values.containskey(key))                 return (t)applicationdata.current.localsettings.values[key];              return default(t);         }          public void setvalue(string key, object value)         {             if (value == null)                 applicationdata.current.localsettings.values.remove(key);              applicationdata.current.localsettings.values[key] = value;         }     }      public interface ilocalsettings     {         t getvalue<t>(string key);         void setvalue(string key, object value);     }      public static class settings     {         private static readonly ilocalsettings _settings;          static settings()         {             _settings = new localsettings();         }          public static class authentification         {             private const string currentuserkey = "currentuserid";             public static int currentuserid             {                 { return _settings.getvalue<int>(currentuserkey); }                 set { _settings.setvalue(currentuserkey, value); }             }              private const string sessiontokenkey = "sessiontoken";             public static string sessiontoken             {                 { return _settings.getvalue<string>(sessiontokenkey); }                 set { _settings.setvalue(sessiontokenkey, value); }             }         }     } 

when app starts, try check if existauthdata in appuser

if (!appuser.existauthdata)             {                 ...             } 

and throw me exception:

'appuser.existauthdata' threw exception of type 'system.typeinitializationexception'

but when try value before appuser.existauthdata every things fine:

var temp = applicationdata.current.localsettings.values.containskey("anykey"); if (!appuser.existauthdata) 

why it's happening?

upd

 @ system.stubhelpers.stubhelpers.getwinrtfactoryobject(intptr pcpcmd)    @ windows.storage.applicationdata.get_current()    @ eventsnotifier.helpers.localsettings.getvalue[t](string key) in e:\new projects\events\eventsnotifier\eventsnotifier\helpers\settings.cs:line 9    @ eventsnotifier.helpers.settings.authentification.get_sessiontoken() in e:\new projects\events\eventsnotifier\eventsnotifier\helpers\settings.cs:line 70    @ eventsnotifier.helpers.appuser.restorepreviewuserstate() in e:\new projects\events\eventsnotifier\eventsnotifier\helpers\appuser.cs:line 13    @ eventsnotifier.helpers.appuser..cctor() in e:\new projects\events\eventsnotifier\eventsnotifier\helpers\appuser.cs:line 8 

i tried reproducing issue , managed reproduce while debugging it:

  • even if appuser.existauthdata first call in app constructor, worked fine long didn't put breakpoints before call.
  • if put breakpoint before call , hovered on appuser.existauthdata property, managed reproduce error exact same stack trace.

the reason seems if try initialize localsettings (first call) while main thread stopped, throws exception (you can see in debugger tooltip). happens, there's no way use appuser class more, because exception thrown static constructor called single time , rethrows same exception @ future attempt acces it. i've blogged behavior years ago.

if access localsettings before call, have initialized it, making sure future attempts access don't fail if main thread stopped. way, works fine if hover on property in debugger.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -