线程被陷在一个HttpApplication
2023-04-18 12:52:10 时间
我有一个从Global.asax文件启动一个WCF应用程序,有一个应用程序启动事件,代码如下:线程被陷在一个HttpApplication
public class Global : HttpApplication
{
private bool isInitialized = false;
private static readonly ILog log = LogManager.GetLogger(typeof(Global));
private PushController pushController;
private Task pushMonitoringTask;
protected void Application_Start(object sender, EventArgs e)
{
Initialize();
pushMonitoringTask = Task.Run(() => pushController.StartMonitoring());
}
...}
推控制器:
public class PushController
{
private IRepository<Question> QuestionRepository { get; set; }
private IRepository<Mobile_Report> ReportRepository { get; set; }
private readonly string _syncFilePath;
private readonly string certificatePath;
private readonly PushBroker _broker;
private bool _isStopRequired;
public PushController(IRepository<Question> questionRepository, IRepository<Mobile_Report> reportsRepository, HttpServerUtility server)
{
QuestionRepository = questionRepository;
ReportRepository = reportsRepository;
_broker = new PushBroker();
_syncFilePath = server.MapPath("~/SyncFile.txt");
certificatePath = "My cert path";
if (!File.Exists(_syncFilePath))
{
using (StreamWriter sw = File.AppendText(_syncFilePath))
{
sw.WriteLine(DateTime.Now);
sw.Flush();
}
}
_isStopRequired = false;
}
public void StartMonitoring()
{
var delay = Convert.ToInt32(ConfigurationManager.AppSettings["NewQuestionsMonitoringDelay"]);
RunPushServices();
while (!_isStopRequired)
{
using (var fileStream = new FileStream(_syncFilePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
{
DateTime lastSyncDate;
using (var sw = new StreamReader(fileStream))
{
lastSyncDate = Convert.ToDateTime(sw.ReadLine());
}
if (lastSyncDate == default(DateTime))
{
throw new Exception("Corrupted or missing sync date");
}
EnqueueNotificationsList(lastSyncDate);
using (var sw = new StreamWriter(_syncFilePath, false))
{
sw.WriteLine(DateTime.Now);
sw.Flush();
}
}
Thread.Sleep(delay * 1000);
}
//_broker.StopAllServices();//waits for the queue to drain
}
private void RunPushServices()
{
_broker.RegisterAppleService(new ApplePushChannelSettings(false, certificatePath, "My cert password"));
//_broker.RegisterGcmService(new GcmPushChannelSettings("senderId", "sender auth token", "android app package name"));
}
private void EnqueueNotificationsList(DateTime lastSyncDate)
{
var newQuestions = QuestionRepository.GetAll().Where(q => q.UtcDateCreated >= lastSyncDate);
var newReports = ReportRepository.GetAll().Where(r => r.CreatedDate >= lastSyncDate);
EnqueueQuestionsNotifications(newQuestions);
EnqueueReportsNorifications(newReports);
}
private void EnqueueQuestionsNotifications(IEnumerable<Question> newQuestions)
{
foreach (var questionGroup in newQuestions.GroupBy(q => q.Mobile_Sample))
{
var firstQuestion = questionGroup.First();
var targetSample = firstQuestion.Mobile_Sample;
var allSessions = firstQuestion.Mobile_Sample.Mobile_Claim.Mobile_Contact.UserSession;
var questionsNotificationString = BuildQuestionsNotificationString(targetSample, questionGroup.Count());
foreach (var userSession in allSessions)
{
switch (userSession.DeviceType)
{
case (int)DeviceType.IOSDeivce:
_broker.QueueNotification(new AppleNotification()
.ForDeviceToken(userSession.DeviceToken)
.WithAlert(questionsNotificationString)
.WithBadge(1)
.WithSound("default")
.WithCustomItem("PushInfo", "Questions", targetSample.ID));
break;
//case (int)DeviceType.AndroidDevice:
// _broker.QueueNotification(
// new GcmNotification().ForDeviceRegistrationId(userSession.DeviceToken)
// .WithJson(@"{""alert"":""ITEL Questions"",""badge"":7,""sound"":""sound.caf""}");
// break;
}
}
}
}
private void EnqueueReportsNorifications(IEnumerable<Mobile_Report> newReports)
{
foreach (var reportsGroup in newReports.GroupBy(q => q.Mobile_Sample))
{
var firstReport = reportsGroup.First();
var targetSample = firstReport.Mobile_Sample;
var allSessions = firstReport.Mobile_Sample.Mobile_Claim.Mobile_Contact.UserSession;
var reportNotificationString = BuildReportNotificationString(targetSample);
foreach (var userSession in allSessions)
{
switch (userSession.DeviceType)
{
case (int)DeviceType.IOSDeivce:
_broker.QueueNotification(new AppleNotification()
.ForDeviceToken(userSession.DeviceToken)
.WithAlert(reportNotificationString)
.WithBadge(1)
.WithSound("default")
.WithCustomItem("Target", "Reports", targetSample.ID));
break;
//case (int)DeviceType.AndroidDevice:
// _broker.QueueNotification(
// new GcmNotification().ForDeviceRegistrationId(userSession.DeviceToken)
// .WithJson(@"{""alert"":""ITEL Questions"",""badge"":7,""sound"":""sound.caf""}");
// break;
}
}
}
}
private string BuildQuestionsNotificationString(Mobile_Sample targetSample, int count)
{
var claimLastNameString = !string.IsNullOrEmpty(targetSample.Mobile_Claim.IILastName)
? " " + targetSample.Mobile_Claim.IILastName
: string.Empty;
var claimNumberString = !string.IsNullOrEmpty(targetSample.Mobile_Claim.ClaimNumber)
? " Claim #" + targetSample.Mobile_Claim.ClaimNumber + ", "
: " Claim # -";
var areaDamagedString = !string.IsNullOrEmpty(targetSample.AreaDamaged)
? " (" + targetSample.AreaDamaged + ")"
: string.Empty;
return "You have " + count + " new questions for" + claimLastNameString + claimNumberString + targetSample.Mobile_SampleType.Name + areaDamagedString;
}
private string BuildReportNotificationString(Mobile_Sample targetSample)
{
var claimLastNameString = !string.IsNullOrEmpty(targetSample.Mobile_Claim.IILastName)
? " " + targetSample.Mobile_Claim.IILastName
: string.Empty;
var claimNumberString = !string.IsNullOrEmpty(targetSample.Mobile_Claim.ClaimNumber)
? " Claim #" + targetSample.Mobile_Claim.ClaimNumber + ", "
: " Claim # -";
var areaDamagedString = !string.IsNullOrEmpty(targetSample.AreaDamaged)
? " (" + targetSample.AreaDamaged + ")"
: string.Empty;
return "You have a new report for" + claimLastNameString + claimNumberString + targetSample.Mobile_SampleType.Name + areaDamagedString;
}
public void RequestMonitoringStop()
{
_isStopRequired = true;
}
}
线程被陷在一个HttpApplication
相关文章
- 【技术种草】cdn+轻量服务器+hugo=让博客“云原生”一下
- CLB运维&运营最佳实践 ---访问日志大洞察
- vnc方式登陆服务器
- 轻松学排序算法:眼睛直观感受几种常用排序算法
- 十二个经典的大数据项目
- 为什么使用 CDN 内容分发网络?
- 大数据——大数据默认端口号列表
- Weld 1.1.5.Final,JSR-299 的框架
- JavaFX 2012:彻底开源
- 提升as3程序性能的十大要点
- 通过凸面几何学进行独立于边际的在线多类学习
- 利用行动影响的规律性和部分已知的模型进行离线强化学习
- ModelLight:基于模型的交通信号控制的元强化学习
- 浅谈Visual Source Safe项目分支
- 基于先验知识的递归卡尔曼滤波的代理人联合状态和输入估计
- 结合网络结构和非线性恢复来提高声誉评估的性能
- 最佳实践丨云开发CloudBase多环境管理实践
- TimeVAE:用于生成多变量时间序列的变异自动编码器
- 具有线性阈值激活的神经网络:结构和算法
- 内网渗透之横向移动 -- 从域外向域内进行密码喷洒攻击