如何解决在wcf中使用wsHttpBinding启用会话
| 我已编写此代码: 接口: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,UserFullName ?? \"Guest\");
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException(\"composite\");
}
if (composite.BoolValue)
{
composite.StringValue += \"Suffix\";
}
return composite;
}
}
webconfig:
为什么UserFullName始终为null?
解决方法
将InstanceContextMode.PerCall更改为PerSession。
在您的示例中,每次调用都会创建一个服务实例。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。