Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asp.Net Core 2.1 Exceptions on application start #1576

Closed
Sonic198 opened this issue Nov 30, 2019 · 3 comments
Closed

Asp.Net Core 2.1 Exceptions on application start #1576

Sonic198 opened this issue Nov 30, 2019 · 3 comments

Comments

@Sonic198
Copy link

Hi,
When I starting my Asp.Net Core 2.1 application I'm always getting three exceptions but app is still running and looks like everything is working. Why that exceptions are showing up?
image
I'm using following packages:

Here is my config:

    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors(options => options.AddPolicy("AllowAll", p => p.SetIsOriginAllowed(isOriginAllowed: _ => true)
                .AllowAnyMethod()
                .AllowAnyHeader()
                .AllowCredentials()));

             ...

            services.AddHangfire(config => 
            {
                config.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
                    .UseSimpleAssemblyNameTypeSerializer()
                    .UseRecommendedSerializerSettings();
            });
            services.AddHangfireServer(options => { options.WorkerCount = 1; });
            services.AddScoped<ICacheUserRolesJob, CacheUserRolesJob>();

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
                .AddJsonOptions(opt => opt.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore);
            
             ...
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env,
            IOptions<ConnectionStrings> connectionStrings, IOptions<AppSettings> appSettings, ICacheUserRolesJob cacheUserRolesJob)
        {
            app.UseCors("AllowAll");

            ...

            app.UseAuthentication();
            app.UseHttpsRedirection();
                        
            GlobalConfiguration.Configuration.UseMongoStorage(connectionStrings.Value.MongoDB, appSettings.Value.MongoDBName,
                new MongoStorageOptions { 
                    Prefix = "platform.hangfire",
                    MigrationOptions = new MongoMigrationOptions
                    {
                        Strategy = MongoMigrationStrategy.Drop,
                        BackupStrategy = MongoBackupStrategy.None
                    }
                });
            GlobalJobFilters.Filters.Add(new AutomaticRetryAttribute { Attempts = 1 });

            app.UseHangfireDashboard();

            app.UseMvc();
            app.UseStaticFiles();

            HangfireJobScheduler.ScheduleRecurringJobs(appSettings.Value);
            RecurringJob.Trigger(nameof(CacheUserRolesJob));
        }
    }


    public class HangfireJobScheduler
    {
        public static void ScheduleRecurringJobs(AppSettings appSettings)
        {
            RecurringJob.RemoveIfExists(nameof(CacheUserRolesJob));
            RecurringJob.AddOrUpdate<CacheUserRolesJob>(nameof(CacheUserRolesJob),
                job => job.Run(JobCancellationToken.Null), appSettings.BackgroudJobs.CacheUserRolesCron, TimeZoneInfo.Local);
        }
    }


    public class CacheUserRolesJob : ICacheUserRolesJob
    {
        private readonly ILogger<CacheUserRolesJob> _logger;

        public CacheUserRolesJob(ILogger<CacheUserRolesJob> logger)
        {
            _logger = logger;
        }

        public async Task Run(IJobCancellationToken token)
        {
            token.ThrowIfCancellationRequested();
            await RunAtTimeOf(DateTime.Now);
        }

        public async Task RunAtTimeOf(DateTime now)
        {
            _logger.LogInformation("Cache User Roles Job Starts...");
            await Task.Delay(10000);
            _logger.LogInformation("Cache User Roles Job Completed.");
        }
    }
@joro550
Copy link

joro550 commented Dec 6, 2019

Looking at the code in the Hangfire Core library it looks like they throw these exceptions in a number of circumstances and catch them, I'd say if you're Hangfire server is running and is completing jobs then these exceptions are to be expected from the framwork code and can be ignored.

@Sonic198
Copy link
Author

Sonic198 commented Dec 9, 2019

For some reason 'run without debugging' option is not working for me in VS. I don't know if that can be because of that errors.
I was able to debug them and here is what I found:
System.InvalidOperationException is throwed in this line of code

RecurringJob.AddOrUpdate<CacheUserRolesJob>(nameof(CacheUserRolesJob),
                job => job.Run(JobCancellationToken.Null), appSettings.BackgroudJobs.CacheUserRolesCron, TimeZoneInfo.Local);

Error message & stack trace

The 'Job' field has a null or empty value
   at Hangfire.RecurringJobEntity..ctor(String recurringJobId, IDictionary`2 recurringJob, ITimeZoneResolver timeZoneResolver, DateTime now)

System.NotSupportedException x 2
Error message & stack trace

Specified method is not supported.
   at Hangfire.Storage.JobStorageConnection.GetFirstByLowestScoreFromSet(String key, Double fromScore, Double toScore, Int32 count)

System.NotSupportedException is throwed event I comment out above line of code.

@odinserj
Copy link
Member

That's not even a problem, just ignore those exceptions or tell Visual Studio to not to break on them. Hangfire handles a lot of exceptions, and may generate tons of OperationCanceledException during shutdown that tells all the background processes that shutdown was requested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants