hi everyone I’m trying to get the hangfire server to work, sometimes it works if I launch it in local m, then even if I disconnect the local it continues to work in production at other times it gives me this message:
System.InvalidOperationException: Recurring job can't be scheduled, see inner exception for details. ---> Hangfire.Common.JobLoadException: Could not load the job. See inner exception for the details. ---> System.IO.FileNotFoundException: Could not load file or assembly 'CityCare.Server, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.
at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, IntPtr ptrLoadContextBinder)
at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, IntPtr ptrLoadContextBinder)
at System.Reflection.Assembly.Load(AssemblyName assemblyRef)
at Hangfire.Common.TypeHelper.AssemblyResolver(String assemblyString)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at Hangfire.Common.TypeHelper.CachedAssemblyResolver(AssemblyName assemblyName)
at System.TypeNameParser.ResolveAssembly(String asmName, Func`2 assemblyResolver, Boolean throwOnError, StackCrawlMark& stackMark)
at System.TypeNameParser.ConstructType(Func`2 assemblyResolver, Func`4 typeResolver, Boolean throwOnError, Boolean ignoreCase, StackCrawlMark& stackMark)
at System.TypeNameParser.GetType(String typeName, Func`2 assemblyResolver, Func`4 typeResolver, Boolean throwOnError, Boolean ignoreCase, StackCrawlMark& stackMark)
at System.Type.GetType(String typeName, Func`2 assemblyResolver, Func`4 typeResolver, Boolean throwOnError)
at Hangfire.Common.TypeHelper.DefaultTypeResolver(String typeName)
at Hangfire.Storage.InvocationData.DeserializeJob()
--- End of inner exception stack trace ---
at Hangfire.Storage.InvocationData.DeserializeJob()
at Hangfire.RecurringJobEntity..ctor(String recurringJobId, IDictionary`2 recurringJob, ITimeZoneResolver timeZoneResolver, DateTime now)
--- End of inner exception stack trace ---
at Hangfire.Server.RecurringJobScheduler.ScheduleRecurringJob(BackgroundProcessContext context, IStorageConnection connection, String recurringJobId, RecurringJobEntity recurringJob, DateTime now)
even if it gives me this message and I trigger now from the dashboard, it works for me, but it does not automatically because it gives me that error.
i am using it in a blazor web assembly application.
this is my code for add recurringjob:
private async Task SetJobs(ComuniImpostazioni model)
{
try
{
if (model.ControllaNotifiche)
{
ApplicationUser user = null;
if (User != null && !string.IsNullOrEmpty(User.Identity.Name))
{
user = await _userManager.FindByNameAsync(User.Identity.Name.ToLower());
}
var idComune = user != null ? user.IdComune : _comuniProvider.GetComuni();
var idJob = $"Notifiche_{idComune}";
RecurringJob.AddOrUpdate<NotificheService>(idJob, ms => ms.LeggiNotifiche(model, idComune), $"*/{model.TempoControllo} 7-21 * * 1-6");
model.IdJob = idJob;
}
else
{
RecurringJob.RemoveIfExists(model.IdJob);
model.IdJob = null;
model.TempoControllo = 0;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return;
}
}
and this is my service:
public class NotificheService : INotificheService
{
private readonly ApplicationDBContext _context;
public NotificheService(ApplicationDBContext context)
{
_context = context;
}
public async Task LeggiNotifiche( ComuniImpostazioni impostazioniComuni, Guid _idComune)
{
try
{
if (!string.IsNullOrEmpty(impostazioniComuni.UrlNotifiche))
{
var client = new HttpClient();
List<string> listaUrl = impostazioniComuni.UrlNotifiche.Split(';').ToList();
foreach (var itemUrl in listaUrl)
{
var httpResponse = await client.GetAsync($"{itemUrl}");
if (httpResponse.StatusCode == System.Net.HttpStatusCode.OK)
{
var contentStream = await httpResponse.Content.ReadAsStreamAsync();
try
{
var objcetDEseralized = await System.Text.Json.JsonSerializer.DeserializeAsync<List<AvvisiDTO>>(contentStream, new System.Text.Json.JsonSerializerOptions { IgnoreNullValues = true, PropertyNameCaseInsensitive = true });
if (objcetDEseralized != null)
{
var listaStoricoNotifiche = _context.StoricoNotifiche.IgnoreQueryFilters().Where(x => x.date.Date >= DateTime.Now.Date && x.IdComune == _idComune).Select(s => s.IdNotifica).ToList();
var notificheDiOggi = objcetDEseralized.Where(x => x.date.Date >= DateTime.Now.Date && !listaStoricoNotifiche.Contains(x.id)).ToList();//
if (notificheDiOggi != null && notificheDiOggi.Count != 0)
{
foreach (var item in notificheDiOggi)
{
SendNotifica(impostazioniComuni, item.title.rendered, item.date);
await SalvaStoricoNotifica(_context, item, _idComune);
}
}
else
{
//return;
//test
//SendNotifica(impostazioniComuni, objcetDEseralized.Last().title.rendered, objcetDEseralized.Last().date);
}
}
}
catch (JsonException) // Invalid JSON
{
Console.WriteLine("Invalid JSON.");
return;
}
}
else
{
return;
}
}
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
private async Task SalvaStoricoNotifica(ApplicationDBContext context, AvvisiDTO item, Guid _idComune)
{
try
{
var notificaStoricoModel = new StoricoNotifiche();
notificaStoricoModel.Id = Guid.NewGuid();
notificaStoricoModel.DataCreazione = DateTime.Now;
notificaStoricoModel.DataModifica = DateTime.Now;
notificaStoricoModel.IdComune = _idComune;
notificaStoricoModel.IdNotifica = item.id;
notificaStoricoModel.Title = item.title.rendered;
context.StoricoNotifiche.Add(notificaStoricoModel);
await context.SaveChangesAsync();
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}