WCF分布式开发常见错误(14):无效的操作异常,At least one operation on the .2011-03-31 博客园 Frank Xu LeiWCF事务编程过程中,会出现这个操作无效异常。信息如下:At least one operation on the "WCFServiceTransaction1" contract is configured with the TransactionFlowAttribute attribute set to Mandatory but the channel"s binding "NetTcpBinding" is not configured with a TransactionFlowBindingElement. The TransactionFlowAttribute attribute set to Mandatory cannot be used without a TransactionFlowBindingElement.异常界面如图:

我是在调试客户端事务模式的时候出现的错误,服务契约和服务类的代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Runtime.Serialization;
using System.Transactions;
using System.Diagnostics;
//ServiceContract 属性以及 Indigo 使用的所有其他属性均在 System.ServiceModel 命名空间中定义,
//因此本例开头使用 using 语句来引用该命名空间。
namespace WCFService
{
//1.服务契约
[ServiceContract(Namespace = "http://www.cnblogs.com/frank_xl/")]
public interface IWCFServiceTransaction1
{
//操作契约
[OperationContract]
//强制事务流,使用客户端事务
//[TransactionFlow(TransactionFlowAttribute
[TransactionFlow(TransactionFlowOption.Mandatory)]
bool AddNewData(string name);
}
//2.服务类,实现契约
//事务有10分钟的超时限制
//[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
[ServiceBehavior(IncludeExceptionDetailInFaults = true, TransactionTimeout = "00:30:00")]
public class WCFServiceTransaction1 : IWCFServiceTransaction1
{
//实现接口定义的方法
//需要事务环境,启动事务
[OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
public bool AddNewData(string name)
{
Transaction transaction = Transaction.Current;
Debug.Assert(System.Transactions.Transaction.Current.TransactionInformation.DistributedIdentifier!=Guid.Empty);
Console.WriteLine("Create a new Transaction at {0}", System.Transactions.Transaction.Current.TransactionInformation.CreationTime);
Console.WriteLine("WCFService 1 Transaction Status is {0}", System.Transactions.Transaction.Current.TransactionInformation.Status);
Console.WriteLine("WCFService 1 Transaction LocalIdentifier is {0}", System.Transactions.Transaction.Current.TransactionInformation.LocalIdentifier);
Console.WriteLine("WCFService 1 Transaction DistributedIdentifier is {0}", System.Transactions.Transaction.Current.TransactionInformation.DistributedIdentifier);
//using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope())
//{
try
{
using (userTableAdapter adapter = new userTableAdapter())
{
if (1 == adapter.CreateUser(name))
{
Console.WriteLine("Calling WCF Transaction sucessfully
name length is:{0}", name.Length);
}
else
{
Console.WriteLine("Calling WCF Transaction unsucessfully
name length is:{0}", name.Length);
//return false;
}
}
}
catch (Exception e)
{
Console.WriteLine("Calling WCF Transaction error:{0}", e.Message);
throw e;
//return false;
}
//ts.Complete();
return true;
}
}
}