Asp.net InstanceContextMode.PerSession不工作
我在wshttpbinding中使用InstanceContextMode.PerSession,如下代码所述,但它似乎不起作用,因为我的计数没有增加 请给我一些建议Asp.net InstanceContextMode.PerSession不工作,asp.net,wcf,c#-4.0,wcf-binding,Asp.net,Wcf,C# 4.0,Wcf Binding,我在wshttpbinding中使用InstanceContextMode.PerSession,如下代码所述,但它似乎不起作用,因为我的计数没有增加 请给我一些建议 [ServiceContract(SessionMode = SessionMode.Required)] public interface ITransService { [OperationContract(IsInitiating=true)] int Insert(int userId);
[ServiceContract(SessionMode = SessionMode.Required)]
public interface ITransService
{
[OperationContract(IsInitiating=true)]
int Insert(int userId);
[TransactionFlow(TransactionFlowOption.Allowed)]
[OperationContract]
int Update();
// TODO: Add your service operations here
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class TransService : ITransService
{
public int count = 0;
[OperationBehavior(TransactionScopeRequired = true)]
public int Insert(int userId)
{
count = count+1;
return count;
}
[OperationBehavior(TransactionScopeRequired = true)]
public int Update()
{
count = count++;
return count;
}
}
客户电话--
using (TransactionScope tranScope = new TransactionScope())
{
TransServiceClient obj = new TransServiceClient();
var a= obj.Insert(123);
var b = obj.Insert(123);
var c = obj.Update();
tranScope.Complete();
}
除了将接口标记为(SessionMode=SessionMode.Allowed)
,您还需要定义会话在接口上的哪些方法,例如:if Just Insert启动会话:[OperationContract(IsInitiating=true)]int Insert(int userId);有一个
(否,IsInitiating=true
是任何情况下的默认值)
根据扩展的评论,一种反复试验的方法最终产生了TransactionFlow
阻止SessionMode
正常工作的结果。我目前找不到这方面的确切参考资料——最接近日期的参考资料。从逻辑上讲,会话的持续时间可能比事务长得多(并且考虑到事务可能持有数据库和队列资源的锁),因此有一些逻辑
解决方案是删除TransactionFlow
编辑
对于那些希望同时使用
InstanceContextMode=InstanceContextMode.PerSession
和TransactionFlow
的人,我在下面添加并更新了我的服务参考,但没有运气[操作合同(IsInitiating=true)]IsInitiating进入界面-我在最初的帖子中犯了错误?您也可以尝试通过将SessionMode=SessionMode.Allowed
更改为SessionMode.Required
来强制执行。我已经在我的代码中更新了相同的内容并更新了客户端服务引用,但我仍然有相同的问题。我不知道我在这段代码中犯了什么错误。是的,你是对的+1,删除TransactioFlow后,这一切正常。但您能帮我找到一些链接或您的答案吗?为什么这是不兼容的?@user1721131经过一些尝试和错误,我最终能够让会话和事务流同时工作-。在MSDN,FWR中,这看起来没有很好的记录。