如何找到导致异常的代码?

时间:2019-07-01 14:56:19

标签: c# xamarin

我有一个很大的尝试捕获块,它偶尔会在iOS和Android上引发此异常。 例如消息

  

对象引用未设置为对象的实例。

我将错误消息保存在字符串中:

  catch (Exception ex)
  {
    ExceptionMessage = ex.Message.ToString();
  }

但是我不知道是什么引起了错误。是否可以将导致错误的事物的名称保存在字符串中?

我正在真实设备上测试沙盒和Google Play alpha版本。

带有try catch块的代码:

  string ExceptionMessage = "No text yet";
  string SignedData = "", Signature = "";

    public async Task<bool> PurchaseItem(string productId, string payload)
    {
        var purchaseSuccesful = false;

        if (CrossInAppBilling.IsSupported == true)
        {
            var billing = CrossInAppBilling.Current;
            try
            {
                var connected = await billing.ConnectAsync(ItemType.InAppPurchase);
                if (connected == true)
                {
                    var verify = DependencyService.Get<IInAppBillingVerifyPurchase>();

                    var purchase = await billing.PurchaseAsync(productId, ItemType.InAppPurchase, "apppayload", verify);

                    if (verify != null)
                    {
                        SignedData = ((Verify)verify).SignedDataverify;
                        Signature = ((Verify)verify).Signatureverify;
                        ExceptionMessage = "No error message yet";
                    }
                    else
                    {
                        ExceptionMessage = "No error message yet";
                    }

                    if (purchase == null)
                    {
                        //did not purchase
                        purchaseSuccesful = false;
                    }
                    else
                    {
                        if (purchase.State == PurchaseState.Purchased)
                        {
                            purchaseSuccesful = true;

                            if (Device.RuntimePlatform == Device.iOS)
                            {
                                if (purchase.ProductId == "Consumable1")
                                {
                                    PurchaseditemCurrencyCode = Consum1CurrencyDisplay;
                                    PurchaseditemPurchasePrice = Consum1PriceDisplay;

                                    await DoValidateIOSReceiptAsync(PurchaseditemCurrencyCode, PurchaseditemPurchasePrice, SignedData, Signature);
                                }

                                if ((purchase.ProductId == "Nonconsumable1") && (Nonconsum1purchased == false)) 
                                {
                                    PurchaseditemCurrencyCode = Nonconsum1CurrencyDisplay;
                                    PurchaseditemPurchasePrice = Nonconsum1PriceDisplay;

                                    await DoValidateIOSReceiptAsync(PurchaseditemCurrencyCode, PurchaseditemPurchasePrice, SignedData, Signature);
                                }
                            }
                            else
                            {

                                if (purchase.ProductId == "Consumable1")
                                {
                                    var consumedItem = await billing.ConsumePurchaseAsync(purchase.ProductId, purchase.PurchaseToken);

                                    if (consumedItem != null)
                                    {
                                        PurchaseditemCurrencyCode = Consum1CurrencyDisplay;
                                        PurchaseditemPurchasePrice = Consum1PriceDisplay;

                                        await DoValidateIOSReceiptAsync(PurchaseditemCurrencyCode, PurchaseditemPurchasePrice, SignedData, Signature);
                                    }
                                }

                                if (purchase.ProductId == "Nonconsumable1")
                                {
                                    PurchaseditemCurrencyCode = Nonconsum1CurrencyDisplay;
                                    PurchaseditemPurchasePrice = Nonconsum1PriceDisplay;

                                    await DoValidateIOSReceiptAsync(PurchaseditemCurrencyCode, PurchaseditemPurchasePrice, SignedData, Signature);
                                }

                        }
                    }
                        else
                            purchaseSuccesful = false;
                    }
                }
                else
                {
                    //we are offline or can't connect, don't try to purchase
                    purchaseSuccesful = false;
                }

            }

            catch (InAppBillingPurchaseException purchaseEx)
            {
                //Billing Exception handle this based on the type
                purchaseSuccesful = false;

                var message = string.Empty;
                switch (purchaseEx.PurchaseError)
                {
                    case PurchaseError.AppStoreUnavailable:
                        message = "Currently the app store seems to be unavailble. Try again later.";
                        break;
                    case PurchaseError.BillingUnavailable:
                        message = "Billing seems to be unavailable, please try again later.";
                        break;
                    case PurchaseError.PaymentInvalid:
                        message = "Payment seems to be invalid, please try again.";
                        break;
                    case PurchaseError.PaymentNotAllowed:
                        message = "Payment does not seem to be enabled/allowed, please try again.";
                        break;
                }

                //Decide if it is an error we care about and display message to user
                if (string.IsNullOrWhiteSpace(message))
                    Debug.WriteLine("Error: " + "Other Error");
                else
                    Debug.WriteLine("Error: " + message);


            }
            catch (Exception ex)
            {
                //Issue connecting
                ExceptionMessage = ex.Message.ToString();
                purchaseSuccesful = false;
            }
            finally
            {
                await billing.DisconnectAsync();
            }

        }

        CrossInAppBilling.Dispose();

        return purchaseSuccesful;
    }

验证课程:

using System.Threading.Tasks;
using Plugin.InAppBilling.Abstractions;
using InapppurchaseTest;

[assembly: Xamarin.Forms.Dependency(typeof(Verify))]
namespace InapppurchaseTest
{
  public class Verify : IInAppBillingVerifyPurchase
  {
    const string key1 = @"XOR_key1";
    const string key2 = @"XOR_key2";
    const string key3 = @"XOR_key3";

    public string SignedDataverify = "", Signatureverify = "";

    public Task<bool> VerifyPurchase(string signedData, string signature, string productId = null, string transactionId = null)
    {
    #if __ANDROID__
    var key1Transform = Plugin.InAppBilling.InAppBillingImplementation.InAppBillingSecurity.TransformString(key1, 1);
    var key2Transform = Plugin.InAppBilling.InAppBillingImplementation.InAppBillingSecurity.TransformString(key2, 2);
    var key3Transform = Plugin.InAppBilling.InAppBillingImplementation.InAppBillingSecurity.TransformString(key3, 3);
    SignedDataverify = signedData;
    Signatureverify = signature
    return Task.FromResult(Plugin.InAppBilling.InAppBillingImplementation.InAppBillingSecurity.VerifyPurchase(key1Transform + key2Transform + key3Transform, signedData, signature));
    #else
        SignedDataverify = signedData;
        Signatureverify = signature;
        return Task.FromResult(true);
    #endif
    }
  }
}

0 个答案:

没有答案