问题
I'm just getting started with NServiceBus and I've got a very basic implementation up and running - much like the PubSub sample application.
What I want to do is to send some information when a subscriber subscribes.
For example, lets say I have a database with products that are categorized. My publisher will check the database every N seconds and will send messages when a new product is added to the database.
But each subscriber is only interested in a particular category and I want to send that category when they subscribe so that the publisher knows the check the database for new products in that category.
The categories are dynamic, so I can't create different messages for the different categories. So for that reason I assume that all the subscribers have to subscribe to the same published IMessage
.
NOTE: I do also wish to use this information to then filter the messages NServiceBus sends to subscribers, but I thought I would post that as a separate question.
回答1:
You can implement the IAuthorizeSubscriptions interface to get a callback at the publisher when a subscriber requests to subscribe. If you wanted to include some additional meta-data in the subscription request, you'll need to use Bus.OutgoingHeaders - then you can look at that info at the publisher and decided what to do with it.
回答2:
I haven't worked with NServiceBus for quite some time but I don't think NServiceBus has any events to notify you when a subscription has taken place.
Be that as it may I think you may be approaching your solution on a level that is too fine.
You usually subscribe to events that the business is typically interested in. In your case when a new product is added. A simpler solution would be to simply subscribe to say a ProductAddedEvent
and have your subscriber ignore any messages that it is not interested in.
Self-promotion warning! :) --- I have a FOSS ESB available here (still CTP): http://shuttle.codeplex.com/
We make use of pipelines for various parts of the bus and you have given me an idea to include a SubscriptionRequested
event on the StartupPipeline
.
来源:https://stackoverflow.com/questions/6592499/nservicebus-sending-information-when-a-subscriber-subscribes