C#WebService 之Session之我见

        这几天一直在学习WebService的知识。正好现在有一个项目,需要在WebService实现如下接口: 

String Login(string username, string password) // 登录方法,返回值用来指名是不是登录成功,并且这个值在之后的接口中用来找到相对应的服务器上的session。

       因此WebService需要使用到Session,而网上大部分资料是说WebService是无状态的(StateLess),不怎么支持Session。因此难题出现了,首先在Web Serivice

放上声明如下Attribute,[WebMethod(EnableSession = true)],此为表示在webService上能使用Session。

      现在放上我写的WebService的例子代码(service端):

    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class Bussiness : System.Web.Services.WebService
    {
        
        [WebMethod(Description = "登录方法,返回值用来指名是不是登录成功,并且这个值在之后的接口中用来找到相对应的服务器上的session",
            EnableSession = true)]
        public string Login(string username, string password)
        {

            string state = "";
            if (IsLogin(username))
                state = "Logined";
            else
            {
                state = UserHelper.Login(username, password);
                if (state != null && state != "Failed")//
                {
                    //Session.Timeout = 1;
                    Session[state] = username;
                }
            }
            return state;

        }
        [WebMethod(Description = "判断是否登录",
          EnableSession = true)]
        private  bool  IsLogin(string name)
        {
            if (name != null &&  Session[name]!=null&& Session[name].ToString() == name)
                return true;
            return false;
        }

 客户端代码,我用的是soap方式写的,soap方式調用WebService的方法,我参看了《Programing Web Service with SOAP》。具体方式参看第三章的。我的客户端SOAP代码(我新建一个类)如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Serializ
  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值