WCF分布式开发常见错误(14):无效的操作异常,At least one operation on the ...

Posted on 2009-05-30 23:41 Frank Xu Lei 阅读(375) 评论(2)   编辑 收藏 网摘 所属分类: WCF分布式开发常见错误

    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.

异常界面如图:

我是在调试客户端事务模式的时候出现的错误,服务契约和服务类的代码如下:
 
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 ;

        }
    }
}
我查阅到的资料:
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)].
可以解决问题。但是我不能这样。
我这里是为了测试客户端事务模式写的代码。
所以大家都什么好的解决办法~谢谢
3.已经找到解决办法:
使用配置文件方式:
添加一个绑定协议节点,。也可以使用编程方式。这里给出代码:
     < bindings >
      
< netTcpBinding  >
        
< binding name = " netTcpBindingTcp "  transactionFlow = " true "  transactionProtocol = " WSAtomicTransactionOctober2004 "    >

          
<!--< reliableSession enabled = " true "  ordered = " true " />
          
< security mode = " None " ></ security >-->
        
</ binding >
      
</ netTcpBinding >
    
</ bindings >
然后在服务终结点里使用这个配置:


 

  < endpoint 
          address
= " net.tcp://localhost:9002/WCFServiceTransaction2 "  
           bindingConfiguration
= " netTcpBindingTcp "
          binding
= " netTcpBinding "  
          contract
= " WCFService.IWCFServiceTransaction2 " >
        
</ endpoint >

 

老徐的博客

【作者】:Frank Xu Lei

【地址】:http://www.cnblogs.com/frank_xl/archive/2009/05/30/1492513.html

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
"java.io.IOException: Stream closed"是一个常见错误消息,它表示在处理输入或输出流时出现了问题。在Java中,输入和输出流是用于读取和写入数据的工具。 通常情况下,抛出这个异常的原因是由于输入或输出流在操作之前已被关闭。当我们使用Java的IO类进行输入或输出操作时,我们需要按照一定的顺序正确关闭流,以避免出现此错误。 在遇到这个错误消息时,我们可以检查以下几个方面: 1. 检查是否正确地打开和关闭了输入或输出流。在使用完流之后,我们应该使用`close()`方法来关闭流。 例如,在读取文件时,我们应该使用以下代码片段: ```java try { FileInputStream file = new FileInputStream("myfile.txt"); // 读取文件的代码逻辑 } catch (IOException e) { e.printStackTrace(); } finally { try { if (file != null) { file.close(); // 关闭流 } } catch (IOException e) { e.printStackTrace(); } } ``` 2. 检查代码中是否存在多个线程尝试共享同一个流。当多个线程同时对同一个流进行操作时,可能会导致其中一个线程关闭了流,而其他线程尝试读取或写入数据时抛出"Stream closed"异常。确保在多线程环境中正确同步流的访问。 3. 检查流对象是否被重复使用。有时我们可能会在多个地方使用相同的流对象进行读写操作。如果在一次操作之后关闭了流,在后续操作中再次使用该流对象将导致"Stream closed"异常。确保每次操作都使用一个新的流对象。 总之,当遇到"java.io.IOException: Stream closed"异常时,我们应该仔细检查流的打开和关闭过程,确保在正确的时间关闭流,并避免多个线程或重复使用流对象造成的问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值