在尝试回答question时,我自己或OP都无法同时构建同时启用Session
和TransactionFlow
的WCF服务,这是唯一的解决方案可以提供的是删除TransactionFlow
以使WCF会话实例化工作。
虽然逻辑上会话持续时间可以想象会超过事务(请记住TransactionFlow
可能意味着事务持有宝贵的服务器资源,如数据库锁,消息队列和事务文件系统),我找不到明确的参考无论是确认还是拒绝同时实施这两个特征的可能性。
所以我的问题是:如何让InstanceContextMode=InstanceContextMode.PerSession
和TransactionFlow
同时在WCF中工作?
尝试了什么:
ITransactionService.cs
[ServiceContract(SessionMode = SessionMode.Required)]
public interface ITransactionService
{
[OperationContract(IsInitiating = true, IsTerminating = false)]
[TransactionFlow(TransactionFlowOption.Mandatory)]
int Start(int userId);
[OperationContract(IsInitiating = false, IsTerminating = true)]
[TransactionFlow(TransactionFlowOption.Mandatory)]
int Finish(int userId);
}
TransactionService.svc.cs
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]
public class TransactionService : ITransactionService
{
public int count = 0;
[OperationBehavior(TransactionScopeRequired = true)]
public int Start(int userId)
{
Debug.Assert(Transaction.Current != null, "No Transaction!");
count = count + 1;
return count;
}
[OperationBehavior(TransactionScopeRequired = true)]
public int Finish(int userId)
{
Debug.Assert(Transaction.Current != null, "No Transaction!");
Debug.Assert(count > 0, "No Session!");
return count;
}
Client.cs
using (var ts = new TransactionScope())
using (var svc = new TransactionServiceClient())
{
svc.Start(0);
svc.Finish(0);
ts.Complete();
}
服务web.config
<services>
<service name="WcfLibrary1.TransactionService">
<endpoint address="" binding="wsHttpBinding"
contract="WcfLibrary1.ITransactionService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</service>
</services>
<bindings>
<wsHttpBinding>
<binding transactionFlow="true"/>
</wsHttpBinding>
</bindings>
<protocolMapping>
<remove scheme="http" />
<add scheme="http" binding="wsHttpBinding" bindingConfiguration="transactionFlowEnabled"/>
</protocolMapping>
客户端web.config
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_ITransactionService" transactionFlow="true" />
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:65171/TransactionService.svc"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ITransactionService"
contract="TransactionService.ITransactionService" name="WSHttpBinding_ITransactionService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
答案 0 :(得分:0)
经过一些试验和错误后,似乎有可能。问题似乎是TransactionFlow
applies the following default settings的默认行为:
ReleaseServiceInstanceOnTransactionComplete = true
TransactionAutoComplete = true
当相互结合使用时,将导致服务实例被释放,从而破坏现有会话中保留的任何状态。
禁用其中任何一个选项将使会话期间的服务实例保持活动状态,从而允许同时使用TransactionFlow
和Session
:
即。在ServiceContract
上:
[ServiceContract(SessionMode = SessionMode.Required)]
public interface ITransactionService
{
[OperationContract(IsInitiating = true, IsTerminating = false)]
[TransactionFlow(TransactionFlowOption.Mandatory)]
int Start(int userId);
[OperationContract(IsInitiating = false, IsTerminating = true)]
[TransactionFlow(TransactionFlowOption.Mandatory)]
int Finish(int userId);
}
在Service类上更改为:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession,
ReleaseServiceInstanceOnTransactionComplete = false)]
public class TransactionService : ITransactionService
{
或者,在操作上:
[OperationBehavior(TransactionScopeRequired = true,
TransactionAutoComplete = false)]
public int Start(int userId)
{