我发现在具有Apple推送通知服务的Windows服务中增加了内存使用量。所以我只是将APN部分分成了控制台应用程序并进行了测试。
控制台应用程序应该使用1分钟计时器并在已用时间事件上建立新的连接APN服务器。
我的代码如下所示
class Program
{
static ApplePushNotification.PushNotificationService apn = new ApplePushNotification.PushNotificationService(ApplePushNotification.ServiceEnvironments.Production);
const double interval60Mins = 1 * 60 * 1000;
static System.Timers.Timer checkForTime = new System.Timers.Timer(interval60Mins);
static void Main(string[] args)
{
checkForTime.Elapsed += new System.Timers.ElapsedEventHandler(checkForTime_Elapsed);
checkForTime.Enabled = true;
apn.Connect();
Console.ReadKey();
}
static void checkForTime_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
Console.WriteLine("Timer elapsed at " + DateTime.UtcNow.ToString());
Console.WriteLine("Memory used : " + GC.GetTotalMemory(true));
apn.Close();
apn.Connect();
}
}
public class PushNotificationService
{
public const string PUSH_SANDBOX = "gateway.sandbox.push.apple.com";
public const string PUSH_PRODUCTION = "gateway.push.apple.com";
public X509Certificate2 certificate = null;
public const string PRODUCTION_CERTIFICATE_SUBJECT_NAME = "blind";
public const int PUSH_PORT = 2195;
private static SslStream sslStream = null;
private static System.Net.Sockets.TcpClient client = null;
public CancellationTokenSource tokenSource = null;
Task read, write;
public PushNotificationService(ServiceEnvironments serviceEnvironment)
{
this.serviceEnvironment = serviceEnvironment;
}
private void GetCertificate()
{
string certificate_name = string.Empty;
X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
X509Certificate2Collection certificates = null;
store.Open(OpenFlags.ReadOnly);
X509Certificate2 result = null;
certificates = store.Certificates;
store.Close();
if (serviceEnvironment == ServiceEnvironments.Production)
{
this.hostName = PUSH_PRODUCTION;
certificate_name = PRODUCTION_CERTIFICATE_SUBJECT_NAME;
}
else
{
this.hostName = PUSH_SANDBOX;
certificate_name = SANDBOX_CERTIFICATE_SUBJECT_NAME;
}
foreach (X509Certificate2 cert in certificates)
{
if (cert.SubjectName.Name == certificate_name)
{
certificate = new X509Certificate2(cert);
//System.Diagnostics.Debug.WriteLine("Certificate Found : " + hostname);
break;
}
}
if (certificate == null)
{
Console.WriteLine("Unable to find correct certificate");
throw new Exception("Certficate was not found");
}
}
public void Connect()
{
try
{
tokenSource = new CancellationTokenSource();
var token = tokenSource.Token;
if (this.certificate == null) GetCertificate();
client = new System.Net.Sockets.TcpClient();
//connecting to the push interface
client.Connect(hostName, PUSH_PORT);
sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
var certificateCollections = new X509Certificate2Collection(this.certificate);
try
{
sslStream.AuthenticateAsClient(hostName, certificateCollections, SslProtocols.Default, true);
}
catch (AuthenticationException e)
{
if (e.InnerException != null)
{
Console.WriteLine("Inner exception: " + e.InnerException.Message);
}
Console.WriteLine("Authentication failed - closing the connection.");
client.Close();
}
catch (Exception ex)
{
}
read = Task.Factory.StartNew(() => Read(sslStream, token), token);
write = Task.Factory.StartNew(() => Write(sslStream, token), token);
Console.WriteLine("APN Started " + DateTime.UtcNow.ToString());
Console.WriteLine();
}
catch (Exception e)
{
}
}
public void Close()
{
tokenSource.Cancel();
if (read != null) read.Wait();
if (write != null) write.Wait();
read.Dispose();
write.Dispose();
client.Close();
}
}
由于这个控制台,我可以看到内存使用量不断增加。
控制台输出如下所示。
计时器于2016年11月23日上午10:26:44过去了 使用的内存:2621688 APN开始于11/23/2016 10:26:44 AM
计时器已于11/23/2016 10:27:44 AM过去了 使用的内存:2607156 APN开始于11/23/2016 10:27:44 AM
...
计时器已于11/23/2016 11:16:44 AM过去了 使用的内存:4055948 APN 11/23/2016 11:16:44 AM开始
...
计时器于2016年11月23日下午1:38:44过去了 使用的内存:6706152 APN 11/23/2016 1:38:44 PM
...
计时器于2016年11月23日下午2:12:44过去了 使用的内存:8937776 APN开始于2016年11月23日下午2:12:45
任何人都可以帮助我了解发生了什么吗?
谢谢,