【问题标题】:How to use hangfire in .net core with mongodb?如何在 .net 核心和 mongodb 中使用 hangfire?
【发布时间】:2019-10-11 11:23:22
【问题描述】:

我想在注册表单过程中将Hangfire用于后台作业,但是我找不到Hangfire.mongo的Startup.cs文件代码。

【问题讨论】:

标签: c# mongodb .net-core nuget-package hangfire


【解决方案1】:

只是对这个帖子的更新,

从 v0.7.11 开始,MongoMigrationOptions 已更新,不再包含策略。根据release notes,您现在必须使用MigrationStrategy 而不是策略。此外,您用于这些的值也不同。请参见下面的示例。

var migrationOptions = new MongoMigrationOptions
{
    MigrationStrategy = new MigrateMongoMigrationStrategy(),
    BackupStrategy = new CollectionMongoBackupStrategy()
};

【讨论】:

    【解决方案2】:

    在启动类中 在 ConfigureServices 方法中 添加

    //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();
    

    如果需要,可以在配置方法中添加

    app.UseHangfireDashboard();
    

    【讨论】:

      【解决方案3】:

      @Graeme 的 anwser 适用于旧版本的 Hangfire。对于新版本,

      1. MigrationBackup 策略从枚举更改为类。

         var migrationOptions = new MongoMigrationOptions
         {
             MigrationStrategy = new DropMongoMigrationStrategy(),
             BackupStrategy = new CollectionMongoBackupStrategy()
         };
        
      2. UseMongoStorage 方法不再接受集合名称。

         services.AddHangfire(config =>
         {
             config.SetDataCompatibilityLevel(CompatibilityLevel.Version_170);
             config.UseSimpleAssemblyNameTypeSerializer();
             config.UseRecommendedSerializerSettings();
             config.UseMongoStorage(mongoConnection, new MongoStorageOptions { MigrationOptions = migrationOptions, CheckConnection = false });
         });
        

        services.AddHangfireServer();

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-09
        • 1970-01-01
        相关资源
        最近更新 更多