鉴于下面的代码,这是采用requestDTO(LeadInformation)并通过ServiceStack中的Messaging抽象将其发布到RabbitMq的正确/最快方式吗?这个impl有效,但鉴于ServiceStack的内置功能,我可能会遗漏一些东西。
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,
};
}
}
谢谢你, 斯蒂芬
答案 0 :(得分:2)
ServiceStack Messaging API提供另一个渠道来调用您的服务。您不需要注入自己的IMessageQueueClient
,因为ServiceStack的Service类已经包含对ServiceStack base.MessageProducer
依赖项和PublishMessage
API的访问权限。改为使用:
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,
};
}
注意:我已经更改了重新发布请求DTO:
PublishMessage(request);
将其转换为备用请求DTO:
PublishMessage(request.ConvertTo<ProcessLeadInfo>());
因为您不太可能想要重新发布相同的Request DTO,这会导致自己以递归循环结束,例如:
LeadInformation DTO -> RabbitMQ Broker -> RabbitMQ Server -> Post(LeadInformation)
使用HTTP客户端作为类比,它与使用ServiceClient调用自己一样:
public object Post(LeadInformation request)
{
//...
client.Post(request);
}
通过使用单独的Request DTO,我们确保只调用一次此服务(源自LeadInformation Service):
public object Any(ProcessLeadInfo request)
{
//...
}
ServiceClients SendOneWay
API调用ServiceStack的特殊pre-defined /oneway
路由,如果已注册,将自动将请求DTO发布到您注册的MQ服务器,否则将正常调用服务:
client.SendOneWay(new ProcessLeadInfo { ... });