如何清除用于服务总线主题订阅的消息 [英] How to purge messages for Service Bus Topic Subscription
问题描述
只是想知道从服务总线主题的订阅中清除消息的最佳方法(即使通过Portal,Powershell或C#).
Just wondering the best way (even if via Portal, Powershell, or C#) to purge the messages off of a Service Bus Topic's Subscription.
想象一下,我们有一个包含4个订阅的主题,而我们只想清除其中一个订阅中的消息.
Imagine we have a topic with 4 subscriptions, and we only want to purge the messages from one of the subscriptions.
我觉得唯一的方法可能是在while循环中阅读消息,但是希望有更好的东西.
I have a feeling the only way may be to read the messages in a while loop, but hoping for something better.
更新:
除了使用代码外,您还可以按照答案中的建议使用服务器资源管理器-右键单击订阅并清除消息:
Apart from using code, you can use the Server Explorer as suggested in the answer - right click subscription and purge messages:
推荐答案
您当然可以通过代码来完成.如果您使用的是 Service Bus SDK
,则可以执行以下操作:
You can most certainly do it via code. If you're using Service Bus SDK
, you could do something like the following:
static void PurgeMessagesFromSubscription()
{
var connectionString = "Endpoint=sb://account-name.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=access key";
var topic = "topic-name";
var subscription = "subscription-name";
int batchSize = 100;
var subscriptionClient = SubscriptionClient.CreateFromConnectionString(connectionString, topic, subscription, ReceiveMode.ReceiveAndDelete);
do
{
var messages = subscriptionClient.ReceiveBatch(batchSize);
if (messages.Count() == 0)
{
break;
}
}
while (true);
}
此代码将执行的操作是以Receive & Delete
模式从订阅(一次100条)中获取消息,以便一旦获取消息,它们就会自动从订阅中删除.
What this code will do is fetch messages from the subscription (100 at a time) in Receive & Delete
mode so that as soon as messages are fetched, they are deleted from the subscription automatically.
我相信 Service Bus Explorer
工具还具有清除消息的功能.您也可以使用它,而不用编写代码.
I believe Service Bus Explorer
tool also has the capability to purge messages. You can use that as well instead of writing the code.
这篇关于如何清除用于服务总线主题订阅的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!