当前位置:编程学习 > C#/ASP.NET >>

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端):

view plaincopy to clipboardprint?/// <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; 
    } 
    /// <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代码(我新建一个类)如下:

view plaincopy to clipboardprint?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.Serialization; 
 
namespace Test 

    [WebServiceBinding(Name = "BusServiceSoap", Namespace = "http://tempuri.org/")] 
    public class BusService : SoapHttpClientProtocol 
    { 
        public BusService() 
      

补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,