问题描述
我M在使用的WSHttpBinding作为InstanceContextMode.PerSession低于code提及,但似乎没有工作,因为我的计数不增加。
请咨询。
[的ServiceContract(SessionMode = SessionMode.Required)
公共接口ITransService
{
[OperationContract的(IsInitiating =真)]
INT插入(INT用户id); [TransactionFlow(TransactionFlowOption.Allowed)
[OperationContract的]
INT更新(); // TODO:添加您的业务运营在这里
}[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)
公共类TransService:ITransService
{
公众诠释计数= 0;
[OperationBehavior(TransactionScopeRequired =真)]
公众诠释插入(INT用户id)
{
数=计+ 1;
返回计数;
} [OperationBehavior(TransactionScopeRequired =真)]
公众诠释更新()
{
数=计++;
返回计数;
}
}
客户端调用---
使用(TransactionScope的tranScope =新的TransactionScope())
{
TransServiceClient的obj =新TransServiceClient();
VAR一个= obj.Insert(123);
变种B = obj.Insert(123);
变种C = obj.Update(); tranScope.Complete();
}
的(不, IsInitiating = TRUE
是在任何情况下默认)的
根据扩展解说,试错法,最终得出了 TransactionFlow
prevents SessionMode
从正常工作。我目前不能找到这个权威的参考 - 最接近日期<一个href=\"https://books.google.co.za/books?id=PvNrurEhmiEC&pg=PA362&lpg=PA362&dq=WCF+incompatible+SessionMode+TransactionFlow&source=bl&ots=CixwJwKZQx&sig=SsxL7kNeWdmZfqqhL4lPpNl3zxw&hl=en&sa=X&ei=ngmbVN__McX1UL_0g7AL&ved=0CB8Q6AEwAA#v=onepage&q=WCF%20incompatible%20SessionMode%20TransactionFlow&f=false\"相对=nofollow>是这样的。在逻辑上,会话可以持续超过交易远较长时间(并考虑到交易可容纳数据库和队列资源锁)有一定的逻辑。
解决方法是删除 TransactionFlow
。
修改
对于那些希望同时结合 InstanceContextMode = InstanceContextMode.PerSession
和 TransactionFlow
,see这里
I m using InstanceContextMode.PerSession in wshttpbinding as mentioned in below code but it seems not working,since my count is not increasing.
Please advice.
[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;
}
}
Client Call ---
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();
}
(No, IsInitiating = true
is the default in any event)
As per the extended commentary, a trial and error approach eventually yielded that TransactionFlow
prevents SessionMode
from working correctly. I cannot currently find a definitive reference on this - the closest to date is this. Logically, sessions can last for far longer durations than Transactions (and given that Transactions could hold locks on database and queue resources) there is some logic.
The solution was to remove TransactionFlow
.
Edit
For those looking to combine both InstanceContextMode = InstanceContextMode.PerSession
and TransactionFlow
, see here
这篇关于InstanceContextMode.PerSession不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!