当前位置:首页 >时尚 >让 Hangfire 使用 MongoDB 存储 存储通过使用这个库

让 Hangfire 使用 MongoDB 存储 存储通过使用这个库

2024-06-30 17:35:04 [百科] 来源:避面尹邢网

让 Hangfire 使用 MongoDB 存储

作者:conan 存储 存储软件 MongoDB MongoDB 支持Hangfire库。存储通过使用这个库,存储您可以将所有作业信息存储在 MongoDB 中。存储要安装 Hangfire MongoDB Storage,存储请在 Nuget 包管理器控制台中运行以下命令。存储

[[413350]]

本文转载自微信公众号「后端Q  」,存储作者conan 。存储转载本文请联系后端Q  公众号。存储

让 Hangfire 使用 MongoDB 存储 存储通过使用这个库

MongoDB 支持Hangfire库。存储通过使用这个库,存储您可以将所有作业信息存储在 MongoDB 中。存储

让 Hangfire 使用 MongoDB 存储 存储通过使用这个库

要安装 Hangfire MongoDB Storage,存储请在 Nuget 包管理器控制台中运行以下命令:

让 Hangfire 使用 MongoDB 存储 存储通过使用这个库

  1. PM> Install-Package Hangfire.Mongo 
  1. public void ConfigureServices(IServiceCollection services) 
  2. {  
  3.     var mongoUrlBuilder = new MongoUrlBuilder("mongodb://localhost/jobs"); 
  4.     var mongoClient = new MongoClient(mongoUrlBuilder.ToMongoUrl()); 
  5.  
  6.     // Add Hangfire services. Hangfire.AspNetCore nuget required 
  7.     services.AddHangfire(configuration => configuration 
  8.         .SetDataCompatibilityLevel(CompatibilityLevel.Version_170) 
  9.         .UseSimpleAssemblyNameTypeSerializer() 
  10.         .UseRecommendedSerializerSettings() 
  11.         .UseMongoStorage(mongoClient,存储 mongoUrlBuilder.DatabaseName, new MongoStorageOptions 
  12.         {  
  13.             MigrationOptions = new MongoMigrationOptions 
  14.             {  
  15.                 MigrationStrategy = new MigrateMongoMigrationStrategy(), 
  16.                 BackupStrategy = new CollectionMongoBackupStrategy() 
  17.             }, 
  18.             Prefix = "hangfire.mongo", 
  19.             CheckConnection = true 
  20.         }) 
  21.     ); 
  22.     // Add the processing server as IHostedService 
  23.     services.AddHangfireServer(serverOptions => 
  24.     {  
  25.         serverOptions.ServerName = "Hangfire.Mongo server 1"; 
  26.     }); 
  27.  
  28.     // Add framework services. 
  1. var options = new MongoStorageOptions 
  2. {  
  3.     MigrationOptions = new MongoMigrationOptions 
  4.     {  
  5.         MigrationStrategy = new DropMongoMigrationStrategy(), 
  6.         BackupStrategy = new NoneMongoBackupStrategy() 
  7.     } 
  8. }; 
  9. GlobalConfiguration.Configuration.UseMongoStorage("mongodb://localhost/jobs", options); 
  10. app.UseHangfireServer(); 
  11. app.UseHangfireDashboard(); 

Throw

这是默认的迁移策略。如果架构版本不匹配,存储它将抛出InvalidOperationException,存储永远不会让您启动并运行。因此,它迫使您决定什么迁移策略最适合您,同时确保数据的安全。

Drop

这只需删除现有的Hangfire.Mongo数据库并更新模式版本。没有绒毛,准备从头开始。这是一个完美的策略,如果你在创业时把所有的工作都安排好。

Migrate

这将把数据库从一个模式版本迁移到下一个模式版本,直到达到所需的模式版本。并不是所有的数据都可以迁移,这就是为什么会发生一些数据丢失的原因。在投入生产前,请谨慎使用并仔细测试。我们不对数据丢失负责。

注意:只支持前向迁移。如果需要还原到以前的架构版本,则需要手动删除或还原以前的数据库。

https://github.com/sergeyzwezdin/Hangfire.Mongo

 

责任编辑:武晓燕 来源: 后端Q MongoDB存储Hangfire

(责任编辑:焦点)

    推荐文章
    热点阅读