0

I'm trying to create a 'once-off' process which will pre-load my service with a bunch of data by raising the appropriate events in NServiceBus 5. However I'm struggling with how I might configure my 'fake' subscription so the events get published to the appropriate endpoint.

Here is my code to create my bus to send the messages:

var busConfiguration = new BusConfiguration();

busConfiguration.UseSerialization<XmlSerializer>();
busConfiguration.UseTransport<MsmqTransport>();
busConfiguration.UsePersistence<InMemoryPersistence>();

using (var bus = Bus.CreateSendOnly(busConfiguration))
{       
    bus.Publish(new MyEvent());
}

But I'm unsure of how to get access to the ISubscriptionStorage for the InMemoryPersistence. I'm guessing if I can get access, I can do something like:

var subscriptionStorage = GetSubscriptionStorage();
subscriptionStorage.Subscribe(new Address("MyQueueName", "MyMachineName"), new MessageType(typeof(MyEvent)));

Any ideas?

Rafik Tighilt
  • 2,071
  • 1
  • 15
  • 27
antmeehan
  • 865
  • 9
  • 11

2 Answers2

1

You're creating a send-only endpoint. Send-only endpoints send messages out and do not recieve. Due to that, there will be no events received and subscription is not requried.

Switching endpoint to a full endpoint will require a storage to be defined. You could wrap InMemoryPersistence storage with your own implementation or fully replace it with a custom one.

Sean Feldman
  • 23,443
  • 7
  • 55
  • 80
  • My once-off process only needs to send messages to an *existing* endpoint. Hence the use of the Send-Only endpoint. My problem is that I can't convince it to send Events to a that existing endpoint. I've ended up just using the native MSMQ API to post the messages as per https://docs.particular.net/transports/msmq/operations-scripting?version=core_5 – antmeehan Aug 10 '18 at 02:46
  • I see. Was confused by your ask to get access to `ISubscriptionStorage`. In case you just need to dispatch a message (event or command), native integration is a valid route. – Sean Feldman Aug 11 '18 at 16:06
0

I've ended up just using the MSMQ API to push the messages onto the appropriate endpoint queue. Using tips from https://docs.particular.net/transports/msmq/operations-scripting?version=core_5 and https://docs.particular.net/nservicebus/messaging/headers?version=core_5.

Here's the helper class I built, which you pass in MessageQueue and my event message:

private static void SendMessage<T>(MessageQueue messageQueue, T eventMessage)
{
    var xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(T));

    using (var scope = new TransactionScope())
    {
        using (var stream = new MemoryStream())
        {
            xmlSerializer.Serialize(stream, eventMessage);

            stream.Position = 0;

            using (var message = new Message())
            {
                message.BodyStream = stream;
                message.Extension = CreateHeaders(new List<HeaderInfo>()
                {
                    new HeaderInfo() {Key = "NServiceBus.ContentType", Value = "text/xml"},
                    new HeaderInfo() {Key = "NServiceBus.EnclosedMessageTypes", Value = typeof(T).FullName},
                });
                messageQueue.Send(message, MessageQueueTransactionType.Automatic);
            }
        }

        scope.Complete();
    }
}
antmeehan
  • 865
  • 9
  • 11