|
| 1 | +#nullable enable |
| 2 | + |
| 3 | +/** |
| 4 | + * The code in this file is not meant to be executed. It only shows idiomatic usages of NServiceBus |
| 5 | + * APIs, but with nullable reference types enabled, so that we can attempt to make sure that the |
| 6 | + * addition of nullable annotations in our public APIs doesn't create nullability warnings on |
| 7 | + * our own APIs under normal circumstances. It's sort of a mini-Snippets to provide faster |
| 8 | + * feedback than having to release an alpha package and check that Snippets in docs compile. |
| 9 | + */ |
| 10 | + |
| 11 | +namespace NServiceBus.Core.Tests.API.NullableApiUsages |
| 12 | +{ |
| 13 | + using System; |
| 14 | + using System.Threading; |
| 15 | + using System.Threading.Tasks; |
| 16 | + using NServiceBus.Extensibility; |
| 17 | + using NServiceBus.Logging; |
| 18 | + using NServiceBus.MessageMutator; |
| 19 | + using NServiceBus.Persistence; |
| 20 | + using NServiceBus.Pipeline; |
| 21 | + using NServiceBus.Sagas; |
| 22 | + |
| 23 | + public class TopLevelApis |
| 24 | + { |
| 25 | + public async Task SetupEndpoint(CancellationToken cancellationToken = default) |
| 26 | + { |
| 27 | + var cfg = new EndpointConfiguration("EndpointName"); |
| 28 | + |
| 29 | + cfg.Conventions() |
| 30 | + .DefiningCommandsAs(t => t.Namespace?.EndsWith(".Commands") ?? false) |
| 31 | + .DefiningEventsAs(t => t.Namespace?.EndsWith(".Events") ?? false) |
| 32 | + .DefiningMessagesAs(t => t.Namespace?.EndsWith(".Messages") ?? false); |
| 33 | + |
| 34 | + cfg.SendFailedMessagesTo("error"); |
| 35 | + |
| 36 | + var routing = cfg.UseTransport(new LearningTransport()); |
| 37 | + routing.RouteToEndpoint(typeof(Cmd), "Destination"); |
| 38 | + |
| 39 | + var persistence = cfg.UsePersistence<LearningPersistence>(); |
| 40 | + |
| 41 | + cfg.UseSerialization<SystemJsonSerializer>() |
| 42 | + .Options(new System.Text.Json.JsonSerializerOptions()); |
| 43 | + |
| 44 | + // Start directly |
| 45 | + await Endpoint.Start(cfg, cancellationToken); |
| 46 | + |
| 47 | + // Or create, then start |
| 48 | + var startable = await Endpoint.Create(cfg, cancellationToken); |
| 49 | + var ep = await startable.Start(cancellationToken); |
| 50 | + |
| 51 | + await ep.Send(new Cmd(), cancellationToken); |
| 52 | + await ep.Publish(new Evt(), cancellationToken); |
| 53 | + await ep.Publish<Evt>(cancellationToken); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public class TestHandler : IHandleMessages<Cmd> |
| 58 | + { |
| 59 | + ILog logger; |
| 60 | + |
| 61 | + public TestHandler(ILog logger) |
| 62 | + { |
| 63 | + this.logger = logger; |
| 64 | + } |
| 65 | + |
| 66 | + public async Task Handle(Cmd message, IMessageHandlerContext context) |
| 67 | + { |
| 68 | + logger.Info(message.OrderId); |
| 69 | + await context.Send(new Cmd()); |
| 70 | + await context.Publish(new Evt()); |
| 71 | + |
| 72 | + var opts = new SendOptions(); |
| 73 | + opts.DelayDeliveryWith(TimeSpan.FromSeconds(5)); |
| 74 | + opts.SetHeader("a", "1"); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public class TestSaga : Saga<TestSagaData>, |
| 79 | + IAmStartedByMessages<Cmd>, |
| 80 | + IHandleMessages<Evt>, |
| 81 | + IHandleTimeouts<TestTimeout> |
| 82 | + { |
| 83 | + protected override void ConfigureHowToFindSaga(SagaPropertyMapper<TestSagaData> mapper) |
| 84 | + { |
| 85 | + mapper.MapSaga(saga => saga.OrderId) |
| 86 | + .ToMessage<Cmd>(m => m.OrderId) |
| 87 | + .ToMessage<Evt>(m => m.OrderId) |
| 88 | + .ToMessageHeader<Cmd>("HeaderName"); |
| 89 | + } |
| 90 | + |
| 91 | + public async Task Handle(Cmd message, IMessageHandlerContext context) |
| 92 | + { |
| 93 | + await context.Send(new Cmd()); |
| 94 | + await context.Publish(new Evt()); |
| 95 | + Console.WriteLine(Data.OrderId); |
| 96 | + await RequestTimeout<TestTimeout>(context, TimeSpan.FromMinutes(1)); |
| 97 | + MarkAsComplete(); |
| 98 | + } |
| 99 | + |
| 100 | + public async Task Handle(Evt message, IMessageHandlerContext context) |
| 101 | + { |
| 102 | + await context.Send(new Cmd()); |
| 103 | + await context.Publish(new Evt()); |
| 104 | + await context.Publish<Evt>(); |
| 105 | + } |
| 106 | + |
| 107 | + public Task Timeout(TestTimeout state, IMessageHandlerContext context) |
| 108 | + { |
| 109 | + Console.WriteLine(state.TimeoutData); |
| 110 | + return Task.CompletedTask; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + public class TestSagaOldMapping : Saga<TestSagaData>, |
| 115 | + IAmStartedByMessages<Cmd> |
| 116 | + { |
| 117 | + protected override void ConfigureHowToFindSaga(SagaPropertyMapper<TestSagaData> mapper) |
| 118 | + { |
| 119 | + mapper.ConfigureMapping<Cmd>(m => m.OrderId).ToSaga(s => s.OrderId); |
| 120 | + mapper.ConfigureHeaderMapping<Cmd>("HeaderName"); |
| 121 | + } |
| 122 | + |
| 123 | + public Task Handle(Cmd message, IMessageHandlerContext context) => throw new NotImplementedException(); |
| 124 | + } |
| 125 | + |
| 126 | + public class Cmd : ICommand |
| 127 | + { |
| 128 | + public string? OrderId { get; set; } |
| 129 | + } |
| 130 | + |
| 131 | + public class Evt : IEvent |
| 132 | + { |
| 133 | + public string? OrderId { get; set; } |
| 134 | + } |
| 135 | + |
| 136 | + public class TestSagaData : ContainSagaData |
| 137 | + { |
| 138 | + public string? OrderId { get; set; } |
| 139 | + } |
| 140 | + |
| 141 | + public class TestTimeout |
| 142 | + { |
| 143 | + public string? TimeoutData { get; set; } |
| 144 | + } |
| 145 | + |
| 146 | + public class NotUsedSagaFinder : ISagaFinder<TestSagaData, Cmd> |
| 147 | + { |
| 148 | + public async Task<TestSagaData?> FindBy(Cmd message, ISynchronizedStorageSession storageSession, IReadOnlyContextBag context, CancellationToken cancellationToken = default) |
| 149 | + { |
| 150 | + // Super-gross, never do this |
| 151 | + await Task.Yield(); |
| 152 | + |
| 153 | + if (context.TryGet<TestSagaData>(out var result)) |
| 154 | + { |
| 155 | + return result; |
| 156 | + } |
| 157 | + |
| 158 | + return null; |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + public class TestBehavior : Behavior<IIncomingLogicalMessageContext> |
| 163 | + { |
| 164 | + public override async Task Invoke(IIncomingLogicalMessageContext context, Func<Task> next) |
| 165 | + { |
| 166 | + await Task.Delay(10); |
| 167 | + await next(); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + public class TestIncomingMutator : IMutateIncomingMessages |
| 172 | + { |
| 173 | + public Task MutateIncoming(MutateIncomingMessageContext context) => Task.CompletedTask; |
| 174 | + } |
| 175 | + |
| 176 | + public class TestIncomingTransportMutator : IMutateIncomingTransportMessages |
| 177 | + { |
| 178 | + public Task MutateIncoming(MutateIncomingTransportMessageContext context) => Task.CompletedTask; |
| 179 | + } |
| 180 | + |
| 181 | + public class TestOutgoingMutator : IMutateOutgoingMessages |
| 182 | + { |
| 183 | + public Task MutateOutgoing(MutateOutgoingMessageContext context) => Task.CompletedTask; |
| 184 | + } |
| 185 | + |
| 186 | + public class TestOutgoingTransportMutator : IMutateOutgoingTransportMessages |
| 187 | + { |
| 188 | + public Task MutateOutgoing(MutateOutgoingTransportMessageContext context) => Task.CompletedTask; |
| 189 | + } |
| 190 | +} |
0 commit comments