I'm in the process of adding in-app purchases IAP
to my UWP
app but when testing the process, it crashes the app but only once a purchase has been approved via the test dialog that's displayed when calling:
await CurrentAppSimulator.RequestProductPurchaseAsync(
IAPs.CoreFeatures, false);
Here is the full function that's contained in my ViewModel:
private async void UnlockFeaturesNow()
{
LicenseInformation licenseInformation = App.LicenseInformation;
if (!licenseInformation.ProductLicense[IAPs.CoreFeatures].IsActive)
{
try
{
// The customer doesn't own this feature, so show the purchase dialog.
await CurrentAppSimulator.RequestProductPurchaseAsyn(
IAPs.CoreFeatures, false);
//Check the license state to determine if the in-app purchase was successful.
if (!licenseInformation.ProductLicenses[IAPs.CoreFeatures].IsActive)
{
await this._messageBoxService.Show("features were not purchased.",
"Features");
}
else
{
await this._messageBoxService.Show("features were purchased
successfully.", "Features");
//Remove the purchase button from splitview popup menu.
SectionModel unlockFeatureSection = this.Sections.FirstOrDefault
(m => m.PageName == PageNameEnum.UnlockFeaturesPage);
if (unlockFeatureSection != null)
{
this.Sections.Remove(unlockFeatureSection);
unlockFeatureSection = null;
}
}
}
catch (Exception ex)
{
// The in-app purchase was not completed because an error occurred.
await this._messageBoxService.Show("Failed to purchase the features for
the following reason: " + ex.Message, "Features");
}
}
else
{
// The customer already owns this feature.
}
}
My app never crashes if I cancel the 'IAP test dialog' or if I select any other options other than OK.
If I select OK, it tells me that my IAP was purchased successfully and depending on where I'm in my app it works as expected but the second I click on my hamburger menu, it displays my SplitView
menu and if I click anywhere else on my app it just crashes and displays the following error:
"A debugger is attached to myApp.exe but not configured to debug
this unhandled exception. To debug this exception, detach the current
debugger."
I checked my event viewer for additional information and this is what I found:
Faulting application name: MyApp.exe, version: 1.0.0.0, time stamp: 0x563304b4
Faulting module name: combase.dll, version: 10.0.10586.103, time stamp: 0x56a84cbb
Exception code: 0xc000027b
Fault offset: 0x00166d7e
Faulting process ID: 0x2138
Faulting application start time: 0x01d17a76e526db18
Faulting application path: C:\MyFolder\MyApp\bin\x86\Debug\AppX\MyApp.exe
Faulting module path: C:\WINDOWS\SYSTEM32\combase.dll
Report ID: bb041374-507f-4927-84b8-75bf7cb6df63
Faulting package full name: MyApp1.1.25.0_x86__z2199h13vtehs
Faulting package-relative application ID: App
So it looks like Combase.dll is crashing, but I have no idea what this .dll is for and why is it crashing only after selecting 'OK' and not the other options.
I've just tried the UWP Store
sample provided by Microsoft
which uses SplitView
and provides the same IAP (scenario 2) option I'm using and when I'm selecting OK
on the purchase 'IAP Test' dialog, everything works as expected, so it would make you think it's something to do with my code but I'm pretty sure it isn't as the app never crashes when used and never crashes when the IAP test dialog is displayed unless I select 'OK' option.
I will keep investigating tomorrow as one big difference between my app and the Microsoft sample is that I'm using MVVM
and this code is in my ViewModel
rather than in the Code-Behind
from XAML Page.
Can you let me know if you've experience this problem and if you've managed resolved it?
Thanks.
I just found the answer to my problem by fluke and I still can't believe how poorly it is handled by .NET.
The culprit was removing the "Purchase" option item from a Listbox
contained in the SplitView
menu once the IAP
is purchased successfully! Nothing to do with CurrentApp
or CurrentAppSimulator
:
//Remove the purchase button from splitview popup menu.
SectionModel unlockFeatureSection = this.Sections.FirstOrDefault
(m => m.PageName == PageNameEnum.UnlockFeaturesPage);
if (unlockFeatureSection != null)
{
this.Sections.Remove(unlockFeatureSection);
unlockFeatureSection = null;
}
without refreshing the list (Sections in this instance) of items which is binded to my Listbox
in the SplitView
Menu. So now, when remove the item, I make sure to raise an event via mvvmlight
which will rebuild my list in my MainViewModel.
In my MainViewModel, I have the following in the constructor:
Messenger.Default.Register<bool>(this,
Tokens.LicenseInformationChanged, (value) =>
{
InitializeSections();
});
And I've now changed my Sections from a List
to an ObservableCollection
:
private void InitializeSections()
{
this.Sections = (this.Sections
?? new ObservableCollection<SectionModel>());
this.Sections.Clear();
.... Add sections (or not!)
}
But again, what a dreadful way .NET
is handling this! Bombing out my app throwing the error without a proper exception being thrown is just dreadful!
Hope this helps others.
I think it crashes because you should use this code for your licenseInformation
#if DEBUG
LicenseInformation licenseInformation = CurrentAppSimulator.LicenseInformation;
#else
LicenseInformation licenseInformation = CurrentApp.LicenseInformation;
User contributions licensed under CC BY-SA 3.0