【问题标题】:Enable session with wsHttpBinding in wcf在 wcf 中启用与 wsHttpBinding 的会话
【发布时间】:2011-05-27 17:00:18
【问题描述】:

我已经写了这段代码:

界面:

public interface IService1
{
    [OperationContract]
    string Welcome(string fullName);

    [OperationContract]
    string Goodbye();

    [OperationContract]
    string GetSessionID();

    [OperationContract]
    CompositeType GetDataUsingDataContract(CompositeType composite);
}

服务:

[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]
public class Service1 : IService1
{
    private string UserFullName { get; set; }

    public string GetSessionID()
    {
        var sessionId = OperationContext.Current.SessionId;
        return sessionId.ToString();
    }

    public string Welcome(string fullName) 
    { 
        UserFullName = fullName ?? "Guest"; return string.Format("Welcome back, {0}!", UserFullName); 
    }    

    public string Goodbye() 
    {
        return string.Format("Come back soon, {0}!", UserFullName ?? "Guest"); 
    }

    public CompositeType GetDataUsingDataContract(CompositeType composite)
    {
        if (composite == null)
        {
            throw new ArgumentNullException("composite");
        }
        if (composite.BoolValue)
        {
            composite.StringValue += "Suffix";
        }
        return composite;
    }
}

网络配置:

为什么 UserFullName 总是为空?

【问题讨论】:

  • 请不要使用特定于版本的标签,例如“c#-4.0 c#-3.0”,除非您的问题是针对这些版本的。

标签: c# wcf wshttpbinding


【解决方案1】:

InstanceContextMode.PerCall 更改为 PerSession

在您的示例中,每次调用都会创建一个服务实例。

【讨论】:

  • 好的,要进行会话,您需要使用消息安全或开启可靠会话。另一种解决方案是使用此创建自定义绑定 -
  • 可靠会话已开启但无法正常工作。自定义绑定是什么意思?
  • 您是否尝试开启 Message Security?对于自定义绑定看@这个答案 - stackoverflow.com/questions/4396961/…
  • 好的,wsHttpBinding 不支持会话是真的吗?
  • 应该的。您尝试过消息安全吗?
猜你喜欢
  • 2012-09-16
  • 1970-01-01
  • 1970-01-01
  • 2011-09-25
  • 2011-06-13
  • 1970-01-01
  • 2010-11-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多