调用Web服务时,Session得到null(一段时间) [英] Session got null (some time) when call to Web services

查看:60
本文介绍了调用Web服务时,Session得到null(一段时间)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网络服务中,我需要从会话变量中获取一个值。在我的Web服务的构造函数中,我得到的这个值就像





 constructor() //   Web服务的构造函数 
{
Person p =(Person)Session [< span class =code-string> xyz];
string name = p.Name;
string 地址= p.Address
}





我的java脚本就像下面那样



  function  CallWebservice() 
{
MyWebservice.GetValueFromDb(onsucess,onfailure)
}





它工作得很好但是有一段时间我把会话作为null,我在 web.config 中有会话状态,其默认时间为60分钟。



但是在60分钟(并非所有时间)之前会话变为空,只有当我从java脚本调用web服务时才会发生这种情况

解决方案

< blockquote>我同意ryanb31的回答。

首先,您必须在您的网络服务中启用会话。

您可以通过在您的网络服务中添加以下行来实现这一目标

[WebMethod(EnableSession = true)]



http://msdn.microsoft.com/en-us/library/aa480509.aspx [ ^ ]

http://msdn.microsoft.com/en-us/library/system.web.services.webmethodattribute .enablesession(v = vs.90).aspx [ ^ ]



并避免会话超时,你可以创建一个非功能性的Web服务,并使用javascript setInter继续调用它val方法。

 [WebMethod(true)] 
public void KeepAlive()
{
//什么都不做
}



  //  保持会话活动 
window .setInterval( function (){
// 调用KeepAlive网络服务
}, 60000 );


In my web service I need to get one value from session variable. In the constructor of my web service I am getting this value like


constructor()  // constructor of web service
{
  Person p = (Person)Session["xyz"];
  string name = p.Name;
  string Address = p.Address
}



my java script is like bellow

function CallWebservice()
{
MyWebservice.GetValueFromDb(onsucess,onfailure)
}



It works perfectly fine but some time i got the session as null, i have the session state in web.config which has default time out as 60 minutes.

but before 60 minutes(not all the time) the session is getting null, this things happens only when i am calling the webservice from java script

解决方案

Im agreed with ryanb31 answer.
First you have to enable the session in your web service.
you can do that by adding following line to your web service
[WebMethod(EnableSession = true)]

http://msdn.microsoft.com/en-us/library/aa480509.aspx[^]
http://msdn.microsoft.com/en-us/library/system.web.services.webmethodattribute.enablesession(v=vs.90).aspx[^]

and avoid the session time out, you can create a non functional web service and keep calling to it using the javascript setInterval method.

[WebMethod(true)]
        public void KeepAlive()
        {
            //do nothing
        }


//to keep the session alive
window.setInterval(function(){
  //call KeepAlive web service 
}, 60000);


这篇关于调用Web服务时,Session得到null(一段时间)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆