I have got some Classes implements IHostedService.
EmailSendService:
public class EMailSendService : IHostedService, IDisposable
{
private Timer _timer;
private IMailAccountService _mailAccountService;
private IMailTaskDAL _mailTaskDal;
private LoggerServiceBase _loggerServiceBase;
public EMailSendService(IMailAccountService mailAccountService, IMailTaskDAL mailTaskDal)
{
_mailAccountService = mailAccountService;
_mailTaskDal = mailTaskDal;
_loggerServiceBase = (LoggerServiceBase)Activator.CreateInstance(typeof(DatabaseLogger));
}
private void SendMail(object state)
{
var isAnyMailSended = false;
var logDetail = new LogDetail
{
UserID = "",
UserAgent = "",
IP = "System",
MethodName = "SENDMAILTASK",
StackTrace = "",
TraceIdentifier = ""
};
try
{
var now = DateTime.Now;
var mailTasks = _mailTaskDal.GetList(op => !op.IsSended && op.NextSendDate < now && op.TryCount < 10);
isAnyMailSended = mailTasks.Count > 0;
foreach (var mailTask in mailTasks)
{
var toList = mailTask.ToList.Split(',');
var sendResult = _mailAccountService.SendMail(toList, mailTask.Subject, mailTask.Body);
mailTask.Result = sendResult.Messages[0].Description;
if (sendResult.Success)
{
mailTask.IsSended = true;
mailTask.SendDate = DateTime.Now;
}
else
{
mailTask.NextSendDate = DateTime.Now.AddMinutes(5);
mailTask.TryCount++;
}
_mailTaskDal.Update(mailTask);
}
}
catch (Exception e)
{
logDetail.ExceptionMessage = e.Message;
logDetail.StackTrace = e.StackTrace;
}
finally
{
if (isAnyMailSended)
_loggerServiceBase.Info(logDetail);
}
}
public System.Threading.Tasks.Task StartAsync(CancellationToken cancellationToken)
{
_timer = new Timer(SendMail, null, TimeSpan.Zero, TimeSpan.FromSeconds(30));
return System.Threading.Tasks.Task.CompletedTask;
}
public System.Threading.Tasks.Task StopAsync(CancellationToken cancellationToken)
{
_timer?.Change(Timeout.Infinite, 0);
return System.Threading.Tasks.Task.CompletedTask;
}
public void Dispose()
{
_timer?.Dispose();
}
}
CreateActivityTask:
public class CreateActivityTask : IHostedService, IDisposable
{
private Timer _timer;
private LoggerServiceBase _loggerServiceBase;
public CreateActivityTask()
{
_loggerServiceBase = (LoggerServiceBase)Activator.CreateInstance(typeof(DatabaseLogger));
}
private void CreateActivity(object state)
{
var logDetail = new LogDetail
{
UserID = "",
UserAgent = "",
IP = "System",
MethodName = "CreateActivityTask",
StackTrace = "",
TraceIdentifier = ""
};
try
{
//create activity
}
catch (Exception e)
{
logDetail.ExceptionMessage = e.Message;
logDetail.StackTrace = e.StackTrace;
}
finally
{
_loggerServiceBase?.Info(logDetail);
}
}
public System.Threading.Tasks.Task StartAsync(CancellationToken cancellationToken)
{
_timer = new Timer(CreateActivity, null, TimeSpan.Zero, TimeSpan.FromSeconds(60));
return System.Threading.Tasks.Task.CompletedTask;
}
public System.Threading.Tasks.Task StopAsync(CancellationToken cancellationToken)
{
_timer?.Change(Timeout.Infinite, 0);
return System.Threading.Tasks.Task.CompletedTask;
}
public void Dispose()
{
_timer?.Dispose();
}
}
I Use two extension function to add this classes in startup.cs -> ConfigureServices:
services.AddHostedService<EMailSendService>();
services.AddHostedService<CreateActivityTask>();
But if i use like that, program does not run. When i start it, this code throws an error:
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run(); // This is where throws error
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
Error Detail:
System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=Microsoft.Extensions.Hosting
StackTrace:
at Microsoft.Extensions.Hosting.Internal.Host.<StartAsync>d__9.MoveNext()
If i comment
services.AddHostedService();
Still i get the error. But if i comment all "CreateActivityTask" class and "services.AddHostedService();", then i can run the app.
EmailTaskService can run properly. But if i left "CreateActivityTask" alone (I mean, i commented all "EmailTaskService class etc..), program gets error again.
I can't see any error on CreateActivity class, so i can't find where is the mistake. Could you help me please?
User contributions licensed under CC BY-SA 3.0