Question

I have a requirement to be able to dynamically set the transaction timeout for a WCF service running over MSMQ. I am following the example on MSDN: http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.channeldispatcher.transactiontimeout.aspx. However, the timeout that I am setting does not seem to be working.

The code that I am using to set the TransactionTimeout property is below:

ServiceProperties properties = ...; // has a TransactionTimeout value for the service
var serviceHost = new ServiceHost(...);
serviceHost.Open();
var channelDispatchers =
    serviceHost.ChannelDispatchers.Select(
        cd => new ChannelDispatcher(cd.Listener)).ToArray();
foreach (var channelDispatcher in channelDispatchers)
{
    channelDispatcher.TransactionTimeout = properties.TransactionTimeout;
}

When I run my service and put a 2-minute delay in my service implementation, I receive a transaction enlistment error when I try to write to another MSMQ queue:

Exception: An error occurred while sending to the queue: The transaction specified cannot be enlisted. (-1072824232, 0xc00e0058).Ensure that MSMQ is installed and running. If you are sending to a local queue, ensure the queue exists with the required access mode and authorization.

Has anyone been able to get this to work in the past? Any ideas would be greatly appreciated.

Thank you in advance.

Was it helpful?

Solution

I figured out the right approach. The correct way to do this is to find and modify the ServiceBehaviorAttribute object that is attached to the service description:

var transactionTimeout = TimeSpan.FromSeconds(...);
var behavior = serviceHost.Description.Behaviors.Find<ServiceBehaviorAttribute>();
behavior.TransactionTimeout = transactionTimeout.ToString();
serviceHost.Open();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top