Reputation: 6363
Given the code below, is this the proper/fastest way to take the requestDTO (LeadInformation) and publish it to RabbitMq via the Messaging abstractions in ServiceStack? This impl works, but given the built-in functionality of ServiceStack, I could be missing something.
public override void Configure(Container container)
{
// https://github.com/ServiceStack/ServiceStack/wiki/The-IoC-container
container.Register<IMessageService>(c => new RabbitMqServer{ DisablePriorityQueues = true });
var mqServer = (RabbitMqServer)container.Resolve<IMessageService>();
mqServer.Start();
container.Register<IMessageQueueClient>(c => mqServer.CreateMessageQueueClient());
}
public class Service : ServiceStack.Service
{
public IMessageQueueClient MessageQueueClient { get; set; }
public object Post(LeadInformation request)
{
if (request == null) throw new ArgumentNullException("request");
var sw = Stopwatch.StartNew();
MessageQueueClient.Publish(request);
return new LeadInformationResponse
{
TimeTakenMs = sw.ElapsedMilliseconds,
};
}
}
Thank you, Stephen
Upvotes: 0
Views: 599
Reputation: 143339
ServiceStack's Messaging API provides another channel to invoke your Services. You don't need to inject your own IMessageQueueClient
as the ServiceStack's Service class already includes access to ServiceStack base.MessageProducer
dependency and PublishMessage
API which you can use instead:
public object Post(LeadInformation request)
{
if (request == null) throw new ArgumentNullException("request");
var sw = Stopwatch.StartNew();
PublishMessage(request.ConvertTo<ProcessLeadInfo>());
return new LeadInformationResponse
{
TimeTakenMs = sw.ElapsedMilliseconds,
};
}
Note: I've changed republishing the Request DTO:
PublishMessage(request);
To converting it to an alternate Request DTO:
PublishMessage(request.ConvertTo<ProcessLeadInfo>());
Since it's unlikely you want to re-publish the same Request DTO which has the effect of calling yourself ending up in a recursive loop, e.g:
LeadInformation DTO -> RabbitMQ Broker -> RabbitMQ Server -> Post(LeadInformation)
To use a HTTP Client as an analogy it would be the same as calling yourself with a ServiceClient:
public object Post(LeadInformation request)
{
//...
client.Post(request);
}
By using a separate Request DTO we ensure that this Service is only called once, (originating from the LeadInformation Service):
public object Any(ProcessLeadInfo request)
{
//...
}
The ServiceClients SendOneWay
API's calls ServiceStack's special pre-defined /oneway
route which will automatically publish the Request DTO to your registered MQ Server if one is registered, otherwise will invoke the Service as normal:
client.SendOneWay(new ProcessLeadInfo { ... });
Upvotes: 2