2

私のPushSharpサービスはWindowsサービスで自己ホストされています
が、しばらくすると常に次のようにスローされます:
Events_OnNotificationSendFailure -例外は「ThreadPoolに十分な空きスレッドがなく、操作を完了できませんでした」 .

それを行う適切な方法は何ですか?

public partial class PushNotificationService : ServiceBase
{
    private static bool flagNotification = true;
    static PushService push;


    protected override void OnStart(string[] args)
    {
        SetPushService();

        //start console service worker
        Thread t = new Thread(NotificationServiceWorker);
        t.IsBackground = true;
        t.Start();
    }

    private static void NotificationServiceWorker()
    {
        try
        {
            int sendNotificationTimeGap = Convert.ToInt32(ConfigurationManager.AppSettings["SendNotificationTimeGap"]);
            while (flagNotification)
            {
                try
                {
                    /// get IosPushNotificationService
                    IosPushNotificationService pns = new IosPushNotificationService();
                    /// Get The New Notification from db
                    List<NewPushNotifications> notificationToSend = pns.GetIosNotificationsToSend().Where(n => (n.CreatedDate ?? DateTime.Now) > DateTime.Now.AddMinutes(-5)).ToList();


                    if (notificationToSend != null && notificationToSend.Count > 0)
                    {
                        SendNotificationToIphone(notificationToSend.Where(gn => gn.deviceType.Value == (int)DeviceType.Iphone).ToList());
                        SendNotificationToAndroid(notificationToSend.Where(gn => gn.deviceType.Value == (int)DeviceType.Android).ToList());
                    }

                    Thread.Sleep(sendNotificationTimeGap);
                }
                catch (Exception ex)
                {
                    CustomError.Error("error in flagNotification loop", ex);
                }
            }
        }
        catch (Exception ex)
        {
            CustomError.Error("error in NotificationServiceWorker", ex);
        }
    }

    private static void SetPushService()
    {
        push = new PushService();

        push.Events.OnDeviceSubscriptionIdChanged += new PushSharp.Common.ChannelEvents.DeviceSubscriptionIdChanged(Events_OnDeviceSubscriptionIdChanged);
        push.Events.OnDeviceSubscriptionExpired += new PushSharp.Common.ChannelEvents.DeviceSubscriptionExpired(Events_OnDeviceSubscriptionExpired);
        push.Events.OnChannelException += new PushSharp.Common.ChannelEvents.ChannelExceptionDelegate(Events_OnChannelException);
        push.Events.OnNotificationSendFailure += new PushSharp.Common.ChannelEvents.NotificationSendFailureDelegate(Events_OnNotificationSendFailure);
        push.Events.OnNotificationSent += new PushSharp.Common.ChannelEvents.NotificationSentDelegate(Events_OnNotificationSent);

        string androidSenderId = ConfigurationManager.AppSettings["AndroidSenderId"];
        string androidSenderAuthToken = ConfigurationManager.AppSettings["AndroidSenderAuthToken"];
        string androidPackage = ConfigurationManager.AppSettings["androidPackage"];


        push.StartGoogleCloudMessagingPushService(new GcmPushChannelSettings(androidSenderId, androidSenderAuthToken, androidPackage), new PushSharp.Common.PushServiceSettings() { AutoScaleChannels = false });

        string appleCertificates = ConfigurationManager.AppSettings["AppleCertificates"];
        var appleCert = File.ReadAllBytes(appleCertificates);
        var appleCertPassword = ConfigurationManager.AppSettings["AppleCertPassword"];
        var appleIsProduction = ConfigurationManager.AppSettings["AppleIsProduction"].ToLower() == bool.TrueString;

        push.StartApplePushService(new ApplePushChannelSettings(appleIsProduction, appleCert, appleCertPassword));


    }

}

4

1 に答える 1

0

代わりにスレッドを使用しないでください。このようにタイマーを使用できます

protected override void OnStart(string[] args)
    {
        NotificationServiceEventLog.WriteEntry("Service Started at"+DateTime.Now);

        if (oNotificationComponent ==null)
        oNotificationComponent = new NotificationComponent();

見出し

        try
        {

            if (StartAppleNotificationService())
                StartTimer();


        }
        catch (Exception ex)
        {

            NotificationServiceEventLog.WriteEntry(ex.Message);
        }


       base.OnStart(args);
    }

プライベート ブール StartAppleNotificationService() {

        bool IsStarted = false;
        try
        {

            if (oPushService == null)
            oPushService = new PushService();

            NotificationServiceEventLog.WriteEntry("Apple Notification Service started successfully");
            oPushService.Events.OnChannelCreated += new PushSharp.Common.ChannelEvents.ChannelCreatedDelegate(Events_OnChannelCreated);
            oPushService.Events.OnChannelDestroyed += new PushSharp.Common.ChannelEvents.ChannelDestroyedDelegate(Events_OnChannelDestroyed);
            oPushService.Events.OnChannelException += new PushSharp.Common.ChannelEvents.ChannelExceptionDelegate(Events_OnChannelException);
            oPushService.Events.OnDeviceSubscriptionExpired += new PushSharp.Common.ChannelEvents.DeviceSubscriptionExpired(Events_OnDeviceSubscriptionExpired);
            oPushService.Events.OnDeviceSubscriptionIdChanged += new PushSharp.Common.ChannelEvents.DeviceSubscriptionIdChanged(Events_OnDeviceSubscriptionIdChanged);
            oPushService.Events.OnNotificationSendFailure += new PushSharp.Common.ChannelEvents.NotificationSendFailureDelegate(Events_OnNotificationSendFailure);
            oPushService.Events.OnNotificationSent += new PushSharp.Common.ChannelEvents.NotificationSentDelegate(Events_OnNotificationSent);

            byte[] appleCert = File.ReadAllBytes("PushNSCert.p12");
            var settings = new ApplePushChannelSettings(true, appleCert, "123456");
            oPushService.StartApplePushService(settings);

            IsStarted = true;

        }
        catch (Exception ex)
        {

           throw new Exception("Exception in starting Apple Service :" + ex.Message + Environment.NewLine + ex.StackTrace);
        }

        return IsStarted;
    }
    private bool StartTimer() {


        try
        {
            Double Ms = Convert.ToDouble(ConfigurationManager.AppSettings["TickInterval"]);
            NotificationServiceEventLog.WriteEntry("Time Interval" + Ms.ToString());
            MyTimer = new Timer();
            MyTimer.Interval += (1)*(60)*(1000);
            MyTimer.Enabled = true;
            MyTimer.Elapsed += new ElapsedEventHandler(ServiceTimer_Tick);
        }
        catch (Exception ex) {

            throw ex;

        }
        return true;
    } 
    private void ServiceTimer_Tick(object sender, System.Timers.ElapsedEventArgs e)
    {
        string SentNotificationIDz = "";
        try
        {
            MyTimer.Enabled = false;

            string szConnectionString = @"Server=.\SQL2K8;Database=PaychecksDB_Live;User Id=sa;Password=tdsadmin@321";
            //ConfigurationManager.AppSettings["connString"];

            lNotifictaion = oNotificationComponent.GetNotificationsList(szConnectionString);

            foreach (NotificationModel oNotificationModel in lNotifictaion)
            {
                SendNotification(oNotificationModel.DeviceToken, oNotificationModel.NotificationMessage + " (" + oNotificationModel.NotificationTitle + ")");
                if (SentNotificationIDz == null)
                    SentNotificationIDz += oNotificationModel.Oid;
                else
                    SentNotificationIDz += "," + oNotificationModel.Oid;
            }
            oNotificationComponent.DeleteSentNotifications(SentNotificationIDz, szConnectionString);
            MyTimer.Enabled = true;
        }
        catch (Exception ex) {
            throw ex;
        }

      //  
    }
    private void SendNotification(string p_szDeviceToken,string p_szAlert)
    {

        try
        {

            oPushService.QueueNotification(NotificationFactory.Apple()
                                                      .ForDeviceToken(p_szDeviceToken)
                                                      .WithAlert(p_szAlert)
                                                      .WithBadge(2)
                                                      .WithSound("default")
                                                      .WithExpiry(DateTime.Now.AddDays(1))
                                                       );
        }
        catch (Exception ex)
        {
            throw new Exception("Error in sending Push Notification:" + ex.Message + Environment.NewLine + ex.StackTrace);
        }
    }
于 2013-02-03T16:08:26.590 に答える