【发布时间】:2011-05-17 15:52:30
【问题描述】:
我最近将我的 ASMX 服务转换为 WCF 以利用 Sessions。
我已经查看了 MSDN 上的一些 Sessions 教程,但仍然不确定我的代码设置是否正确。到目前为止它有效,但我不确定为什么。
我明白了
[ServiceContract
(SessionMode = SessionMode.Required,
Namespace = "http://smartshopservice.org")]
那我有
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class SmartShopService : SmartShopInterface
{
private static Shopper sh = new Shopper();
private List<aaa> data = new List<aaa>();
我的问题的第一部分是 Shopper 是否是我的“全局”变量。我希望它始终存在,而其他所有内容(例如“数据”)都会在每个会话中实例化。我也适当地设置了 WebConfig。我还有什么需要做的吗?
我的第二个问题是如何关闭会话,然后刷新所有这些变量?我的客户端现在是一个 WebClient,它的通信方式是这样的:
static GarfieldService.SmartShopInterfaceClient service
= new GarfieldService.SmartShopInterfaceClient();
好像行得通,我在 ASP.NET 页面的 onbody="" 中调用了一个看起来像这样的函数:
[WebMethod]
public static bool Connect() {
try {
if (service.State
== System.ServiceModel.CommunicationState.Closed) {
service.Open();
return true;
}
else if (service.State
== System.ServiceModel.CommunicationState.Created) {
service.Open();
return true;
}
}
catch {}
return false;
}
所以我可以连接,但如何断开或关闭会话?
【问题讨论】: