0

Pushsharp version 4.0

I am getting an exception "System.InvalidOperationException: The collection has been marked as complete with regards to additions". After searching for hours I came to know this is because the queue notification do not accepts the same Id when its called 2nd time. My problem is let say if this service is running I send a notification to User-token-1 and after few seconds if I send the notification to same user with same User-token-1 then I get the exception.

To produce this issue just send the notification two times to same user but calling this code function two times not in single custom message. I am sending it two times because I have a requirement to send multiple notification to same user but not in single push message but a different push message. To give an idea how this error can be reproduces please understand the following situation and please ignore my custom variables. Even though if I ignore this like calling SendPushMessageToEmployees only one time then later if I need to send notification to same user after few minutes on same running service then I will get same exception.

main()
{
          SendPushMessageToEmployees(MyCustomMessage);
          SendPushMessageToEmployees(MyCustomMessage);
}

 public void SendNotification(List<PushMessage> pushMessages)
        {
            try 
            {
                apnsBroker.Start();
                foreach (PushMessage message in pushMessages)
                {
                    if (!String.IsNullOrEmpty(message.DeviceId) )
                    {
                        apnsBroker.QueueNotification(new ApnsNotification
                        {
                            DeviceToken = message.DeviceId,
                            Payload = JObject.Parse("{\"aps\":{\"badge\":0,\"alert\":'" + message.Message + "',\"Workflow\":'" + message.Message + "'}}")
                        });
                    }
                }
                apnsBroker.Stop(true);
            }
            catch (Exception ex)
            {
                Console.WriteLine("{0}",ex.Message);
            }
        }

Exception : System.InvalidOperationException: The collection has been marked as complete with regards to additions. StackTrace: System.Collections.Concurrent.BlockingCollection1.TryAddWithNoTimeValidation(T item, Int32 millisecondsTimeout, CancellationToken cancellationToken) at PushSharp.Core.ServiceBroker1.QueueNotification(TNotification notification)

Class in my project

public  class  AppleServiceBroker : IServiceBroker 
{
    private ApnsServiceBroker apnsBroker;
    public AppleServiceBroker()
    {
        if (apnsBroker == null)
        {
           var config = new ApnsConfiguration(ApnsConfiguration.ApnsServerEnvironment.Sandbox,
             "PushNotification.p12", "MyPassword");

            apnsBroker = new ApnsServiceBroker(config);

            // Wire up events
            apnsBroker.OnNotificationFailed += (notification, aggregateEx) =>
            {
                aggregateEx.Handle(ex =>
                {
                    if (ex is ApnsNotificationException)
                    {
                        var notificationException = (ApnsNotificationException)ex;
                        var apnsNotification = notificationException.Notification;
                        var statusCode = notificationException.ErrorStatusCode;
                        Console.WriteLine("Apple Notification Failed:  ID= " + apnsNotification.Identifier + " Code= " + statusCode);
                    }
                    else
                    {
                        Console.WriteLine("Apple Notification Failed for some unknown reason : " + ex.InnerException);
                    }

                    return true;
                });
            };
        }
    }


    /// <summary>
    /// Sends notification to apple servers
    /// </summary>
    /// <param name="pushMessages">List of push messages</param>
    public void SendNotification(List<PushMessage> pushMessages)
    {
        try 
        {
            apnsBroker.OnNotificationSucceeded += (notification) =>
            {
                Console.WriteLine("Apple Notification Sent!");
            };
            apnsBroker.Start();

            foreach (PushMessage message in pushMessages)
            {
                if (!String.IsNullOrEmpty(message.DeviceId) )
                {
                    apnsBroker.QueueNotification(new ApnsNotification
                    {
                        DeviceToken = message.DeviceId,
                        Payload = JObject.Parse("{\"aps\":{\"badge\":0,\"alert\":'" + message.Message + "',\"Workflow\":'" + message.Message + "'}}")
                    });
                }

            }
            apnsBroker.Stop(true);

        }
        catch (Exception ex)
        {
            Console.WriteLine("{0}",ex.Message);
        }
    }
}

`

I have seen this answer but not help. As I am using Push Sharp Nuget Package. I also tried to contact push sharp author. No response so far. Anyone having same problem please comment.

Regards Ammad

Community
  • 1
  • 1
Emy
  • 344
  • 5
  • 14

1 Answers1

0

The exception magically disappeared. I manually downloaded the source code of push sharp. Instead of targeting Nuget package. I reference the pushsharp.core and pushsharp.Apple from the solution and I was unable to reproduce the same issue. The solution was working fine without issues with local project reference in solution. I thought may be its problem with pushsharp Nuget package.

Then I delete the pushsharp.core and pushsharp.Apple projects from solution and then again downloaded and referenced the PushSharp nuget package. This time it worked fine without the above exception "System.InvalidOperationException: The collection has been marked as complete with regards to additions".

Emy
  • 344
  • 5
  • 14