C# 为什么我的wcf应用程序不能使用和保存会话?
这是我的wcf应用程序的接口和类C# 为什么我的wcf应用程序不能使用和保存会话?,c#,asp.net,wcf,C#,Asp.net,Wcf,这是我的wcf应用程序的接口和类 string sessionId=OperationContext.Current.sessionId 但是我发现sessionId总是空的,所以我试着像这样成功地使用和保存asp.net会话 HttpContext.Current.Session.Add("testSession", "testSession"); 在一个方法中。在另一个方法中,我尝试获取testSession,但它是空的 var myTestSession= HttpContext.Cur
string sessionId=OperationContext.Current.sessionId
但是我发现sessionId总是空的,所以我试着像这样成功地使用和保存asp.net会话
HttpContext.Current.Session.Add("testSession", "testSession");
在一个方法中。在另一个方法中,我尝试获取testSession
,但它是空的
var myTestSession= HttpContext.Current.Session["testSession"];
我的代码怎么了?为什么SessionId总是空的
我的wcf界面
[ServiceContract(SessionMode = SessionMode.Allowed)]
public interface ITestSession
{
[OperationContract]
string SetSession(string sessionvalue);
[OperationContract]
string GetSession();
}
我的wcf课程
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
//[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class TestSession: ITestSession
{
public string SetSession(string sessionvalue)
{
HttpContext.Current.Session.Add("testSession", sessionvalue);
return sessionvalue;
}
public string GetSession(string sessionvalue)
{
var testValue = HttpContext.Current.Session["testSession"];
if (testValue ==null)
return "null value";
else
return testValue;
}
}
}
我已尝试添加或删除类似
的配置,但仍然获得空会话。如何修复它
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
</system.serviceModel>
1)要在WCF中管理会话,请考虑管理您服务的InstanceContextMode,查看以下内容
2) 如果我知道您有一个方法调用,它将在授权调用其他方法之前开始您的会话,如果您需要,您可以使用IsTerminating和IsInitiating方法属性,如下所述:
它们是显式打开和关闭的
HttpContext
根本不适用,因为WCF不一定基于HTTP。WCF在设计上是无状态的。如果可以的话,你应该避免参加会议。但是如果你真的必须这样做,至少不要尝试使用ASP.NET会话。@克里斯,你能告诉我为什么不尝试使用ASP.NET会话吗?我在配置文件中使用了aspNetCompatibilityEnabled=“true”
。本文档描述了WCF和ASP.NET会话之间的区别:我确实需要使用会话,无论是wcf会话还是asp.net会话。但对我来说都是失败的。我仍然不知道为什么OperationContext.Current.SessionId
在wcf会话中为空?我相信您已彻底阅读了文档?只是为了确保:WCF会话必须由客户端显式初始化。