Skip to content

Commit dfa521e

Browse files
committed
Add default implementation for IAsyncDisposable.DisposeAsync to ICompletableSynchronizedStorageSession and revert to also implement IDisposable to make this a non-breaking change.
1 parent 7e17f31 commit dfa521e

File tree

7 files changed

+48
-3
lines changed

7 files changed

+48
-3
lines changed

src/NServiceBus.AcceptanceTesting/AcceptanceTestingPersistence/AcceptanceTestingSynchronizedStorageSession.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ class AcceptanceTestingSynchronizedStorageSession : ICompletableSynchronizedStor
1313
{
1414
public AcceptanceTestingTransaction Transaction { get; private set; }
1515

16+
public void Dispose()
17+
{
18+
Transaction = null;
19+
}
20+
1621
public ValueTask DisposeAsync()
1722
{
1823
Transaction = null;

src/NServiceBus.Core.Tests/ApprovalFiles/APIApprovals.ApproveNServiceBus.approved.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1669,7 +1669,7 @@ namespace NServiceBus.Persistence
16691669
public static System.Threading.Tasks.ValueTask Open(this NServiceBus.Persistence.ICompletableSynchronizedStorageSession session, NServiceBus.Pipeline.IIncomingLogicalMessageContext context) { }
16701670
public static System.Threading.Tasks.ValueTask Open(this NServiceBus.Persistence.ICompletableSynchronizedStorageSession session, NServiceBus.Outbox.IOutboxTransaction outboxTransaction, NServiceBus.Transport.TransportTransaction transportTransaction, NServiceBus.Extensibility.ContextBag contextBag, System.Threading.CancellationToken cancellationToken = default) { }
16711671
}
1672-
public interface ICompletableSynchronizedStorageSession : NServiceBus.Persistence.ISynchronizedStorageSession, System.IAsyncDisposable
1672+
public interface ICompletableSynchronizedStorageSession : NServiceBus.Persistence.ISynchronizedStorageSession, System.IAsyncDisposable, System.IDisposable
16731673
{
16741674
System.Threading.Tasks.Task CompleteAsync(System.Threading.CancellationToken cancellationToken = default);
16751675
System.Threading.Tasks.Task Open(NServiceBus.Extensibility.ContextBag context, System.Threading.CancellationToken cancellationToken = default);

src/NServiceBus.Core.Tests/Fakes/FakeSynchronizedStorageSession.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ public Task Open(ContextBag context, CancellationToken cancellationToken = defau
5858
return Task.CompletedTask;
5959
}
6060

61+
public void Dispose()
62+
{
63+
Transaction = null;
64+
}
65+
6166
public ValueTask DisposeAsync()
6267
{
6368
Transaction = null;

src/NServiceBus.Core/Persistence/Learning/LearningSynchronizedStorageSession.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ namespace NServiceBus;
1313
[SkipWeaving]
1414
class LearningSynchronizedStorageSession : ICompletableSynchronizedStorageSession
1515
{
16+
public void Dispose()
17+
{
18+
foreach (var sagaFile in sagaFiles.Values)
19+
{
20+
sagaFile.Dispose();
21+
}
22+
23+
sagaFiles.Clear();
24+
}
25+
1626
public async ValueTask DisposeAsync()
1727
{
1828
foreach (var sagaFile in sagaFiles.Values)

src/NServiceBus.Core/Persistence/Learning/SagaPersister/SagaStorageFile.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,26 @@ namespace NServiceBus;
99
using Janitor;
1010

1111
[SkipWeaving]
12-
class SagaStorageFile : IAsyncDisposable
12+
class SagaStorageFile : IDisposable, IAsyncDisposable
1313
{
1414
SagaStorageFile(FileStream fileStream)
1515
{
1616
this.fileStream = fileStream;
1717
}
1818

19+
public void Dispose()
20+
{
21+
fileStream.Close();
22+
23+
if (isCompleted)
24+
{
25+
File.Delete(fileStream.Name);
26+
}
27+
28+
fileStream.Dispose(); // Already closed, but for completeness
29+
fileStream = null;
30+
}
31+
1932
public async ValueTask DisposeAsync()
2033
{
2134
fileStream.Close();

src/NServiceBus.Core/Reliability/SynchronizedStorage/ICompletableSynchronizedStorageSession.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/// <summary>
1111
/// Represents a storage session from point of view of the infrastructure.
1212
/// </summary>
13-
public interface ICompletableSynchronizedStorageSession : ISynchronizedStorageSession, IAsyncDisposable
13+
public interface ICompletableSynchronizedStorageSession : ISynchronizedStorageSession, IDisposable, IAsyncDisposable
1414
{
1515
/// <summary>
1616
/// Tries to open the storage session with the provided outbox transaction.
@@ -41,4 +41,12 @@ public interface ICompletableSynchronizedStorageSession : ISynchronizedStorageSe
4141
/// Completes the session by saving the changes.
4242
/// </summary>
4343
Task CompleteAsync(CancellationToken cancellationToken = default);
44+
45+
#pragma warning disable CA1816
46+
ValueTask IAsyncDisposable.DisposeAsync()
47+
#pragma warning restore CA1816
48+
{
49+
Dispose();
50+
return ValueTask.CompletedTask;
51+
}
4452
}

src/NServiceBus.Core/Reliability/SynchronizedStorage/NoOpCompletableSynchronizedStorageSession.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public Task Open(ContextBag context, CancellationToken cancellationToken = defau
2626

2727
public Task CompleteAsync(CancellationToken cancellationToken = default) => Task.CompletedTask;
2828

29+
public void Dispose()
30+
{
31+
}
32+
2933
public ValueTask DisposeAsync() => ValueTask.CompletedTask;
3034

3135
public static readonly ICompletableSynchronizedStorageSession Instance = new NoOpCompletableSynchronizedStorageSession();

0 commit comments

Comments
 (0)