我有一个客户端调用一对Web服务方法作为TransactionScope的一部分。第一种服务方法作为自己的TransactionScope的一部分做了几件事。如果第一个方法的内部TransactionScope未完成(即从不调用scope.Complete()),则无论服务方法实际返回什么,Web服务方法都会抛出ProtocolException。这是完整的异常消息(真实对象名称被混淆以匹配下面的示例):
System.ServiceModel.ProtocolException: The transaction under which this method call was executing was asynchronously aborted.
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.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)
at TransactionScopeExperiment.Service1WCF.IService1.ServiceRequest(ServiceRequest request)
at TransactionScopeExperiment.Service1WCF.Service1ServiceClient.ServiceMethod1(ServiceRequest request) in d:\vs-projects\TransactionScopeExperiment\TransactionScopeExperiment\Service References\Service1WCF\Reference.cs:line 1204
at TransactionScopeExperiment.Program.CallAddMethod() in d:\vs-projects\TransactionScopeExperiment\TransactionScopeExperiment\Program.cs:line 60
at TransactionScopeExperiment.Program.RunWithTransactionScope() in d:\vs-projects\TransactionScopeExperiment\TransactionScopeExperiment\Program.cs:line 81
我的服务看起来像这样
[ServiceBehavior(TransactionIsolationLevel = IsolationLevel.Serializable, TransactionTimeout = "00:01:00")]
[WebService(Namespace = "http://www.demo.com")]
public class Service1 : IService1
{
[TransactionFlow(TransactionFlowOption.Allowed)]
[OperationBehavior(TransactionScopeRequired = true)]
public ServiceResponse ServiceMethod1(ServiceRequest request)
{
try
{
using (var scope = new TransactionScope())
{
// first SQL insert
// second SQL insert fails and throws exception
scope.Complete();
}
}
catch (Exception ex)
{
return new ServiceResponse(error: ex.Message);
}
}
}
[ServiceContract]
public interface IService1
{
[OperationContract]
ServiceResponse ServiceMethod1(ServiceRequest request);
}
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_IService1" transactionFlow="true">
<security mode="None" />
</binding>
</wsHttpBinding>
</bindings>
为什么ServiceMethod1抛出ProtocolException而不是返回我的错误ServiceResponse对象?我如何获得我的回复对象?