Мы разработали службу Windows и используем ее во время разработки, установка и работа нормальны во время разработки, когда служба устанавливается на производственной стороне, затем служба запускает функцию Aggregate
это ошибка журнала сгенерированного исключения, которое выдается только на производственном сервере клиента
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
}