【发布时间】:2009-11-09 17:56:52
【问题描述】:
我的 WCF 服务遇到了奇怪的问题。相同的代码运行良好,直到最近我们添加了更多的 OperationContracts(Web 方法)。
我们有通用的 3 层架构。
- DAL (WCF)
- BLL
- 网页界面
这是我的快速示例代码:
DAL (WCF):
[ServiceContract]
interface IPerson
{
[OperationContract]
[TransactionFlow(TransactionFlowOption.Mandatory)]
int AddPerson(Person p);
}
// AddPerson is implemented in the service
[OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
public int AddPerson(Person p)
{
// LINQ DataContext stuff goes here
}
BLL:
public class EmployeeBLL
{
public void AddNewEmployee(Person p)
{
using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required))
{
try
{
PersonClient perClient = new PersonClient();
int personId = perClient.AddPerson(p);
ts.Complete();
}
catch (Exception ex)
{
// Log exception
}
finally
{
ts.Dispose();
}
}
perClient.Close();
}
}
在网页界面中的使用:
EmployeeBLL empBLL = new EmployeeBLL ()
empBLL.AddNewEmployee(person);
我收到“服务操作需要处理事务。”在我的 BLL 中尝试在服务中调用 AddPerson 方法时。在 web.config 中启用跟踪后运气不佳。
详细的堆栈跟踪:
Server stack trace:
at System.ServiceModel.Channels.ServiceChannel.ThrowIfFaultUnderstood(Message reply, MessageFault fault, String action, MessageVersion version, FaultConverter faultConverter)
at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
客户端配置:
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_IEmployee" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
bypassProxyOnLocal="false" transactionFlow="true" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="5000000"
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" establishSecurityContext="true" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:2882/Test.svc"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IEmployee"
contract="Test.IEmployee" name="WSHttpBinding_IEmployee">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
</system.serviceModel>
【问题讨论】:
-
您使用的是什么绑定? NetTcpBinding、NetNamedPipeBinding、WSHttpBinding、WSDualHttpBinding 和 WSFederationHttpBinding 支持事务。
-
只是出于好奇,当您设置reliableSession enabled=true而不是false时,这是否会失败。我意识到事务不需要可靠的消息传递,我只想确认它仍然会引发异常。
-
我没试过,但可以试一试。基于以下我也不认为交易需要可靠的会话。 msdn.microsoft.com/en-us/library/ms733136.aspx
标签: wcf