服务引用 - 使用EnableSession = true会话为空静态值工作正常

时间:2014-03-26 17:13:29

标签: c# session web service reference

问题:作为服务引用添加的Web服务返回静态字符串,但是当我从Session获取值时返回null。我正在使用[WebMethod(EnableSession=true)]启用会话状态。使用URL的Web服务方法测试工作正常并返回正确的结果。但是当我尝试使用服务引用在不同的项目中获取值时,它不会返回会话值。

这是一个细节,我创建了一个简单的Web服务,它在一个ASP.NET C#3.5项目中将会话值作为字符串返回。网络服务代码如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

/// <summary>
/// Summary description for myWebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 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 myWebService : System.Web.Services.WebService {

    public myWebService () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod(EnableSession=true)]
    public string HelloWorld() {
        if (Session["UserName"] != null)
        {
            return Session["UserName"].ToString();
        }
        else
        {
            return "null ";
        }
    }

}

使用URL,它会返回正确的数据

http://localhost:49942/MyProject/myWebService.asmx/HelloWorld

这是它返回的XML

<?xml version="1.0" encoding="UTF-8"?>
<string xmlns="http://tempuri.org/">myusername@gmail.com</string>

在另一个项目(ASP.NET 3.5 C#)中,我已将此Web服务添加为服务引用。当我调用它的方法时,它返回“null”

ServiceReference1.myWebServiceSoapClient sr = new ServiceReference1.myWebServiceSoapClient();
                string strValue = sr.HelloWorld();

如果我返回一个静态字符串值,则返回该字符串,但Session显示为null。

我确信我错过了一些东西。你能帮忙吗?

0 个答案:

没有答案