Exception while creating SQLite database - c#

I develop a Universal app that use a local SQLite database. As I need to know the database version, I store an additional information that contains this information.
At the application startup, I check if the database exists, and if the database version has changed.
public sealed partial class App : Application
{
// DB Name
public static string DBFileName = "myDb.sqlite";
public static string DBpath;
// DB Version
public static string DBVersionFilename = "myDb.version";
public static string DBVersionPath;
public static string DBVersion = "1.1.0";
public App()
{
this.InitializeComponent();
this.Suspending += this.OnSuspending;
ManageDB();
}
private async Task ManageDB()
{
if (!CheckDbExist().Result)
{
CreateDb();
}
else
{
if (CheckDbVersionUpdated().Result)
{
await DeleteDb();
CreateDb();
}
}
}
private async Task<bool> CheckDbExist()
{
try
{
StorageFile storageFile = await ApplicationData.Current.LocalFolder.GetFileAsync(DBFileName);
DBpath = storageFile.Path;
return true;
}
catch { }
return false;
}
private void CreateDb()
{
try
{
StorageFile storageFile = ApplicationData.Current.LocalFolder.CreateFileAsync(DBFileName, CreationCollisionOption.OpenIfExists).GetResults();
DBpath = storageFile.Path;
var dbconnection = new SQLiteAsyncConnection(DBpath, false);
dbconnection.CreateTableAsync<Article>();
//...
StorageFile file = ApplicationData.Current.LocalFolder.CreateFileAsync(DBVersionFilename, CreationCollisionOption.ReplaceExisting).GetResults();
FileIO.WriteTextAsync(file, DBVersion);
}
catch (Exception e)
{ }
}
private async Task<bool> DeleteDb()
{
try
{
StorageFile storageFile = ApplicationData.Current.LocalFolder.CreateFileAsync(DBFileName, CreationCollisionOption.OpenIfExists).GetResults();
await storageFile.DeleteAsync(StorageDeleteOption.PermanentDelete);
return true;
}
catch { }
return false;
}
private async Task<bool> CheckDbVersionUpdated()
{
try
{
string userDBVersion;
StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(DBVersionFilename);
userDBVersion = await FileIO.ReadTextAsync(file);
if (userDBVersion != DBVersion)
return true;
else
return false;
}
catch { }
return false;
}
}
There is a problem on CreateDB():
if I browse this code step by step, with breakpoint, there is no problem
but if I don't place breakpoint I meet an exception after calling var dbconnection = new SQLiteAsyncConnection(DBpath, false);:
System.InvalidOperationException: A method was called at an unexpected time.
A method was called at an unexpected time.
at Windows.Foundation.IAsyncOperation`1.GetResults()
at TestRating.App.CreateDb()}
Would you have an idea about the encountered problem? Is there another way to do this?

When I remove ".Results()" it seems that the "CreateDb()" function works correctly:
private async Task<bool> CreateDb()
{
try
{
StorageFile storageFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(DBFileName, CreationCollisionOption.OpenIfExists);
DBpath = storageFile.Path;
var dbconnection = new SQLiteAsyncConnection(DBpath, false);
dbconnection.CreateTableAsync<Article>();
//...
StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(DBVersionFilename, CreationCollisionOption.ReplaceExisting);
FileIO.WriteTextAsync(file, DBVersion);
return true;
}
catch (Exception e)
{ }
return false;
}
Do you have an explanation?

Related

UWP c# creating a file in ApplicationData.Current.LocalFolder returns null

I created an app that needs to store user data in a file that is created on Application start and when I compile it, it shows me this System.IO.FileLoadException: Could not find or load a specific file. (Exception from HRESULT: 0x80131621) and this System.NullReferenceException: Invalid pointer.
When the Application starts it calls the Init() Method before everything.
public static class FilesManager
{
public static StorageFolder ROOT_DIRECTORY, USER_DIRECTORY;
public static StorageFile FILES_MANAGER_CACHE_FILE;
public static volatile string FILES_MANAGER_CACHE;
public static volatile StorageFolder FOLDER;
public static async Task Init()
{
try
{
FOLDER = await ApplicationData.Current.LocalFolder.CreateFolderAsync("DashData", CreationCollisionOption.OpenIfExists);
FOLDER = await ApplicationData.Current.LocalFolder.GetFolderAsync("DashData");
if (FOLDER == null) Debug.WriteLine("FOLDER IS NULL.");
FILES_MANAGER_CACHE_FILE = await FOLDER.CreateFileAsync("filesmanageruserdata.json", CreationCollisionOption.OpenIfExists);
FILES_MANAGER_CACHE_FILE = await FOLDER.GetFileAsync("filesmanageruserdata.json");
await LoadFilesManagerCacheAsync();
Debug.WriteLine("TEXT: " + FILES_MANAGER_CACHE);
}
catch(Exception e)
{
Debug.WriteLine("ERROR: " + e.ToString());
}
}
public static async Task LoadFilesManagerCacheAsync()
{
FILES_MANAGER_CACHE = await FileIO.ReadTextAsync(FILES_MANAGER_CACHE_FILE);
}
public static async Task storeUserData(string content)
{
try
{
await FileIO.WriteTextAsync(FILES_MANAGER_CACHE_FILE, content);
}catch(Exception e)
{
Debug.WriteLine("ERROR: " + e.ToString());
}
}
}

Close .Auth/login/done after MSAL login successful in Xamarin application

I have been setting up AD(MSAL) Authentication using azure with my application but am having an issue closing the window that appears after successful sign in. The page that appears within the embedded browser with the link returning to my API homepage simply states "You have successfully signed in" with a link below to return to previous page with goes to my API home page.
The below is my code in my App.xaml.cs
public partial class App : Application
{
public static IPublicClientApplication PCA = null;
public static string ClientID = "********-****-****-****-**********";
public static string[] Scopes = { "User.Read" };
public static string Username = string.Empty;
public static object ParentWindow { get; set; }
public App()
{
InitializeComponent();
}
protected override async void OnStart()
{
PCA = PublicClientApplicationBuilder.Create(ClientID)
//.WithRedirectUri($"msal{ClientID}://auth")
.WithRedirectUri("https://kpiapp-api-dev.azurewebsites.net/.auth/login/aad/callback")
.WithIosKeychainSecurityGroup("com.microsoft.adalcache")
.WithAuthority(AzureCloudInstance.AzurePublic, "********-****-****-****-**********") //TenantID
.Build();
MainPage = new NavigationPage(new LoginPage());
}
protected override void OnSleep()
{
// Handle when your app sleeps
}
protected override void OnResume()
{
// Handle when your app resumes
}
}
And my Loginpage.xaml.cs:
public partial class LoginPage : ContentPage
{
public LoginPage()
{
InitializeComponent();
}
async void OnSignIn(object sender, EventArgs e)
{
AuthenticationResult authResult = null;
IEnumerable<IAccount> accounts = await App.PCA.GetAccountsAsync();
var current = Connectivity.NetworkAccess;
bool connectionFound = false;
if (current == NetworkAccess.Internet)
{
connectionFound = true;
}
string APIData = "";
if(connectionFound == true)
{
try
{
if (SignInButton.Text == "Sign in")
{
try
{
IAccount firstAccount = accounts.FirstOrDefault();
authResult = await App.PCA.AcquireTokenSilent(App.Scopes, firstAccount)
.ExecuteAsync();
}
catch (MsalUiRequiredException ex)
{
try
{
authResult = await App.PCA.AcquireTokenInteractive(App.Scopes)
.WithParentActivityOrWindow(App.ParentWindow)
.ExecuteAsync();
}
catch (Exception ex2)
{
await DisplayAlert("Acquire token interactive failed. See exception message for details: ", ex2.Message, "Dismiss");
}
}
if (authResult != null)
{
var content = await GetHttpContentWithTokenAsync(authResult.AccessToken);
SignInButton.Text = "Sign out";
}
}
else
{
while (accounts.Any())
{
await App.PCA.RemoveAsync(accounts.FirstOrDefault());
accounts = await App.PCA.GetAccountsAsync();
}
});
SignInButton.Text = "Sign in";
}
}
catch (Exception ex)
{
await DisplayAlert("Authentication failed. See exception message for details: ", ex.Message, "Dismiss");
}
await Task.Yield();
APIData = getAPIData();
}
else
{
await DisplayAlert("Connection Error", "Check your internet connection and try again", "Try again");
}
if (APIData != "ConnectionError")
{
await Navigation.PushAsync(new MainPage(APIData));
}
else
{
await Task.Delay(500);
await DisplayAlert("API Download error", "Error connecting to API", "Try again");
}
//MainPage = new MainPage(APIData);
}
public async Task<string> GetHttpContentWithTokenAsync(string token)
{
try
{
//get data from API
HttpClient client = new HttpClient();
HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Get, "https://graph.microsoft.com/v1.0/me");
message.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
HttpResponseMessage response = await client.SendAsync(message);
string responseString = await response.Content.ReadAsStringAsync();
return responseString;
}
catch (Exception ex)
{
await DisplayAlert("API call to graph failed: ", ex.Message, "Dismiss");
return ex.ToString();
}
}
private string getAPIData()
{
string APIData = "";
try
{
APIData = new WebClient().DownloadString("****/api/data");
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
APIData = "ConnectionError";
}
return APIData;
}
}
I'm aware this is doing nothing with that sign in and won't access the api data at the moment. I'm really just looking to get the authentication window closed and then work from there.
Thanks
I managed to solve this issue, by adding .WithUseEmbeddedWebView(true) to the second call of authResult so it appears like this:
catch (MsalUiRequiredException ex)
{
try
{
authResult = await App.PCA.AcquireTokenInteractive(App.Scopes)
.WithParentActivityOrWindow(App.ParentWindow)
.WithUseEmbeddedWebView(true)
.ExecuteAsync();
}
catch (Exception ex2)
{
await DisplayAlert("Acquire token interactive failed. See exception message for details: ", ex2.Message, "Dismiss");
}
}

Xamarin Forms http async request

i am trying to make an asynchronous call to a webservice.
I would like to make this call when opening the app (App.xaml.cs).
According to the answer that comes back to me, it has to navigate to a particular page
But I do not work.
public partial class App : PrismApplication
{
public App(IPlatformInitializer initializer = null) : base(initializer) { }
protected override void OnInitialized()
{
InitializeComponent();
try
{
CheckLogin().Wait();
}
catch (Exception e)
{
var t = e;
}
}
private static async Task CheckLogin()
{
try
{
var login = new Login
{
Email = "test#test.com",
Password = "test",
};
var client = new HttpClient { BaseAddress = new Uri("http://www.api.com/test/") };
var data = JsonConvert.SerializeObject(login);
var content = new StringContent(data, Encoding.UTF8, "application/json");
var response = await client.PostAsync(#"api/it-IT/auth/token", content); //crash without error, freeze
if (response.IsSuccessStatusCode)
{
var successResult = JsonConvert.DeserializeObject<HttpResponseMessage>(response.Content.ReadAsStringAsync().Result);
if (successResult != null)
{
//return true;
}
else
{
//return false;
}
}
}
catch (Exception e)
{
var t = e;
}
}
protected override void RegisterTypes()
{
Container.RegisterTypeForNavigation<NavigationPage>();
Container.RegisterTypeForNavigation<MainPage>();
Container.RegisterTypeForNavigation<MainPage2>();
Container.RegisterTypeForNavigation<MainPage3>();
}
}
When does the postasync call does not go more forward, not I get no errors, but does not proceed.
But if I try the same code in an application console, everything works fine, why?
class Program
{
static void Main(string[] args)
{
Console.WriteLine("A");
CheckLogin().Wait();
Console.WriteLine("K");
Console.ReadKey();
}
private static async Task CheckLogin()
{
try
{
var login = new Login
{
Email = "test#test.com",
Password = "#test",
};
var client = new HttpClient { BaseAddress = new Uri("http://www.api.com/test/") };
var data = JsonConvert.SerializeObject(login);
var content = new StringContent(data, Encoding.UTF8, "application/json");
var response = await client.PostAsync(#"api/it-IT/auth/token", content);
if (response.IsSuccessStatusCode)
{
}
}
catch (Exception e)
{
var t = e;
}
}
}
If I try to do the same operation within a command with wait I do not work the same error, but if I do await, it will work fine, but in App.xaml.cs in OnInitialized() I can not put await
public DelegateCommand callCommand { get; set; }
public MainPage2ViewModel()
{
callCommand = new DelegateCommand(Call);
}
private void Call()
{
//await CheckLogin(); // work
CheckLogin().Wait(); // not work the same problem
var i = "pippo";
}
private async Task CheckLogin()
{
....
}
Is there anything to set with xamarin or with prism?
I've also the same strange error...
i fix with this workaround (use an async void that wrap async task)...
public App()
{
InitializeComponent();
Current.MainPage = new LoadingPage();
}
protected override void OnStart()
{
MagicInit();
base.OnStart();
}
public static async void MagicInit()
{
var f = await FileSystem.Current.LocalStorage.CreateFileAsync("db.sqlite", CreationCollisionOption.OpenIfExists);
DbConnection = f.Path;
await DataService.DbFill();
User = await DataService.Instance.Table<SpUser>().FirstOrDefaultAsync();
Current.MainPage = User != null ? (Page)new MainPage() : new LoginPage();
}

Cannot implicitly convert type 'Windows.Storage.StorageFile' to 'bool'

When i return "FileExist" varable in this code i am getting error..
public static async Task<bool> FileExistsForWp8(string filename)
{
StorageFolder folder = Windows.Storage.ApplicationData.Current.LocalFolder;
try
{
StorageFile FileExists = await folder.GetFileAsync(filename);
return FileExists;
}
catch (Exception ex)
{
return FileExists;
}
}
The GetFileAsync returns a StorageFile.
Thy this:
public static async Task<bool> FileExistsForWp8(string filename)
{
StorageFolder folder = Windows.Storage.ApplicationData.Current.LocalFolder;
try
{
StorageFile File = await folder.GetFileAsync(filename);
return true;
}
catch
{
return false;
}
}

c# winrt delete file

When I delete file like this behind, it throws an exception:
Value does not fall within the expected range.
when deleteasync.
public static async Task DeleteFile(string fileName, string folderPath)
{
try
{
StorageFolder sf = await CheckFolder(folderPath);
StorageFile sfile = await sf.GetFileAsync(fileName);
if (sfile != null)
{
await sfile.DeleteAsync();
}
}
catch (Exception)
{
}
}

Categories

Resources