【发布时间】:2018-03-14 01:15:39
【问题描述】:
我正在尝试使用 Azure 服务总线在多个 azure 函数之间传递消息。我正在代码中创建主题和订阅。但不确定如何从代码中添加过滤器查询。我想按 id 过滤消息。例如 SqlFilter($"'id' ='{id}'") 。由于这个过滤器,我不想为每条消息创建一个订阅。
是否可以为订阅设置不同的过滤器?
可以通过为每个请求创建一个订阅并在之后将其删除来解决此问题。但我不想创建这么多订阅。我的目标是每次调用函数时只更改过滤器。
示例代码:
var connectionString = ConfigurationManager.AppSettings["ServiceBus_CONNECTION_STRING"];
var namespaceManager =
NamespaceManager.CreateFromConnectionString(connectionString);
if (!await namespaceManager.TopicExistsAsync(topicName))
{
// Configure Topic Settings.
var topic = new TopicDescription(topicName)
{
MaxSizeInMegabytes = 1024,
DefaultMessageTimeToLive = TimeSpan.FromMinutes(5)
};
await namespaceManager.CreateTopicAsync(topic);
}
if (!await namespaceManager.SubscriptionExistsAsync(topicName, subscription))
{
await namespaceManager.CreateSubscriptionAsync(topicName, subscription);
}
var cts = new TaskCompletionSource<T>();
var subClient =
SubscriptionClient.CreateFromConnectionString(connectionString, topicName,
subscription);
var ruleDescription = new RuleDescription($"RequestIdRule{id}", new SqlFilter($"'id' ='{id}'"));
await subClient.AddRuleAsync(ruleDescription);
var options = new OnMessageOptions();
subClient.OnMessage(m =>
{
try
{
var body = m.GetBody<T>();
cts.SetResult(body);
}
catch (Exception ex)
{
m.Abandon();
}
},
options);
return await cts.Task;
}
catch (Exception ex)
{
throw;
}
【问题讨论】:
标签: azure azure-functions azureservicebus azure-servicebus-topics