WCF分布式开发常见错误(14):无效的操作异常,At least one operation on the ...
2009-05-30 23:41
676 查看
WCF事务编程过程中,会出现这个操作无效异常。信息如下:
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.
异常界面如图:
[align=left]我是在调试客户端事务模式的时候出现的错误,服务契约和服务类的代码如下:[/align]
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 sucessfullyname length is:{0}", name.Length);
}
else
{
Console.WriteLine("Calling WCF Transaction unsucessfullyname 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;
}
}
}
[align=left]我查阅到的资料:
1.http://social.msdn.microsoft.com/Forums/en-US/windowstransactionsprogramming/thread/d3020922-92b3-4dc5-ab22-b8fe34cbf40e;这个是属性没定义在操作契约上。
2.http://blogs.msdn.com/thelever/archive/2007/03/29/configuration-of-transactions-of-use-in-wcf.aspx;这个建议更换[TransactionFlow(TransactionFlowOption.Allowed)].
可以解决问题。但是我不能这样。
我这里是为了测试客户端事务模式写的代码。
所以大家都什么好的解决办法~谢谢[/align]
[align=left]3.已经找到解决办法:[/align]
[align=left]使用配置文件方式:[/align]
[align=left]添加一个绑定协议节点,。也可以使用编程方式。这里给出代码:[/align]
<bindings>
<netTcpBinding >
<binding name="netTcpBindingTcp" transactionFlow="true" transactionProtocol="WSAtomicTransactionOctober2004" >
<!--<reliableSession enabled="true" ordered="true"/>
<security mode="None"></security>-->
</binding>
</netTcpBinding>
</bindings>
[align=left]然后在服务终结点里使用这个配置:[/align]
<endpoint
address="net.tcp://localhost:9002/WCFServiceTransaction2"
bindingConfiguration="netTcpBindingTcp"
binding="netTcpBinding"
contract="WCFService.IWCFServiceTransaction2">
</endpoint>
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.
异常界面如图:
[align=left]我是在调试客户端事务模式的时候出现的错误,服务契约和服务类的代码如下:[/align]
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 sucessfullyname length is:{0}", name.Length);
}
else
{
Console.WriteLine("Calling WCF Transaction unsucessfullyname 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;
}
}
}
[align=left]我查阅到的资料:
1.http://social.msdn.microsoft.com/Forums/en-US/windowstransactionsprogramming/thread/d3020922-92b3-4dc5-ab22-b8fe34cbf40e;这个是属性没定义在操作契约上。
2.http://blogs.msdn.com/thelever/archive/2007/03/29/configuration-of-transactions-of-use-in-wcf.aspx;这个建议更换[TransactionFlow(TransactionFlowOption.Allowed)].
可以解决问题。但是我不能这样。
我这里是为了测试客户端事务模式写的代码。
所以大家都什么好的解决办法~谢谢[/align]
[align=left]3.已经找到解决办法:[/align]
[align=left]使用配置文件方式:[/align]
[align=left]添加一个绑定协议节点,。也可以使用编程方式。这里给出代码:[/align]
<bindings>
<netTcpBinding >
<binding name="netTcpBindingTcp" transactionFlow="true" transactionProtocol="WSAtomicTransactionOctober2004" >
<!--<reliableSession enabled="true" ordered="true"/>
<security mode="None"></security>-->
</binding>
</netTcpBinding>
</bindings>
[align=left]然后在服务终结点里使用这个配置:[/align]
<endpoint
address="net.tcp://localhost:9002/WCFServiceTransaction2"
bindingConfiguration="netTcpBindingTcp"
binding="netTcpBinding"
contract="WCFService.IWCFServiceTransaction2">
</endpoint>
相关文章推荐
- WCF分布式开发常见错误(14):无效的操作异常,At least one operation on the ...
- WCF分布式开发常见错误(23):This could be due to the fact that the server certificate is not configured properly with HTTP.SYS
- WCF分布式开发常见错误解决(7):Cannot have two operations in the same contract
- WCF分布式开发常见错误解决(1):An error occurred while attempting to find services at...添加服务引用出错
- WCF分布式开发常见错误(11):There is already a listener on IP endpoint ,IP 终结点 已经存在侦听器
- WCF分布式开发常见错误(12):The server was unable to process the request,服务无法处理请求
- WCF分布式开发常见错误(15):Communication with the underlying transaction manager has failed.
- WCF分布式开发常见错误(22):The caller was not authenticated...如何在XP系统为WCF设置可信任的证书
- WCF分布式开发常见错误解决(9):无终结点监听,There was no endpoint listening at
- WCF分布式开发常见错误(12):The server was unable to process the request,服务无法处理请求
- WCF分布式开发常见错误(22):The caller was not authenticated...如何在XP系统为WCF设置可信任的证书
- WCF分布式开发常见错误(28):The requested upgrade is not supported by...
- WCF分布式开发常见错误解决(12):The server was unable to process the request,服务无法处理请求
- WCF分布式开发常见错误解决(1):An error occurred while attempting to find services at...添加服务引用出错
- WCF分布式开发常见错误(24):Could not establish trust relationship for the SSL/TLS secure channel with authority(转)
- WCF分布式开发常见错误(27):Secure channel cannot be opened because security negotiation with the remote endpoint has failed
- WCF分布式开发常见错误解决(9):无终结点监听,There was no endpoint listening at
- WCF分布式开发常见错误(26):Authentication failed because the remote party has closed the transport stream.
- WCF分布式开发常见错误(28):The requested upgrade is not supported by...
- WCF分布式开发常见错误(13):The transaction under which this method call was executing ,此方法调用的事务被异步中断