在标题中我加了一个大家都很熟悉的单词——Session,熟吧?玩过Web开发的朋友肯定在梦中都会见到她。
在Web中为什么要会话呢?毕竟每个用户在一个Web应用中可能不止进行一次操作,比如,某二手飞机交易网站,用户A登陆后,可能他会修改他的个人信息,他也有可能看好了一架二手飞机,打算入手,就把商品放到他的“购物车”中,这些过程中,都会产生许多与用户A相关的数据,这些数据只是对A有效,而当用户B登陆后,对于B,又会有他自己的数据,总的一句话就是,每个客户端在服务器上都有其的独立数据存储区,互不相干,就好像A和服务器在单独谈话一样,所以叫会话。
服务器独立维护着与每个客户端的会话。
示例代码
1、服务端
using System;
using System.ServiceModel;
using System.ServiceModel.Description;
namespace Server
{
class Program
{
//设置服务并启动
static void Main()
{
//服务基地址
Uri baseUri = new Uri("http://localhost:3000/Service");
//声明服务器主机
using (ServiceHost host = new ServiceHost(typeof(MyService), baseUri))
{
//添加绑定和终结点
//使用支持会话的绑定
NetTcpBinding binding = new NetTcpBinding();
binding.Security.Mode = SecurityMode.None;
host.AddServiceEndpoint(typeof (IService), binding, "net.tcp://localhost:3001/test");
//添加服务描述
host.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = true });
//启动服务
try
{
host.Open();
Console.WriteLine("服务已启动");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadKey();
//关闭服务
host.Close();
}
}
}
///<summary>
/// 定义服务协定
/// </summary>
[ServiceContract(SessionMode = SessionMode.Required)] //在定义服务协定的时候,设置SessionMode可以让服务要求客户端启用会话
public interface IService
{
[OperationContract(IsOneWay=true, IsInitiating=true,IsTerminating=false )]
void SetValue(int value);
[OperationContract]
int GetValue();
[OperationContract(IsInitiating = false, IsTerminating = true)]
void EndSession();
}
/// <summary>
/// 实现服务协定
/// </summary>
public class MyService : IService
{
int value = int.MinValue;
public MyService()
{
Console.WriteLine("-------------------------------");
Console.WriteLine("{0}-服务实例被实例化", DateTime.Now.ToLongTimeString());
}
// 在析构函数中也输出信息
~MyService()
{
Console.WriteLine("{0}-服务实例被释放。", DateTime.Now.ToLongTimeString());
Console.WriteLine("--------------------------------");
}
public void SetValue(int value)
{
this.value = value;
Console.WriteLine("------------------------");
Console.WriteLine("当前实例的哈希值:{0}", this.GetHashCode().ToString("x"));
Console.WriteLine("会话ID:{0}", OperationContext.Current.SessionId);
}
public int GetValue()
{
Console.WriteLine("------------------------");
Console.WriteLine("当前实例的哈希值:{0}", this.GetHashCode().ToString("x"));
Console.WriteLine("会话ID:{0}", OperationContext.Current.SessionId);
return this.value;
}
public void EndSession()
{
Console.WriteLine("会话结束。");
}
}
}
在定义服务协定的时候,设置SessionMode可以让服务要求客户端启用会话。
我们在使用OperationContractAttribute定义操作协定时,设置了两个属性:
a、IsInitiating:如果为真,则当调用该操作时就启用会话。
b、IsTerminating:如果为真,则说明当
调
用该操作时终结会话。所以,上面的例子是,当调用SetValue时开始会话,当调用EndSession方法后会话结束,在选择作为结束会话的方法时,最好使用返回值为void或者单向通讯(One Way)的方法,这样,不用等待客户结束才结束会话,因为单向通讯,不需要向客户端回复消息,因为它被调用后就可以马上终止会话了。
2、客户端
using System;
using System.Windows.Forms;
namespace Client
{
public partial class Form1 : Form
{
readonly WS.ServiceClient m_sc = null;
public Form1()
{
InitializeComponent();
m_sc = new WS.ServiceClient();
}
private void btnSet_Click(object sender, EventArgs e)
{
m_sc.SetValue(int.Parse(txtInPutValue.Text.Trim()));
}
private void btnGet_Click(object sender, EventArgs e)
{
txtGetValue.Text = m_sc.GetValue().ToString();
m_sc.EndSession(); //结束会话
}
}
}
运行效果