• 3.WCF会话状态


    项目结构:

    Rhythmk.Contracts //契约

    Rhythmk.Services //服务

    Rhythmk.WCFSvc // 服务寄宿

    Rhythmk.test //测试

    -----------------------------------------------------------------

    说明:通过Session 去保证对象的一致性,通过绑定 binding="wsHttpBinding" 实现会话状态

    1.Rhythmk.Contracts //契约

    using System.ServiceModel;

    /// SessionMode - 获取或设置是否允许、不允许或要求会话
    /// SessionMode.Allowed - 指定当传入绑定支持会话时,协定也支持会话(默认值)
    /// SessionMode.Required - 指定协定需要会话绑定。如果绑定并未配置为支持会话,则将引发异常
    /// SessionMode.NotAllowed - 指定协定永不支持启动会话的绑定
    [ServiceContract( SessionMode=SessionMode.Required, Namespace="http://wwww.wangkun.com")]

    public interface ICalculate
    {

    /// IsInitiating - 获取或设置一个值,该值指示方法是否实现可在服务器上启动会话(如果存在会话)的操作。
    /// IsTerminating - 获取或设置一个值,该值指示服务操作在发送答复消息(如果存在)后,是否会导致服务器关闭会话。
    [OperationContract(IsInitiating = true, IsTerminating = false)]
    string GetSessionId();

    [OperationContract(IsInitiating
    = true, IsTerminating = false)]
    void InitNum();

    [OperationContract(IsInitiating
    = true, IsTerminating = false)]
    void AddNum(int x);

    [OperationContract(IsInitiating
    = true, IsTerminating = false)]
    int GetNum();


    }

    2.Rhythmk.Services //服务

    using Rhythmk.Contracts;
    using System.ServiceModel.Activation;
    using System.ServiceModel;
    /// <summary>
    /// 计算器
    /// </summary>
    /// /// <summary>
    /// 演示会话状态的接口
    /// </summary>
    /// <remarks>
    /// InstanceContextMode - 获取或设置指示新服务对象何时创建的值。
    /// InstanceContextMode.PerSession - 为每个会话创建一个新的 System.ServiceModel.InstanceContext 对象。
    /// InstanceContextMode 的默认值为 InstanceContextMode.PerSession
    /// </remarks>
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
    [AspNetCompatibilityRequirements( RequirementsMode
    =AspNetCompatibilityRequirementsMode.Required)]
    public class Calculate:ICalculate
    {

    #region ICalculate 成员

    private int _Num;

    public string GetSessionId()
    {
    return OperationContext.Current.SessionId;
    }

    public void InitNum()
    {
    _Num
    = 0;
    }

    public void AddNum(int x)
    {
    _Num
    = _Num + x;
    }

    public int GetNum()
    {
    return _Num;
    }

    #endregion
    }

    3.Rhythmk.WCFSvc // 服务寄宿

    <%@ ServiceHost Language="C#" Service="Rhythmk.Services.Calculate" %>

    配置:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
    <system.serviceModel>
    <behaviors>
    <serviceBehaviors>
    <behavior name="metaBehavior">
    <serviceMetadata httpGetEnabled="true" />
    </behavior>
    </serviceBehaviors>
    </behaviors>
    <services>
    <service name="Rhythmk.Services.Calculate" behaviorConfiguration="metaBehavior" >
    <endpoint address=""
    binding
    ="wsHttpBinding" bindingConfiguration="" contract="Rhythmk.Contracts.ICalculate" >
    </endpoint>
    <host>
    <baseAddresses>
    <add baseAddress="http://localhost:3654/One/wsCalculate.svc"/>
    </baseAddresses>
    </host>
    </service>
    </services>
    </system.serviceModel>
    </configuration>

    4.Rhythmk.test //测试

    using System.ServiceModel;

    namespace Rhythmk.test
    {
    public partial class _Default : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {

    if (!Page.IsPostBack)
    {
    if (Session["proxy"] == null)
    {
    Session["proxy"] = new MyService.CalculateClient();
    }
    MyService.ICalculate proxy = Session["proxy"] as MyService.CalculateClient;
    proxy.InitNum();

    }
    }

    protected void btnSet_Click(object sender, EventArgs e)
    {
    MyService.ICalculate proxy = Session["proxy"] as MyService.CalculateClient;
    proxy.AddNum(10);

    }

    protected void btnGet_Click(object sender, EventArgs e)
    {
    MyService.ICalculate proxy = Session["proxy"] as MyService.CalculateClient;
    Response.Write(proxy.GetSessionId());
    Response.Write(proxy.GetNum().ToString());
    }
    }
    }

    一只站在树上的鸟儿,从来不会害怕树枝会断裂,因为它相信的不是树枝,而是它自己的翅膀。与其每天担心未来,不如努力做好现在。
  • 相关阅读:
    gulp之压缩合并MD5清空替换加前缀以及自动编译自动刷新浏览器大全
    HTML5之文件API
    Angular2之路由学习笔记
    nodejs之主机不能访问到虚拟机的web服务器
    学习CSS3动画(animation)
    jQuery之ajax错误调试分析
    Angular2之管道学习笔记
    css3之3D魔方动画(小白版)
    关于二维网格导入autodyn的问题
    两个橡胶球自由落体撞击弹性板
  • 原文地址:https://www.cnblogs.com/rhythmK/p/2063217.html
Copyright © 2020-2023  润新知