安全手柄已关闭 - 月亮APNS

时间:2014-08-21 17:05:09

标签: c# apple-push-notifications unhandled-exception moonapns

我正在尝试开发自己的Apple Push Notification Server应用程序,以向iOS设备发送推送通知。我从GitHub下载了Moon-APNS的源代码,并对其进行了修改以适应我的Web API应用程序。

以下代码行导致IIS抛出未处理的win32异常:

apnsStream.BeginRead(response, 0, 6, new AsyncCallback(ReadResponse), new MyAsyncInfo(response, apnsStream));

我查看我的计算机应用程序Windows登录事件查看器,未处理的异常来自ReadResponse方法。我有一个包含ReadResponse方法和apnsStream.BeginRead语句中包含的try-catch调用的代码,它仍然会抛出一个未处理的异常。

  

消息:安全句柄已关闭

     

堆栈跟踪:在System.Net.LazyAsyncResult.Complete(IntPtr userToken)   在System.Net.FixedSizeReader.ReadCallback(IAsyncResult   transportResult)在System.Net.LazyAsyncResult.Complete(IntPtr   userToken)   System.Threading.ExecutionContext.RunInternal(执行上下文   executionContext,ContextCallback回调,对象状态,布尔值   preserveSyncCtx)at   System.Threading.ExecutionContext.Run(执行上下文   executionContext,ContextCallback回调,对象状态,布尔值   preserveSyncCtx)at   System.Threading.ExecutionContext.Run(执行上下文   executionContext,ContextCallback回调,对象状态)at   System.Net.ContextAwareResult.Complete(IntPtr userToken)at   System.Net.Sockets.BaseOverlappedAsyncResult.CompletionPortCallback(UInt32的   errorCode,UInt32 numBytes,NativeOverlapped * nativeOverlapped)at   System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32的   errorCode,UInt32 numBytes,NativeOverlapped * pOVERLAP)

以下是我将TcpClient打开到Apple推送通知服务器时的完整部分来源:

using(TcpClient apnsClient = new TcpClient())
{
    //open connection to the Apple Push Notification service a TCP client connection
    apnsClient.Connect("gateway.sandbox.push.apple.com", 2195);

    //open an SSL Stream
    using(SslStream apnsStream = new SslStream(apnsClient.GetStream(), false, ValidateServerCertificate, SelectLocalCertificate))
    {
        apnsStream.AuthenticateAsClient("gateway.sandbox.push.apple.com", certificates, System.Security.Authentication.SslProtocols.Default, false);

        _CanReadStream = apnsStream.CanRead;
        _IsConnected = true;

        var response = new byte[6];

        apnsStream.BeginRead(response, 0, 6, new AsyncCallback(ReadResponse), new MyAsyncInfo(response, apnsStream));

        //put code to generate payload here           
    }
}

来自ReadResponse方法的源代码

private void ReadResponse(IAsyncResult ar)
{
    if (_IsConnected) return;

    int payLoadIndex = 0;
    string payLoadId = string.Empty;

    try
    {
        var info = ar.AsyncState as MyAsyncInfo;
        info.Stream.ReadTimeout = 100;

        if (_CanReadStream)
        {
            var command = Convert.ToInt16(info.ByteArray[0]);
            var status = Convert.ToInt16(info.ByteArray[1]);
            var id = new byte[4];
            Array.Copy(info.ByteArray, 2, id, 0, 4);

            payLoadId = Encoding.Default.GetString(id);
            payLoadIndex = int.Parse(payLoadId) - 1000;

            _RejectedDevices.Add(_NotificationPayload[payLoadIndex].DeviceToken);

             _IsConnected = false;
         }
    }
    catch(Exception ex)
    {
        throw ex;
    }
}

如何解决此问题?

0 个答案:

没有答案