简体   繁体   English

在调用ASMX Web服务时维护会话

[英]Maintain Session While Calling an ASMX Web Service

I am using a client app to connect to a web service for authenticated user only. 我正在使用客户端应用程序仅为经过身份验证的用户连接到Web服务。 Here is simplest example: 这是最简单的例子:
My web service code: 我的网络服务代码:

public class TestService : System.Web.Services.WebService
{
    [WebMethod(EnableSession = true)]
    public string WelcomeMsg()
    {
        return "Hello: " + Session["UserName"] + "! Welcome to our store.";
    }

    [WebMethod(EnableSession = true)]
    public void SetUserName(string sName)
    {
        Session["UserName"] = sName;
    }
}

Here is my code on client app (Windows form, not web base): 这是我在客户端应用程序上的代码(Windows窗体,而不是Web基础):

private void btnSetName_Click(object sender, EventArgs e)
{
    TestService.TestService ws = new TestService.TestService(); //Create a web service
    MainForm.m_ccSessionInfo = new System.Net.CookieContainer(); //Create a CookieContainer
    ws.CookieContainer = MainForm.m_ccSessionInfo; //Set CookieContainer of the web service
    ws.SetUserName(txtUserName.Text); //Set value of session 
    ws = null;
}

private void btnWelcome_Click(object sender, EventArgs e)
{
    TestService.TestService ws = new TestService.TestService(); //Create a web service
    ws.CookieContainer = MainForm.m_ccSessionInfo; //Set CookieContainer back
    string sWelcome = ws.WelcomeMsg(); //Get value from session property
    ws = null;

    System.Diagnostics.Debug.WriteLine(sWelcome); 
}

In my example MainForm.m_ccSessionInfo is a static member, I want to keep the session cookies value in this one! 在我的示例中,MainForm.m_ccSessionInfo是一个静态成员,我想在此会话中保留会话cookie值!
However, it don't work :( . The ws.WelcomeMsg() is always return an empty string. 但是,它不起作用:(。ws.WelcomeMsg()总是返回一个空字符串。

Oops, I think I've just found the solution for this problem. 哎呀,我想我刚刚找到了这个问题的解决方案。 The CookieContainer is created by server and must be kept at client app. CookieContainer由服务器创建,必须保存在客户端应用程序中。 On btnSetName_Click, I change 在btnSetName_Click上,我改变了

MainForm.m_ccSessionInfo = new System.Net.CookieContainer(); //Create a CookieContainer
ws.CookieContainer = MainForm.m_ccSessionInfo; //Set CookieContainer of the web service

into

ws.CookieContainer = new System.Net.CookieContainer(); //Create a CookieContainer
MainForm.m_ccSessionInfo = ws.CookieContainer; //Keep CookieContainer for later using

And it works well now! 它现在运作良好! Thanks you all. 谢谢大家。

try below 试试下面

private void btnWelcome_Click(object sender, EventArgs e)
{
    TestService.TestService ws = new TestService.TestService(); //Create a web service
    ws.SetUserName(txtUserName.Text); 
    string sWelcome = ws.WelcomeMsg();
    System.Diagnostics.Debug.WriteLine(sWelcome); 
}

When calling btnSetName_Click method and btnWelcome_Click click webs ervice consider your reqwests as new sessions. 当调用btnSetName_Click方法和btnWelcome_Click单击网络服务时,请将您的需求视为新会话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM