我从我在 azure 中设置的队列接收消息时遇到问题。我之前使用相同的代码成功地完成了此操作,但现在当我尝试获取消息时我只是得到空值。当我在 azure 管理控制台中查看队列时,我清楚地看到队列包含 5 条消息。
这是代码:
ServiceBus SB = new ServiceBus();
Microsoft.ServiceBus.Messaging.BrokeredMessage message;
while (true)
{
message = SB.ReceiveMessage("orders");
if (message == null)
{
break;
}
Procurement.Order order = message.GetBody<Procurement.Order>();
order.id = Guid.NewGuid().ToString();
order.remindercount = 0;
using (DbManager db = new DbManager())
{
if (db.SetSpCommand("CreateOrderHead",
db.Parameter("@companyId", order.companyId),
db.Parameter("@orderId", order.orderId),
db.Parameter("@suppliercode", order.suppliercode),
db.Parameter("@supplierorderId", order.supplierorderId),
db.Parameter("@orderdate", order.orderdate),
db.Parameter("@desireddate", order.desireddate),
db.Parameter("@ordertext", order.ordertext),
db.Parameter("@name", order.name),
db.Parameter("@street", order.street),
db.Parameter("@zip", order.zip),
db.Parameter("@city", order.city),
db.Parameter("@country", order.country),
db.Parameter("@countrycode", order.countrycode),
db.Parameter("@deliveryterms", order.deliveryterms),
db.Parameter("@reference", order.reference),
db.Parameter("@deliveryinstruction", order.deliveryinstruction),
db.Parameter("@id", order.id),
db.Parameter("@partycode", order.partyCode)
).ExecuteNonQuery() == 1)
{
message.Complete();
message = null;
}
db.SetSpCommand("DeleteOrderRows",
db.Parameter("@orderid", order.orderId),
db.Parameter("@companyId", order.companyId)
).ExecuteNonQuery();
foreach (Procurement.Orderrow r in order.Orderrows)
{
db.SetSpCommand("CreateOrderRow",
db.Parameter("@companyId", r.companyId),
db.Parameter("@orderId", r.orderId),
db.Parameter("@orderrowId", r.orderrowId),
db.Parameter("@itemId", r.itemId),
db.Parameter("@itemdesc", r.itemdesc),
db.Parameter("@orderqty", r.orderqty),
db.Parameter("@desireddate", r.desireddate),
db.Parameter("@rowtext", r.rowtext),
db.Parameter("@supplieritemId", r.supplieritemId),
db.Parameter("@unit", r.unit),
db.Parameter("@id", order.id),
db.Parameter("@unitprice", r.unitprice),
db.Parameter("@rowprice", r.rowprice)
).ExecuteNonQuery();
}
}
}
Thread.Sleep(new TimeSpan(0, 1, 0));
这是 ServiceBus 类:
public class ServiceBus
{
TokenProvider TokenProvider;
MessagingFactory Factory;
public ServiceBus()
{
TokenProvider = TokenProvider.CreateSharedSecretTokenProvider(GetIssuerName(), GetSecret());
Factory = MessagingFactory.Create(
GetURINameSpace(),
TokenProvider
);
}
public void SendMessage(string queue, BrokeredMessage message)
{
var client = Factory.CreateQueueClient(queue);
client.Send(message);
}
public BrokeredMessage ReceiveMessage(string queue)
{
var client = Factory.CreateQueueClient(queue, ReceiveMode.ReceiveAndDelete);
BrokeredMessage message = client.Receive();
return message;
}
private static Uri GetURINameSpace()
{
return ServiceBusEnvironment.CreateServiceUri("sb", GetNamespace(), string.Empty);
}
private static string GetNamespace()
{
return "Namespace i have verified its the right one";
}
private static string GetIssuerName()
{
return "Issuer i have verified its the right one";
}
private static string GetSecret()
{
return "Key i have verified its the right one";
}
}
我认为这应该很简单,但我不知道我做错了什么。它可能是我缺少的一些小东西......
无论如何,提前谢谢!