我正在尝试开发自己的 Apple Push Notification Server 应用程序来向 iOS 设备发送推送通知。我从 GitHub 下载了Moon-APNS的源代码,并对其进行了修改以适合我的 Web API 应用程序。
以下代码行导致 IIS 引发未处理的 win32 异常:
apnsStream.BeginRead(response, 0, 6, new AsyncCallback(ReadResponse), new MyAsyncInfo(response, apnsStream));
我在我的计算机应用程序窗口中查看事件查看器,未处理的异常来自该ReadResponse
方法。我有包含在语句中的 ReadResponse 方法和apnsStream.BeginRead
调用的代码try-catch
,它仍然会引发未处理的异常。
消息:安全手柄已关闭
堆栈跟踪:在 System.Net.LazyAsyncResult.Complete(IntPtr userToken) 在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext) 在 System.Net.FixedSizeReader.ReadCallback(IAsyncResult transportResult) 在 System.Net.LazyAsyncResult.Complete(IntPtr userToken) , ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Net.ContextAwareResult.Complete(IntPtr userToken) 在 System.Net.Sockets.BaseOverlappedAsyncResult.CompletionPortCallback(UInt32 errorCode, UInt32 numBytes,NativeOverlapped* nativeOverlapped) 在 System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
这是我打开 TcpClient 到 Apple Push Notification Server 时的完整源代码部分:
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;
}
}
可以做些什么来解决这个问题?