Search code examples
c#mongodb.net-corenuget-packagehangfire

How to use hangfire in .net core with mongodb?


I want to use Hangfire for background jobs in registration form process,But I am unable to find Startup.cs file code for Hangfire.mongo.


Solution

  • In Startup class In ConfigureServices method Add

    //you will use some way to get your connection string
    var mongoConnection = Configuration.GetConnectionString("MongoDBAtlasJaken");
    var migrationOptions = new MongoMigrationOptions
    {
        Strategy = MongoMigrationStrategy.Drop,
        BackupStrategy = MongoBackupStrategy.Collections
    };
    
    services.AddHangfire(config =>
    {
        config.SetDataCompatibilityLevel(CompatibilityLevel.Version_170);
        config.UseSimpleAssemblyNameTypeSerializer();
        config.UseRecommendedSerializerSettings();
        config.UseMongoStorage(mongoConnection, "Hangfire",new MongoStorageOptions {   MigrationOptions = migrationOptions });
    
    });
    services.AddHangfireServer();
    

    In Configure method you can add if you want

    app.UseHangfireDashboard();