在 .NET ASMX Web 服务中获取会话 [英] Getting session in .NET ASMX web-service

查看:21
本文介绍了在 .NET ASMX Web 服务中获取会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 ASP.NET Web 应用程序旁边托管了一个 ASMX Web 服务.现在,我需要让用户会话进入 Web 服务.为了测试这一点,我做了这个简单的方法:

I have an ASMX webservice hosted alongside my ASP.NET web app. Now, I need to get the users session into the Webservice. To test this I made this simple method:

    [WebMethod(EnableSession = true)]
    public string checkSession()
    {
        return HttpContext.Current.Session["userid"].ToString();
    }

所以,首先我登录到我的网络应用程序,然后在浏览器中转到我的网络服务并在自动生成的测试页面上单击checkSession".我已经在 3 台电脑上测试过了.所有 3 个都可以在 webapp 上正常工作(因此正在创建会话等),其中 2 个在调用 webmethod 时返回 Session["userid"] 的值,但是最后一台计算机返回对象引用未设置为对象的实例",因为 Session 为空.

So, first I login to my web app, then in the browser goto my webservice and click "checkSession" on that auto generated test page. I have tested this on 3 computers. All 3 of those work fine with the webapp(so the sessions are being created etc), and 2 of those return the value of Session["userid"] on invoking the webmethod, however the last computer returns "Object reference not set to an instance of an object" because Session is null.

那么,这些计算机之间有什么区别?为什么我的 ASP.NET 应用程序可以在所有计算机上获取会话,而 Web 服务不能?

So, whats the difference between these computers and why can my ASP.NET app get the sessions on all computers but the webservice cant?

推荐答案

也许为时已晚,但你有没有试过这个:

maybe it's too late, but have you tried this:

[WebMethod(EnableSession = true)]
public string checkSession()
{
    return HttpContext.Current.Session.SessionID
}

这篇关于在 .NET ASMX Web 服务中获取会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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