【问题标题】:InstanceContextMode.PerSession not workingInstanceContextMode.PerSession 不工作
【发布时间】:2015-02-22 19:12:05
【问题描述】:

我在 wshttpbinding 中使用 InstanceContextMode.PerSession,如下面的代码所述,但它似乎不起作用,因为我的计数没有增加。

请指教。

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

【问题讨论】:

    标签: asp.net wcf c#-4.0 wcf-binding


    【解决方案1】:

    除了将接口标记为(SessionMode = SessionMode.Allowed)之外,您还需要定义会话initiate接口上的哪些方法,如下所示:例如:如果 Just Insert 启动会话: [OperationContract(IsInitiating=true)] int Insert(int userId);有一个example here

    (不,IsInitiating = true 无论如何都是默认值)

    根据扩展评论,试错法最终导致TransactionFlow 阻止SessionMode 正常工作。我目前无法找到关于此的明确参考 - 最接近日期的 is this。从逻辑上讲,会话可以持续比事务更长的持续时间(并且鉴于事务可以锁定数据库和队列资源),这是有一些逻辑的。

    解决方案是删除TransactionFlow

    编辑

    对于那些希望将InstanceContextMode = InstanceContextMode.PerSessionTransactionFlow 结合起来的用户,see here

    【讨论】:

    • 我在下面添加并更新了我的服务参考,但没有运气 [OperationContract(IsInitiating=true)]
    • IsInitiating 进入界面 - 我在最初的帖子中犯了错误?您也可以尝试通过将SessionMode = SessionMode.Allowed 更改为SessionMode.Required 来强制它
    • 我在我的代码中更新了相同内容并更新了客户端服务参考,但我仍然遇到同样的问题。我不知道我在此代码中犯了什么错误。
    • 是的,你是对的 +1,在删除 TransactioFlow 后它工作正常。但是你能帮我提供一些链接或回答为什么这不兼容吗?
    • @user1721131 经过反复试验,我最终能够让会话和事务流同时工作 - here。这在 MSDN、FWR 中看起来没有很好的记录。
    猜你喜欢
    • 2011-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-10
    • 1970-01-01
    • 2019-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多