从连接字符串创建ASB MessagingFactory并提供设置
本文关键字:设置 MessagingFactory ASB 连接 字符串 创建 | 更新日期: 2023-09-27 18:05:17
我想创建一个Azure服务总线MessagingFactory
实例从连接字符串和特定的BatchFlushInterval
设置。
MessagingFactory
上接受连接字符串的factory方法不接受MessagingFactorySettings
实例
MessagingFactory
上接受MessagingFactorySettings
实例的factory方法不接受连接字符串
我最接近的是:
var connectionStringBuilder = new ServiceBusConnectionStringBuilder(connectionString);
var messagingSettings = new MessagingFactorySetttings
{
TokenProvider = TokenProvider.CreateSharedSecretTokenProvider(connectionStringBuilder.SharedSecretIssuerName, connectionStringBuilder.SharedSecretIssuerSecret),
NetMessagingTransportSettings =
{
BatchFlushInterval = TimeSpan.FromMilliseconds(100) // <-- This is the setting I want to specify.
}
};
var messagingFactory = MessagingFactory.CreateFromConnectionString(connectionStringBuilder.Endpoints, messagingSettings);
但是,只有当您知道这是连接字符串中提供的特定令牌提供程序信息时,这才有效。如果它做了其他事情,比如使用其他五种提供令牌的方法之一,那么这段代码就会失败。
如何从连接字符串和指定BatchFlushInterval
创建MessagingFactory
实例?
我发现这样做的方法是使用名称空间管理器中的令牌提供程序。所以:
var namespaceMngr = NamespaceManager.CreateFromConnectionString(namespaceConnString);
MessagingFactorySettings mfs = new MessagingFactorySettings();
mfs.TokenProvider = namespaceMngr.Settings.TokenProvider;
mfs.NetMessagingTransportSettings.BatchFlushInterval = TimeSpan.FromSeconds(timeToFlush);
MessagingFactory mf = MessagingFactory.Create(namespaceMngr.Address, mfs);
如果你试图设置AmqpTransportSettings.BatchFlushInterval
而不是NetMessagingTransportSettings.BatchFlushInterval
,那么我无法帮助你,我实际上无意中发现了这篇文章寻找答案。试图改变AmqpTransportSettings.BatchFlushInterval
似乎不坚持MessageFactory
,即使MessageFactorySettings
反映的变化。
JordanSchillers回答修复了令牌提供程序问题,但我的地址现在使用端口9355而不是9354。
我结束了使用ServiceBusConnectionStringBuilder和NamespaceManager的混合:
var serviceBusConnectionString = new ServiceBusConnectionStringBuilder(connection.ConnectionString);
MessagingFactorySettings factorySettings = new MessagingFactorySettings();
factorySettings.TransportType = serviceBusConnectionString.TransportType;
//Use the namespacemanager to create the token provider.
var namespaceManager = NamespaceManager.CreateFromConnectionString(connection.ConnectionString);
factorySettings.TokenProvider = namespaceManager.Settings.TokenProvider;
factorySettings.NetMessagingTransportSettings.BatchFlushInterval = TimeSpan.FromMilliseconds(batchTimeInMs);
MessagingFactory factory = MessagingFactory.Create(serviceBusConnectionString.Endpoints, factorySettings);
return factory.CreateTopicClient(topicName);