在 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, {0}!", UserFullName ?? "Guest");
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
}
webconfig:
为什么 UserFullName 始终为空?
I've written this code:
interface:
public interface IService1
{
[OperationContract]
string Welcome(string fullName);
[OperationContract]
string Goodbye();
[OperationContract]
string GetSessionID();
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
}
service:
[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;
}
}
webconfig:
Why is UserFullName always null?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将InstanceContextMode.PerCall更改为PerSession。
在您的示例中,每次调用都会创建一个服务实例。
Change InstanceContextMode.PerCall to PerSession.
In your example an instance of the service is created every call.