Skip to content

Commit 1dcc36d

Browse files
authored
Preventing the app from crashing
1 parent 434d2ea commit 1dcc36d

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

ContosoApp/App.xaml.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ protected override void OnLaunched(LaunchActivatedEventArgs e)
7979
{
8080
switch (dataSource.ToString())
8181
{
82-
case "Rest": UseRest(); break;
82+
case "Rest": if (!UseRest()) UseSqlite(); break;
8383
default: UseSqlite(); break;
8484
}
8585
}
@@ -125,11 +125,19 @@ public static void UseSqlite()
125125
/// Configures the app to use the REST data source. For convenience, a read-only source is provided.
126126
/// You can also deploy your own copy of the REST service locally or to Azure. See the README for details.
127127
/// </summary>
128-
public static void UseRest()
128+
public static bool UseRest()
129129
{
130-
var accessToken = Task.Run(async () => await MsalHelper.GetTokenAsync(Constants.WebApiScopes)).Result;
130+
try
131+
{
132+
var accessToken = Task.Run(async () => await MsalHelper.GetTokenAsync(Constants.WebApiScopes)).Result;
131133

132-
Repository = new RestContosoRepository($"{Constants.ApiUrl}/api/", accessToken);
134+
Repository = new RestContosoRepository($"{Constants.ApiUrl}/api/", accessToken);
135+
}
136+
catch (System.Exception ex)
137+
{
138+
return false;
139+
}
140+
return true;
133141
}
134142
}
135-
}
143+
}

0 commit comments

Comments
 (0)