如何为WCF服务动态设置TransactionTimeout?

时间:2013-09-23 16:26:25

标签: c# wcf msmq transactionscope netmsmqbinding

我需要能够为在MSMQ上运行的WCF服务动态设置事务超时。我正在关注MSDN上的示例:http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.channeldispatcher.transactiontimeout.aspx。但是,我设置的超时似乎不起作用。

我用来设置 TransactionTimeout 属性的代码如下:

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;
}

当我运行我的服务并在服务实现中延迟2分钟时,当我尝试写入另一个MSMQ队列时收到事务登记错误:

  

异常:发送到队列时发生错误:   指定的交易无法入伍。 (-1072824232,   0xc00e0058)。确保已安装并运行MSMQ。如果你是   发送到本地队列,确保队列存在所需的队列   访问模式和授权。

过去有没有人能够让这个工作?任何想法都将不胜感激。

提前谢谢。

1 个答案:

答案 0 :(得分:0)

我找到了正确的方法。执行此操作的正确方法是查找和修改附加到服务描述的ServiceBehaviorAttribute对象:

var transactionTimeout = TimeSpan.FromSeconds(...);
var behavior = serviceHost.Description.Behaviors.Find<ServiceBehaviorAttribute>();
behavior.TransactionTimeout = transactionTimeout.ToString();
serviceHost.Open();
相关问题