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