程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> WCF分布式開發常見錯誤(14):無效的操作異常,At least one operation on the .

WCF分布式開發常見錯誤(14):無效的操作異常,At least one operation on the .

編輯:關於.NET

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>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved