Created
June 19, 2018 13:00
-
-
Save Rensvind/c33b4a9314c6a4bb90f49a19993e3ee0 to your computer and use it in GitHub Desktop.
NServiceBus Seilog
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
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>netcoreapp2.1</TargetFramework> | |
</PropertyGroup> | |
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> | |
<LangVersion>latest</LangVersion> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.0" /> | |
<PackageReference Include="NServiceBus" Version="7.0.1" /> | |
<PackageReference Include="NServiceBus.Newtonsoft.Json" Version="2.1.0" /> | |
<PackageReference Include="NServiceBus.Persistence.Sql" Version="4.1.1" /> | |
<PackageReference Include="NServiceBus.Persistence.Sql.MsBuild" Version="4.1.1" /> | |
<PackageReference Include="NServiceBus.Serilog" Version="4.0.0" /> | |
<PackageReference Include="NServiceBus.Serilog.Tracing" Version="4.0.0" /> | |
<PackageReference Include="NServiceBus.SqlServer" Version="4.0.0" /> | |
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" /> | |
</ItemGroup> | |
</Project> |
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
using System; | |
using System.Data.SqlClient; | |
using System.Threading.Tasks; | |
using NServiceBus; | |
using NServiceBus.Persistence.Sql; | |
using NServiceBus.Serilog; | |
using NServiceBus.Serilog.Tracing; | |
using NServiceBus.Transport.SQLServer; | |
using Serilog; | |
namespace ConsoleApp5 | |
{ | |
class Program | |
{ | |
static async Task Main(string[] args) | |
{ | |
var connectionString = ""; | |
Log.Logger = new LoggerConfiguration() | |
.WriteTo.Console() | |
.CreateLogger(); | |
var logManager = NServiceBus.Logging.LogManager.Use<SerilogFactory>(); | |
logManager.WithLogger(Log.Logger); | |
var endpointConfiguration = Configure(connectionString); | |
var endpointInstance = await Endpoint.Start(endpointConfiguration) | |
.ConfigureAwait(false); | |
try | |
{ | |
while (Console.ReadLine() != "quit") | |
{ | |
await endpointInstance.SendLocal(new StartSaga1 {Id = Guid.NewGuid()}); | |
await endpointInstance.SendLocal(new StartSaga2 {Id = Guid.NewGuid()}); | |
} | |
} | |
finally | |
{ | |
await endpointInstance.Stop() | |
.ConfigureAwait(false); | |
} | |
} | |
private static EndpointConfiguration Configure(string connectionString) | |
{ | |
// General endpoint stuff | |
var config = new EndpointConfiguration("test"); | |
config.EnableInstallers(); | |
config.SendFailedMessagesTo("error"); | |
config.EnableFeature<TracingLog>(); | |
config.SerilogTracingTarget(Log.Logger); | |
// Transport | |
var transport = config.UseTransport<SqlServerTransport>(); | |
transport.DefaultSchema("NServiceBus"); | |
transport.Transactions(TransportTransactionMode.SendsAtomicWithReceive); | |
transport.ConnectionString(connectionString); | |
var delayedDeliverySettings = transport.UseNativeDelayedDelivery(); | |
delayedDeliverySettings.TableSuffix("Delayed"); | |
delayedDeliverySettings.DisableTimeoutManagerCompatibility(); | |
// Persistence | |
var persistence = config.UsePersistence<SqlPersistence>(); | |
persistence.SqlDialect<SqlDialect.MsSqlServer>().Schema("NServiceBus"); | |
persistence.ConnectionBuilder( | |
connectionBuilder: () => new SqlConnection(connectionString)); | |
// Subscriptions | |
var subscriptions = persistence.SubscriptionSettings(); | |
subscriptions.CacheFor(TimeSpan.FromMinutes(1)); | |
return config; | |
} | |
} | |
public class StartSaga1 : ICommand | |
{ | |
public Guid Id { get; set; } | |
} | |
public class StartSaga2 : ICommand | |
{ | |
public Guid Id { get; set; } | |
} | |
public class SendMessage : IMessage | |
{ | |
public Guid MyId { get; set; } | |
} | |
public class ReplyMessage : IMessage | |
{ | |
public Guid MyId { get; set; } | |
} | |
public class Timeout1 | |
{ | |
} | |
public class Timeout2 | |
{ | |
} | |
public class Saga1 : SqlSaga<Saga1.SagaData>, | |
IAmStartedByMessages<StartSaga1>, | |
IHandleMessages<ReplyMessage>, | |
IHandleTimeouts<Timeout1> | |
{ | |
public class SagaData : ContainSagaData | |
{ | |
public Guid MyId { get; set; } | |
public bool Test { get; set; } | |
} | |
protected override void ConfigureMapping(IMessagePropertyMapper mapper) | |
{ | |
mapper.ConfigureMapping<StartSaga1>(_ => _.Id); | |
} | |
protected override string CorrelationPropertyName => nameof(SagaData.MyId); | |
public async Task Handle(StartSaga1 message, IMessageHandlerContext context) | |
{ | |
Data.MyId = message.Id; | |
Data.Test = true; | |
await context.SendLocal(new SendMessage | |
{ | |
MyId = message.Id | |
}); | |
} | |
public async Task Handle(ReplyMessage message, IMessageHandlerContext context) | |
{ | |
Console.WriteLine($"{nameof(Saga1)}: Got a reply"); | |
Data.Test = false; | |
await RequestTimeout<Timeout1>(context, TimeSpan.FromSeconds(3)); | |
} | |
public Task Timeout(Timeout1 state, IMessageHandlerContext context) | |
{ | |
MarkAsComplete(); | |
return Task.CompletedTask; | |
} | |
} | |
public class Saga2 : SqlSaga<Saga2.SagaData>, | |
IAmStartedByMessages<StartSaga2>, | |
IHandleMessages<ReplyMessage>, | |
IHandleTimeouts<Timeout2> | |
{ | |
public class SagaData : ContainSagaData | |
{ | |
public Guid MyId { get; set; } | |
public bool Test { get; set; } | |
} | |
protected override void ConfigureMapping(IMessagePropertyMapper mapper) | |
{ | |
mapper.ConfigureMapping<StartSaga2>(_ => _.Id); | |
} | |
protected override string CorrelationPropertyName => nameof(SagaData.MyId); | |
public async Task Handle(StartSaga2 message, IMessageHandlerContext context) | |
{ | |
Data.MyId = message.Id; | |
Data.Test = true; | |
await context.SendLocal(new SendMessage()); | |
} | |
public async Task Handle(ReplyMessage message, IMessageHandlerContext context) | |
{ | |
Console.WriteLine($"{nameof(Saga2)}: Got a reply"); | |
Data.Test = false; | |
await RequestTimeout<Timeout2>(context, TimeSpan.FromSeconds(3)); | |
} | |
public Task Timeout(Timeout2 state, IMessageHandlerContext context) | |
{ | |
MarkAsComplete(); | |
return Task.CompletedTask; | |
} | |
} | |
public class SendMessageHandler : IHandleMessages<SendMessage> | |
{ | |
public async Task Handle(SendMessage message, IMessageHandlerContext context) | |
{ | |
await context.Reply(new ReplyMessage | |
{ | |
MyId = message.MyId | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment