如何解决我们可以使用RabbitMQ
我想知道是否有可能使用RabbitMq,C#发布Action方法。因此,除了排队对象之外,我不想排队其参数有所变化的Function(或Action)并将其发布,并且Consumer(Async)应该只能够执行Function(或Action)。我的目标是拥有一个通用的消息队列,以便能够接收和容纳任何函数调用。以前,我正在发送对象并进行序列化,对其进行编码,然后发布,而在使用者处将反序列化并执行,但是现在我要排队的函数不尽相同,这些函数因参数类型而异,所以不想在公开时公开每种参数类型。排队消息反而需要以更通用的方式解决它们。 我也欢迎其他任何可以使其更通用的方法。请指导。
以前,我曾经发送过
public void QueueObject(object obj,string type) // can i pass Action here instead of obj and type?
{
IBasicProperties prop = model.CreateBasicProperties();
prop.Persistent = true;
prop.ContentType = "application/json";
prop.Type = type;
string jsonObj = JsonConvert.SerializeObject(obj);
byte[] buffer = Encoding.UTF8.GetBytes(jsonObj);
model.BasicPublish("",queueName,prop,buffer);//how can i publish a Action and receive it at consumer?
}
public void DispatchQueueMessages()
{
model.BasicQos(0,1,false);
var consumer = new AsyncEventingBasicConsumer(model);
model.BasicGet(queueName,autoAck: true);
consumer.Received += async (sender,args) =>
{
string messageType = args.BasicProperties.Type;
var jsonString = Encoding.UTF8.GetString(args.Body.Span);
Object param = null;
if(messageType.Equals(nameof("ABCType1")))
{
param = JsonConvert.DeserializeObject<ABCType1>(jsonString); //I don't want to expose
any ABCType1 here and want to make it generic
}
var response = await ExecuteMessageinQueue(param,messageType);
...//handle response here,yes i care about the response and want to handle them even when passed as an Action
...
}
}
从differentclass.cs, 我想这样调用
msgQ.QueueObject(() => client.SendABCStatusAsync(id,abcStatus));//abcStatus is an object of ABCType1
..
msgQ.QueueObject(() => client.GiveXYZStatusAsync(id,xyzStatus,info));//xyzStatus is an object of XYZType2
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。