Can the same application be as a publisher / subscriber with NServiceBus?

I am new to messaging architecture, so maybe I'm wrong. But I wanted to introduce NServiceBus slowly on my team, resolving a tiny problem.

Agenda appointments are made up of states. Two users could see the same appointment on the same agenda, in the same application. They run this application through a remote session on a central server. Therefore, if user 1 updates the destination state, I would like user 2 to see the new state in real time.

To simulate this or to make a proof of concept, if you want, I created a new console application. I got both NServiceBus and NServiceBus.Host through NuGet, because, as I understood from the documentation, I need both. And I know that in the production code it is not recommended to place everything in one assembly, but the publisher and the subscriber most likely fall into the same assembly, though ...

In the Main Method class, I wrote the following code:

BusConfiguration configuration = new BusConfiguration();

configuration.UsePersistence<InMemoryPersistence>();
configuration.UseSerialization<XmlSerializer>();
configuration.UseTransport<MsmqTransport>();
configuration.TimeToWaitBeforeTriggeringCriticalErrorOnTimeoutOutages(new TimeSpan(1, 0, 0));
ConventionsBuilder conventions = configuration.Conventions();
conventions.DefiningEventsAs(t => t.Namespace != null
    && t.Namespace.Contains("Events"));

using (IStartableBus bus = Bus.Create(configuration))
{
    bus.Start();

    Console.WriteLine("Press key");
    Console.ReadKey();

    bus.Publish<Events.AppointmentStateChanged>(a =>
    {
        a.AppointmentID = 1;
        a.NewState = "New state";
    });

    Console.WriteLine("Event published.");
    Console.ReadKey();
}

In the EndPointConfig class, the Configure Added method:

configuration.UsePersistence<InMemoryPersistence>();
configuration.UseSerialization<XmlSerializer>();
configuration.UseTransport<MsmqTransport>();
ConventionsBuilder conventions = configuration.Conventions();
conventions.DefiningEventsAs(t => t.Namespace != null
    && t.Namespace.Contains("Events"));

AppointmentStateChanged is a simple class in the Events folder:

public class AppointmentStateChanged: IEvent {
    public int AppointmentID { get; set; }
    public string NewState { get; set; }
}

PurposeStateChangedHandler is an event handler:

public class AppointmentStateChangedHandler : IHandleMessages<Events.AppointmentStateChanged> {
public void Handle(Events.AppointmentStateChanged message) {
        Console.WriteLine("AppointmentID: {0}, changed to state: {1}", 
            message.AppointmentID, 
            message.NewState);
    }
}

, . , . , : System.Messaging.MessageQueueException( ). - , - . - ?

Everthing AgendaUpdates, , AgendaUpdates.Events.

2 :

  • ( AgendaUpdates2)
  • MessageEndpointMappings App.Config EndPoint "AgendaUpdates2" MSMQ: " "
  • EndPointConfig: configuration.EndpointName( "AgendaUpdates2" ); MSMQ: " "
  • Main Program: configuration.EndpointName( "AgendaUpdates2" ); .

- > , 2 . IDE.

+4
2

, , .

, .

+1

, , , , , . , ( ); , .

NServiceBus (MSMQ ), , , . , .

, , , :

, , MessageEndpointMappings ( /), , / ( "" ):

http://docs.particular.net/nservicebus/messaging/message-owner#configuring-endpoint-mapping

, / , ( ), /, /. . , , ( ).

, , IDE . , .

MessageEndpointMappings App.Config EndPoint "AgendaUpdates2" . MSMQ: " "

, . , "AgendaUpdates" ( "2" ).

EndPointConfig: configuration.EndpointName( "AgendaUpdates2" ); MSMQ: " "

Main Program: configuration.EndpointName( "AgendaUpdates2" );

, . , EndPointConfig , NSB. .

, , , , ( ).

+3

Source: https://habr.com/ru/post/1612891/


All Articles