0

WCF でトランザクション フローを実装しようとしています。My Clientコードを実行しているとき、レコードがdbでロックされているのを見ると、トランザクションがクライアントからサービスに伝播していることがわかります(SQLサーバーで with(nolock) を使用して、トランザクションが発生しているかどうかを確認しました)。ただし、クライアント コードは正常に完了しますが、トランザクションはロールバックされます。

以下はコードスニペットです。

サービスクラス

[ServiceBehavior( TransactionIsolationLevel = System.Transactions.IsolationLevel.ReadCommitted,  TransactionTimeout = "00:00:30")]
public class TransactionTest : ITransactionTest
{
[OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = false)]
public void DoWork(string aliasName)
{    
    string constr = "Data Source=MyDBServer;User ID=DBUser;password=123;Initial Catalog=MyTestDB;";
    using (SqlConnection objcon = new SqlConnection(constr))
    {
        string qry = @"UPDATE Customer SET Alias = '" + aliasName + "' WHERE CustomerID = 10";
        SqlCommand cmd = new SqlCommand(qry, objcon);
        objcon.Open();
        cmd.ExecuteNonQuery();
    }
}

}

 Service Contract:

 [ServiceContract(SessionMode = SessionMode.Required)]
 public interface ITransactionTest
 {
    [OperationContract, TransactionFlow(TransactionFlowOption.Allowed)]    
void DoWork(string aliasName);
 }

以下は私のWeb.configです

<bindings>
  <wsHttpBinding>
    <binding name="wsHttpBinding_ITransactionTest" transactionFlow="true"/>
  </wsHttpBinding>
</bindings>

クライアント アプリケーション (コンソール アプリケーション) にサービス参照を追加しました。

class Program
{
    static void Main(string[] args)
    {

        //updateCustomeAlias();
        try
        {
            using (TransactionScope TranScope = new TransactionScope(TransactionScopeOption.RequiresNew))
            {
                var proxy = new TransactionTestClient("WSHttpBinding_ITransactionTest");
                proxy.DoWork("XYZ");
                TranScope.Complete();
            }
        }
        catch 
        { 
            //Exception handling.
        }
    }

クライアント アプリケーションの構成

 <system.serviceModel>
<bindings>
  <wsHttpBinding>
    <binding name="WSHttpBinding_ITransactionTest" closeTimeout="00:01:00"
      openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
      bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
      maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text"
      textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
      <reliableSession ordered="true" inactivityTimeout="00:10:00"
        enabled="false" />
      <security mode="Message">
        <transport clientCredentialType="Windows" proxyCredentialType="None"
          realm="" />
        <message clientCredentialType="Windows" negotiateServiceCredential="true"
          algorithmSuite="Default" />
      </security>
    </binding>
  </wsHttpBinding>
</bindings>
<client>
  <endpoint address="http://localhost:91/TransactionTest.svc" binding="wsHttpBinding"
    bindingConfiguration="WSHttpBinding_ITransactionTest" contract="TransactionTestService.ITransactionTest"          
    name="WSHttpBinding_ITransactionTest">        
  </endpoint>
</client>
 </system.serviceModel>
4

1 に答える 1