-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding facade to enable session processing (#1396)
* Adding facade to enable session processing * fixes
- Loading branch information
Showing
3 changed files
with
142 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
src/DotNetCore.CAP.AzureServiceBus/ServiceBusProcessorFacade.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright (c) .NET Core Community. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Azure.Messaging.ServiceBus; | ||
|
||
namespace DotNetCore.CAP.AzureServiceBus; | ||
|
||
public class ServiceBusProcessorFacade : IAsyncDisposable | ||
{ | ||
private readonly ServiceBusProcessor? _serviceBusProcessor; | ||
private readonly ServiceBusSessionProcessor? _serviceBusSessionProcessor; | ||
|
||
public bool IsSessionProcessor { get; } | ||
|
||
public bool IsProcessing => IsSessionProcessor | ||
? _serviceBusSessionProcessor!.IsProcessing | ||
: _serviceBusProcessor!.IsProcessing; | ||
|
||
public bool AutoCompleteMessages => IsSessionProcessor | ||
? _serviceBusSessionProcessor!.AutoCompleteMessages | ||
: _serviceBusProcessor!.AutoCompleteMessages; | ||
|
||
public ServiceBusProcessorFacade(ServiceBusProcessor? serviceBusProcessor = null, | ||
ServiceBusSessionProcessor? serviceBusSessionProcessor = null) | ||
{ | ||
if (serviceBusProcessor is null && serviceBusSessionProcessor is null) | ||
{ | ||
throw new ArgumentNullException(nameof(serviceBusProcessor), | ||
"Either serviceBusProcessor or serviceBusSessionProcessor must be provided"); | ||
} | ||
|
||
_serviceBusProcessor = serviceBusProcessor; | ||
_serviceBusSessionProcessor = serviceBusSessionProcessor; | ||
|
||
IsSessionProcessor = _serviceBusSessionProcessor is not null; | ||
} | ||
|
||
public Task StartProcessingAsync(CancellationToken cancellationToken = default) | ||
{ | ||
return IsSessionProcessor | ||
? _serviceBusSessionProcessor!.StartProcessingAsync(cancellationToken) | ||
: _serviceBusProcessor!.StartProcessingAsync(cancellationToken); | ||
} | ||
|
||
public event Func<ProcessMessageEventArgs, Task> ProcessMessageAsync | ||
{ | ||
add => _serviceBusProcessor!.ProcessMessageAsync += value; | ||
|
||
remove => _serviceBusProcessor!.ProcessMessageAsync -= value; | ||
} | ||
|
||
public event Func<ProcessSessionMessageEventArgs, Task> ProcessSessionMessageAsync | ||
{ | ||
add => _serviceBusSessionProcessor!.ProcessMessageAsync += value; | ||
|
||
remove => _serviceBusSessionProcessor!.ProcessMessageAsync -= value; | ||
} | ||
|
||
public event Func<ProcessErrorEventArgs, Task> ProcessErrorAsync | ||
{ | ||
add | ||
{ | ||
if (IsSessionProcessor) | ||
{ | ||
_serviceBusSessionProcessor!.ProcessErrorAsync += value; | ||
} | ||
else | ||
{ | ||
_serviceBusProcessor!.ProcessErrorAsync += value; | ||
} | ||
} | ||
|
||
remove | ||
{ | ||
if (IsSessionProcessor) | ||
{ | ||
_serviceBusSessionProcessor!.ProcessErrorAsync -= value; | ||
} | ||
else | ||
{ | ||
_serviceBusProcessor!.ProcessErrorAsync -= value; | ||
} | ||
} | ||
} | ||
|
||
|
||
public async ValueTask DisposeAsync() | ||
{ | ||
if (_serviceBusProcessor is not null) await _serviceBusProcessor.DisposeAsync(); | ||
if (_serviceBusSessionProcessor is not null) await _serviceBusSessionProcessor.DisposeAsync(); | ||
} | ||
} |