Exception in release but not debug

2019-07-21 05:14发布

I have wrapped the C# FCM AdminSDK in a WCF. When I publish the code to my local using debug everything works as expected. When I publish the code using release I get a "Object reference not set to an instance of an object." when attempting to instantiate the "Message" object. Why does this happen? The exception happens on the line "var fcmMessage = new Message()"

using FirebaseAdmin;
using FirebaseAdmin.Messaging;
using Google.Apis.Auth.OAuth2;
using ID.Service.PushNotification.Enums;
using ID.Service.PushNotification.Models;
using ID.Service.PushNotification.ServiceHelpers;
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Hosting;

namespace ID.Service.PushNotification.Helpers
{
    public class FcmHelper
    {
        readonly static FirebaseApp app = FirebaseApp.Create(new AppOptions()
        {
            Credential = GoogleCredential.FromFile(HostingEnvironment.MapPath(@"~/App_Data/jq4bb-37597f7301.json"))
        });

        public static void BulkPushNotification(List<EnrolmentModel> enrolments, string message, int messageId, DeepLink path = DeepLink.None)
        {
            foreach (EnrolmentModel enrolment in enrolments)
            {
                PushNotification(enrolment, message, messageId, path);
            }
        }

        public static async void PushNotification(EnrolmentModel enrolment, string message, int messageId, DeepLink path = DeepLink.None)
        {

            try
            {



                var pathLink = (path != DeepLink.None) ? path.GetPath() : "";

                var registrationToken = Encoding.UTF8.GetString(Convert.FromBase64String(enrolment.DeviceToken));

                LogHelper.Error("rt: " + registrationToken);
                LogHelper.Error("msg: " + message);
                LogHelper.Error("pl" + pathLink);

                var fcmMessage = new Message()
                {
                    Token = registrationToken,
                    Android = new AndroidConfig()
                    {
                        Notification = new AndroidNotification()
                        {
                            Body = message,
                            Title = "Title",
                            Sound = "bing"

                            //ClickAction = "rewards",
                            //Color = "#CA5151",
                            //Icon="",                            
                        },                        
                        Priority = Priority.Normal,
                        TimeToLive = TimeSpan.FromSeconds(2419200),

                        //Data = new Dictionary<string, string>()
                        //{
                        //    { "deepLinkPath", pathLink }
                        //},
                    }
                };

                // Send a message to the device corresponding to the provided
                // registration token.                
                string response = await FirebaseMessaging.DefaultInstance.SendAsync(fcmMessage);
                bool successfullySent = false;

                if (response.ToLower().Contains("projects/com-app/messages/0:"))
                {
                    successfullySent = true;
                }

                ResultFeedbackServiceHelper.SaveResultFeedback(
                           response,
                           Convert.ToInt32(messageId),
                           Convert.ToInt32(enrolment.DeviceId),
                           successfullySent,
                           new List<string> { enrolment.DeviceToken }
                       );
            }
            catch (Exception ex)
            {
                ResultFeedbackServiceHelper.SaveResultFeedback(
                           ex.Message,
                           Convert.ToInt32(messageId),
                           Convert.ToInt32(enrolment.DeviceId),
                           false,
                           new List<string> { enrolment.DeviceToken }
                       );

                LogHelper.Error("Error sending push messages to (fcm) gcn " + ex.ToString());
            }

        }
    }
}

Exception:''2019-03-05 15:09:55,637 Thread:'[13]' Level:'ERROR' Message:'Error sending push messages to (fcm) gcn System.NullReferenceException: Object reference not set to an instance of an object. at ID.Service.PushNotification.Helpers.FcmHelper.d__2.MoveNext() in D:\BuildAgents\Agent1_work\475\s\PNS\Main\ID.Service.PushNotification\Helpers\FCMHelper.cs:line 49'

0条回答
登录 后发表回答