【发布时间】:2019-10-30 11:16:01
【问题描述】:
我们开发了一个 Windows 服务并在开发时使用它, 安装和工作在开发时间是正常的, 在生产端安装服务时,服务 开始抛出聚合函数
以下是抛出异常的日志错误,只在客户端生产服务器抛出
System.AggregateException: One or more errors occurred. ---> System.NullReferenceException:
Object reference not set to an instance of an object.
at AutoLeaders.Infrastructure.ParseHelper.<GetInstallations>d__2.MoveNext()
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
at System.Threading.Tasks.Task`1.get_Result()
at AutoLeaders.PushNotifications.PushNotificationsSender.notificationTimer_Elapsed(Object sender, ElapsedEventArgs e)
---> (Inner Exception #0) System.NullReferenceException: Object reference not set to an instance of an object.
at AutoLeaders.Infrastructure.ParseHelper.<GetInstallations>d__2.MoveNext()<---
System.AggregateException: One or more errors occurred. ---> System.NullReferenceException: Object reference not set to an instance of an object.
at AutoLeaders.Infrastructure.ParseHelper.<GetInstallations>d__2.MoveNext()
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
at System.Threading.Tasks.Task`1.get_Result()
at AutoLeaders.PushNotifications.PushNotificationsSender.notificationTimer_Elapsed(Object sender, ElapsedEventArgs e)
---> (Inner Exception #0) System.NullReferenceException: Object reference not set to an instance of an object.
at AutoLeaders.Infrastructure.ParseHelper.<GetInstallations>d__2.MoveNext()<---
以下是代码示例
namespace AutoLeaders.PushNotifications
{
public partial class PushNotificationsSender : ServiceBase
{
Timer _notificationTimer;
// Keep track of the last processed id.
int _lastProcessedId;
public PushNotificationsSender()
{
InitializeComponent();
AutoLog = false;
eventLogComponent = new EventLog();
if (!EventLog.SourceExists("AL Push Notifications"))
{
EventLog.CreateEventSource("AL Push Notifications", "AL add-on Services");
}
eventLogComponent.Source = "AL Push Notifications";
eventLogComponent.Log = "AL add-on Services";
}
protected override void OnStart(string[] args)
{
_notificationTimer = new Timer();
_notificationTimer.Elapsed += notificationTimer_Elapsed;
_notificationTimer.Interval = 25 * 1000;
_notificationTimer.Start();
LogData.LogDataFile(string.Format("AL Push Notifications Service started at {0}.", DateTime.Now));
eventLogComponent.WriteEntry(string.Format("AL Push Notifications Service started at {0}.", DateTime.Now), EventLogEntryType.Information, LogCodes.ServiceStarted);
}
public void OnDebug()
{
_notificationTimer = new Timer();
_notificationTimer.Elapsed += notificationTimer_Elapsed;
_notificationTimer.Interval = 25 * 1000;
_notificationTimer.Start();
LogData.LogDataFile(string.Format("AL Push Notifications Service started at {0}.", DateTime.Now));
eventLogComponent.WriteEntry(string.Format("AL Push Notifications Service started at {0}.", DateTime.Now), EventLogEntryType.Information, LogCodes.ServiceStarted);
notificationTimer_Elapsed(null, null);
}
void notificationTimer_Elapsed(object sender, ElapsedEventArgs e)
{
_notificationTimer.Enabled = false;
try
{
// Code to handle timer
while (true)
{
// Get installations from web service
// Async function to call push notifications for mobile
}//while true
}
catch (Exception ex)
{
try
{// it seems exception cached here
File.AppendAllText(AppDomain.CurrentDomain.BaseDirectory + "Log.txt", ex.ToString() + "\n\n");
}
catch
{
}
}
_notificationTimer.Enabled = true;
}// notificationTimer
}
}
更新
生产服务器是 Windows Server 2012 ,创建设置的 Visual Studio 是 Visual Studio 2019
这里有更多关于代码的内容
protected override void OnStart(string[] args)
{
_notificationTimer = new Timer();
_notificationTimer.Elapsed += notificationTimer_Elapsed;
_notificationTimer.Interval = 25 * 1000;
_notificationTimer.Start();
LogData.LogDataFile(string.Format("AL Push Notifications Service started at {0}.", DateTime.Now));
eventLogComponent.WriteEntry(string.Format("AL Push Notifications Service started at {0}.", DateTime.Now), EventLogEntryType.Information, LogCodes.ServiceStarted);
}
void notificationTimer_Elapsed(object sender, ElapsedEventArgs e)
{
_notificationTimer.Enabled = false;
try
{
LogData.LogDataFile(string.Format("Job started (notificationTimer_Elapsed(sender,e)) at {0}.", DateTime.Now));
// Get Installation Objects
var skip = 0;
var pageIndex = 1;
var limit = 100;
while (true)
{
Get Data //
foreach (......)
{
if (true)
{
foreach (......)
{
using (DataBaseContext)
{
if (lastTrackRecord.RecordDate > currentDate)
{
if (true)
{
if (true)
{
// Push Notofication HERE
}
}
}//if currentDate
}// end using
} // end if foreach
}// if engine
#endregion
} // foreach installation
}//while true
}
catch (Exception ex)
{
// Log Data
}
_notificationTimer.Enabled = true;
}// notificationTimer
}
【问题讨论】:
-
您的错误是由您未等待的任务中引发的异常引起的。您需要展示更多代码。
-
@Nick 我更新了我的帖子我添加了更多代码
-
// Push Notofication HERE你有没有机会在那里调用异步方法?比如,返回一个任务? -
public static async Task SendPushMessage(string deviceToken, string message) @Nick 方法的签名
标签: c# multithreading windows-services