问题描述
Pushsharp 4.0版
Pushsharp version 4.0
我收到一个异常"System.InvalidOperationException:就添加而言,该集合已被标记为完成".经过几个小时的搜索,我才知道这是因为队列通知在第二次调用时不接受相同的ID.我的问题是,如果此服务正在运行,我会向User-token-1发送通知,几秒钟后,如果我向具有相同User-token-1的同一用户发送通知,则会得到异常.
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.
要产生此问题,只需将通知两次发送给同一用户,但两次(而不是在单个自定义消息中)两次调用此代码功能.我发送两次是因为我需要向同一用户发送多个通知,但不能在单个推送消息中发送,而要在不同的推送消息中发送.为了使您可以重现此错误,请理解以下情况,并忽略我的自定义变量.即使我忽略了一次仅调用一次SendPushMessageToEmployees之类的操作,但是稍后如果我需要在同一运行的服务上几分钟后向同一用户发送通知,那么我将收到相同的异常.
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);
}
}
异常:System.InvalidOperationException:对于添加项,该集合已标记为完成. StackTrace:System.Collections.Concurrent.BlockingCollection 1.TryAddWithNoTimeValidation(T item, Int32 millisecondsTimeout, CancellationToken cancellationToken) at PushSharp.Core.ServiceBroker
1.QueueNotification(TNotification通知)
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.ServiceBroker
1.QueueNotification(TNotification notification)
我的项目中的课程
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);
}
}
}
`
我已经看到了这个 answer 但没有帮助.当我使用Push Sharp Nuget软件包时.我也尝试联系推锐作者.到目前为止,没有任何回应.任何有相同问题的人请发表评论.
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.
问候阿玛德(Ammad)
RegardsAmmad
推荐答案
该异常神奇地消失了.我手动下载了push sharp的源代码.而不是针对Nuget包.我从解决方案中引用了pushsharp.core和pushsharp.Apple,但无法重现相同的问题.解决方案运行良好,解决方案中没有本地项目参考的问题.我认为可能是pushsharp Nuget软件包的问题.
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.
然后,我从解决方案中删除了pushsharp.core和pushsharp.Apple项目,然后再次下载并引用了PushSharp nuget包.这次,它工作正常,没有上述异常"System.InvalidOperationException:集合已被标记为完成添加".
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".
这篇关于关于APNSBroker队列通知推送中的新增内容,该集合已被标记为已完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!