InstanceContextMode.PerSession不工作 [英] InstanceContextMode.PerSession not working

查看:113
本文介绍了InstanceContextMode.PerSession不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我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();
                }


解决方案

除了标记接口(SessionMode = SessionMode.Allowed),你'还需要会定义接口上哪些方法<一href=\"http://msdn.microsoft.com/en-us/library/system.servicemodel.operationcontractattribute.isinitiating(v=vs.110).aspx\"相对=nofollow>启动会话,就像这样:如如果只是将启动会话:[OperationContract的(IsInitiating = TRUE)INT插入(INT用户id);这里有一个例如线

(不, 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 ,<一个href=\"http://stackoverflow.com/questions/27649719/how-to-simultaneously-implement-session-and-transaction-flow-in-wcf\">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();
                }

解决方案

In addition to marking the interface as (SessionMode = SessionMode.Allowed), you'll also need to define which methods on the interface initiate the session, like so: e.g. if Just Insert initiates the session: [OperationContract(IsInitiating=true)] int Insert(int userId); There's an example here

(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不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆