问题
I am creating a worker role using the service bus worker role template.
It is taking more than a minute for me to process each message.
Because of this, i am seeing that the same message is received by the worker role multiple times, roughly one message every minute.
I figured that this is because this value defaults to 60 seconds.
http://msdn.microsoft.com/en-us/library/microsoft.servicebus.messaging.messagingfactorysettings.operationtimeout.aspx
But I am not sure how to increase this value, because i am not seeing the messageFactorySettings class anywhere.
Where do I set this property?
here is the code I am using
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();
}
}
回答1:
Use the ConnectionStringBuilder which is easier to use than creating the necessary address for MessagingFactory by yourself:
var builder = new ServiceBusConnectionStringBuilder(_connectionString)
{
OperationTimeout = TimeSpan.FromMinutes(2)
};
var messagingFactory = MessagingFactory.CreateFromConnectionString(builder.ToString());
var queueClient = MessagingFactory.CreateQueueClient(_queuePath);
回答2:
From what I can gather, you need to use the MessagingFactory
class to do this.
I've just written the following to increase the timeout to 2 mins:
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");
来源:https://stackoverflow.com/questions/24416435/setting-the-operationtimeout-property-for-a-service-bus-worker-role