選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ICapPublisher.MongoDB.cs 1.7 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright (c) .NET Core Community. All rights reserved.
  2. // Licensed under the MIT License. See License.txt in the project root for license information.
  3. using System;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using DotNetCore.CAP.Abstractions;
  7. using DotNetCore.CAP.Models;
  8. using Microsoft.Extensions.DependencyInjection;
  9. using MongoDB.Driver;
  10. namespace DotNetCore.CAP.MongoDB
  11. {
  12. public class MongoDBPublisher : CapPublisherBase, ICallbackPublisher
  13. {
  14. private readonly IMongoClient _client;
  15. private readonly MongoDBOptions _options;
  16. public MongoDBPublisher(IServiceProvider provider, MongoDBOptions options)
  17. : base(provider)
  18. {
  19. _options = options;
  20. _client = ServiceProvider.GetRequiredService<IMongoClient>();
  21. }
  22. public async Task PublishCallbackAsync(CapPublishedMessage message)
  23. {
  24. await PublishAsyncInternal(message);
  25. }
  26. protected override Task ExecuteAsync(CapPublishedMessage message, ICapTransaction transaction,
  27. CancellationToken cancel = default(CancellationToken))
  28. {
  29. var insertOptions = new InsertOneOptions {BypassDocumentValidation = false};
  30. var collection = _client
  31. .GetDatabase(_options.DatabaseName)
  32. .GetCollection<CapPublishedMessage>(_options.PublishedCollection);
  33. if (NotUseTransaction)
  34. {
  35. return collection.InsertOneAsync(message, insertOptions, cancel);
  36. }
  37. var dbTrans = (IClientSessionHandle) transaction.DbTransaction;
  38. return collection.InsertOneAsync(dbTrans, message, insertOptions, cancel);
  39. }
  40. }
  41. }