Skip to content

Commit c905a0c

Browse files
boblangleydanielmarbach
authored andcommitted
Merge pull request #87 from Particular/storage-session-bug
Make container information non-mandatory for handlers that don't require storage access
1 parent d33d06a commit c905a0c

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

src/NServiceBus.Persistence.CosmosDB/SynchronizedStorage/StorageSession.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ public void AddOperation(Operation operation)
4646

4747
public async Task Commit()
4848
{
49+
// in case there is nothing to do don't even bother checking the rest
50+
if (operations.Count == 0)
51+
{
52+
return;
53+
}
54+
4955
if (ContainerHolder == null)
5056
{
5157
throw new Exception("Unable to retrieve the container name and the partition key during processing. Make sure that either `persistence.Container()` is used or the relevant container information is available on the message handling pipeline.");
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
namespace NServiceBus.AcceptanceTests
2+
{
3+
using System.Threading.Tasks;
4+
using System;
5+
using Persistence.CosmosDB;
6+
using Pipeline;
7+
using AcceptanceTesting;
8+
using EndpointTemplates;
9+
using NUnit.Framework;
10+
11+
public class When_regular_handler_with_no_container_information : NServiceBusAcceptanceTest
12+
{
13+
[Test]
14+
public async Task Should_work()
15+
{
16+
var context = await Scenario.Define<Context>()
17+
.WithEndpoint<EndpointWithRegularHandler>(b => b.When(session => session.SendLocal(new MyMessage())))
18+
.Done(c => c.MessageReceived)
19+
.Run();
20+
21+
Assert.True(context.MessageReceived);
22+
}
23+
24+
public class Context : ScenarioContext
25+
{
26+
public bool MessageReceived { get; set; }
27+
}
28+
29+
public class EndpointWithRegularHandler : EndpointConfigurationBuilder
30+
{
31+
public EndpointWithRegularHandler()
32+
{
33+
EndpointSetup<DefaultServer>(config =>
34+
{
35+
config.Pipeline.Register(new ContainerInformationRemoverBehavior.Registration());
36+
});
37+
}
38+
39+
class ContainerInformationRemoverBehavior : Behavior<IIncomingLogicalMessageContext>
40+
{
41+
public override Task Invoke(IIncomingLogicalMessageContext context, Func<Task> next)
42+
{
43+
context.Extensions.Remove<ContainerInformation>();
44+
return next();
45+
}
46+
47+
public class Registration : RegisterStep
48+
{
49+
public Registration() : base(nameof(ContainerInformationRemoverBehavior),
50+
typeof(ContainerInformationRemoverBehavior),
51+
"Removes the container information if present",
52+
b => new ContainerInformationRemoverBehavior())
53+
{
54+
InsertBeforeIfExists(nameof(LogicalOutboxBehavior));
55+
}
56+
}
57+
}
58+
59+
public class AHandler : IHandleMessages<MyMessage>
60+
{
61+
public AHandler(Context testContext)
62+
{
63+
this.testContext = testContext;
64+
}
65+
66+
public Task Handle(MyMessage message, IMessageHandlerContext context)
67+
{
68+
testContext.MessageReceived = true;
69+
return Task.CompletedTask;
70+
}
71+
72+
readonly Context testContext;
73+
}
74+
}
75+
76+
public class MyMessage : ICommand
77+
{
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)