为Service Bus worker角色设置OperationTimeOut属性

时间:2014-06-25 18:57:15

标签: c# .net azure azure-worker-roles servicebus

我正在使用服务总线工作者角色模板创建一个辅助角色。

处理每条消息需要一两分钟以上。

因此,我发现工作者角色多次收到相同的消息,每分钟大约收到一条消息。

我认为这是因为此值默认为60秒。

http://msdn.microsoft.com/en-us/library/microsoft.servicebus.messaging.messagingfactorysettings.operationtimeout.aspx

但是我不知道如何增加这个值,因为我没有在任何地方看到messageFactorySettings类。

我在哪里设置此属性?

这是我正在使用的代码

public class WorkerRole : RoleEntryPoint
    {

        // QueueClient is thread-safe. Recommended that you cache 
        // rather than recreating it on every request
        QueueClient Client;
        ManualResetEvent CompletedEvent = new ManualResetEvent(false);

        public override void Run()
        {

            Client.OnMessage((receivedMessage) =>
                {
                    ProcessMessage(recievedMessage);
                });

            CompletedEvent.WaitOne();
        }

        public override bool OnStart()
        {
            ServicePointManager.DefaultConnectionLimit = 12;

            string connectionString = ConfigurationUtility.GetConnectionString("Microsoft.ServiceBus.ConnectionString");
            string queneName = ConfigurationUtility.GetConnectionString("QueueName");

            // Create the queue if it does not exist already
            var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);
            if (!namespaceManager.QueueExists(queneName))
            {
                namespaceManager.CreateQueue(queneName);

            }

            Client = QueueClient.CreateFromConnectionString(connectionString, queneName);
            return base.OnStart();
        }

        public override void OnStop()
        {
            // Close the connection to Service Bus Queue
            Client.Close();
            CompletedEvent.Set();
            base.OnStop();
        }
    }

2 个答案:

答案 0 :(得分:5)

使用ConnectionStringBuilder比自己创建MessagingFactory所需的地址更容易使用:

var builder = new  ServiceBusConnectionStringBuilder(_connectionString)
{
OperationTimeout = TimeSpan.FromMinutes(2) 
};

var messagingFactory = MessagingFactory.CreateFromConnectionString(builder.ToString());
var queueClient = MessagingFactory.CreateQueueClient(_queuePath);

答案 1 :(得分:1)

从我可以收集到的内容中,您需要使用MessagingFactory类来执行此操作。

我刚刚写了以下内容,将超时时间增加到2分钟:

MessagingFactorySettings settings = new MessagingFactorySettings { 
                OperationTimeout = new TimeSpan(0, 2, 0), 
                TokenProvider = TokenProvider.CreateSharedSecretTokenProvider("issuer", "sharedkey") };

var address = ServiceBusEnvironment.CreateServiceUri("sb", "serviceNamespace", string.Empty);

var messagingFactory = MessagingFactory.Create(address, settings);

return messagingFactory.CreateSubscriptionClient("queueName");