Created
May 21, 2018 12:19
-
-
Save SzymonPobiega/49e3d1b340755dee8edbfb2dc93725c1 to your computer and use it in GitHub Desktop.
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; | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Start().GetAwaiter().GetResult(); | |
} | |
static async Task Start() | |
{ | |
var config = new EndpointConfiguration("SeparateConnections"); | |
config.EnableInstallers(); | |
config.SendFailedMessagesTo("error"); | |
var persistence = config.UsePersistence<SqlPersistence>(); | |
persistence.SqlDialect<SqlDialect.MsSqlServer>(); | |
persistence.ConnectionBuilder(() => new SqlConnection("data source=.\\sqlexpress; integrated security=true; initial catalog=nservicebus")); | |
var subscriptions = persistence.SubscriptionSettings(); | |
subscriptions.DisableCache(); | |
subscriptions.ConnectionBuilder(() => new SqlConnection("data source=.\\sqlexpress; integrated security=true; initial catalog=nservicebus1")); | |
var timeouts = persistence.TimeoutSettings(); | |
timeouts.ConnectionBuilder(() => new SqlConnection("data source=.\\sqlexpress; integrated security=true; initial catalog=nservicebus2")); | |
var routing = config.UseTransport<MsmqTransport>().Routing(); | |
routing.RegisterPublisher(typeof(MyEvent), "SeparateConnections"); | |
var endpoint = await Endpoint.Start(config); | |
Console.WriteLine("Press <enter> to send a message"); | |
while (true) | |
{ | |
Console.ReadLine(); | |
var sendOptions = new SendOptions(); | |
sendOptions.DelayDeliveryWith(TimeSpan.FromSeconds(5)); | |
sendOptions.RouteToThisEndpoint(); | |
await endpoint.Send(new MyMessage(), sendOptions); | |
} | |
} | |
} | |
class MyMessageHandler : IHandleMessages<MyMessage> | |
{ | |
public Task Handle(MyMessage message, IMessageHandlerContext context) | |
{ | |
return context.Publish(new MyEvent()); | |
} | |
} | |
class MyEventHandler : IHandleMessages<MyEvent> | |
{ | |
public Task Handle(MyEvent message, IMessageHandlerContext context) | |
{ | |
Console.WriteLine("Got event"); | |
return Task.CompletedTask; | |
} | |
} | |
class MyMessage : IMessage | |
{ | |
} | |
class MyEvent : IEvent | |
{ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment