.net WCF事务流未在成功时提交

.net WCF事务流未在成功时提交,.net,sql-server,wcf,transactions,.net,Sql Server,Wcf,Transactions,我一直在尝试在WCF中实现事务流。当我执行客户机代码时,我可以看到事务正在从客户机传播到服务,因为我看到记录被锁定在db中(我在SQL server中与(nolock)一起使用以查看事务是否正在发生)。然而,我的客户端代码成功完成,但事务被回滚,我是否错过了一些东西 下面是代码片段 服务等级 [ServiceBehavior( TransactionIsolationLevel = System.Transactions.IsolationLevel.ReadCommitted, Transa

我一直在尝试在WCF中实现事务流。当我执行客户机代码时,我可以看到事务正在从客户机传播到服务,因为我看到记录被锁定在db中(我在SQL server中与(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();
    }
}
}

下面是我的Web.config

<bindings>
  <wsHttpBinding>
    <binding name="wsHttpBinding_ITransactionTest" transactionFlow="true"/>
  </wsHttpBinding>
</bindings>
客户端应用程序配置

 <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>

在服务端,您有
[OperationBehavior(TransactionAutoComplete=false)]
,这意味着服务必须明确投票提交,否则将发生回滚。你有两个选择。在服务方:

  • 将行为更改为
    TransactionAutoComplete=true
    ,除非出现异常,否则将自动投票提交事务
  • 在服务方法中,TransactionScope(TransactionScopeOption.Required)将获得环境事务范围,您可以在其上调用
    Complete()

    如果我指定TransactionAutoComplete=False,则客户端事务不会传播到服务。我不明白。您说“事务正在从客户端传播到服务”,并且您的发布代码设置TransactionAutoComplete=False。。。没有任何变化,也没有传播?
    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>