locked
how to call session in .asmx page ? RRS feed

  • Question

  • User-535616387 posted

    Hi guys,

    in general we can call session using this code : 

    string id;
    id = Session["LoginId"];

    i thought the coding above can work inside the .asmx page but actually no.

    my question is how to call session in .asmx page ?

    Thank You & Regards,

    Wibowo Wiwit

    Monday, August 7, 2017 7:29 AM

Answers

  • User541108374 posted

    Hi,

    it does't work.

    What error do you get?

    Also check if you get the needed cookie for the session in your browser. You can make use of the F12 tools of your browser of choice to look that up.

    Kris.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 7, 2017 8:03 AM
  • User475983607 posted

    You should not use Session in ASMX web services or any web service for that matter.  It goes against best practices and therefore you should rethink the design.  Can you explain why Session is required?

    Keep in  mind, Session uses state on the client (cookie) and the server (memory). The cookie unlocks the user's memory location. You need to pass the cookie on each and every request from the client.  This means the client must persist the data. 

    IMHO, if the client is already persisting the data then there is no reason to use Session as you are storing the same data in two different locations.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 7, 2017 11:29 AM
  • User-271186128 posted

    Hi Wibowo Wiwit,

    in general we can call session using this code : 

    string id;
    id = Session["LoginId"];

    i thought the coding above can work inside the .asmx page but actually no.

    my question is how to call session in .asmx page ?

    According to your code, I create a sample using the following code, it seems that the web service (.asmx page) and the web page should in the same project.

    Code in web page:

        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
        <script type="text/javascript">
            $(function () {
                $("#Button1").click(function () {
                    $.ajax({
                        type: "POST",
                        url: "../Web_Service/WebService1.asmx/SayHello",
                        data: '{name: "' + "AAA" + '"}',
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (response) {
                            $("#div_MethondResult").html(response.d);
                        },
                        failure: function (response) {
                            $("#div_MethondResult").html(response.d);
                        }
                    });
                    return false;
                });
            });
        </script>
    
                <asp:Button ID="Button1" runat="server" Text="Button" />
                <div id="div_MethondResult">
                </div>

    Code in web page code behind:

            protected void Page_Load(object sender, EventArgs e)
            {
                Session["Date"] = DateTime.Now.ToString();
            }

    Code in web Service:

        [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 WebService1 : System.Web.Services.WebService
        {
            [WebMethod(EnableSession =true)]
            public string SayHello(string name)
            {
                string datetime = "Null";
                if (Session["Date"] != null)
                    datetime = Session["Date"].ToString();
    
                return string.Format("Hello {0}, Login time: {1}", name, datetime);
            }
        }

    The screenshot as below:

    Besides, if you want to share the session value from different project, I suggest you could try to use DataBase.

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 10, 2017 8:38 AM

All replies

  • User541108374 posted

    Hi,

    should be

    id = HttpContext.Current.Session["LoginId"];

    Kris.

    Monday, August 7, 2017 7:40 AM
  • User-535616387 posted

    Hi Kris,

    thank for you answer.

    it does't work.

    i also add  [WebMethod(EnableSession = true)] 

    but does't work to get session.

    Monday, August 7, 2017 7:56 AM
  • User541108374 posted

    Hi,

    it does't work.

    What error do you get?

    Also check if you get the needed cookie for the session in your browser. You can make use of the F12 tools of your browser of choice to look that up.

    Kris.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 7, 2017 8:03 AM
  • User475983607 posted

    You should not use Session in ASMX web services or any web service for that matter.  It goes against best practices and therefore you should rethink the design.  Can you explain why Session is required?

    Keep in  mind, Session uses state on the client (cookie) and the server (memory). The cookie unlocks the user's memory location. You need to pass the cookie on each and every request from the client.  This means the client must persist the data. 

    IMHO, if the client is already persisting the data then there is no reason to use Session as you are storing the same data in two different locations.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 7, 2017 11:29 AM
  • User-271186128 posted

    Hi Wibowo Wiwit,

    in general we can call session using this code : 

    string id;
    id = Session["LoginId"];

    i thought the coding above can work inside the .asmx page but actually no.

    my question is how to call session in .asmx page ?

    According to your code, I create a sample using the following code, it seems that the web service (.asmx page) and the web page should in the same project.

    Code in web page:

        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
        <script type="text/javascript">
            $(function () {
                $("#Button1").click(function () {
                    $.ajax({
                        type: "POST",
                        url: "../Web_Service/WebService1.asmx/SayHello",
                        data: '{name: "' + "AAA" + '"}',
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (response) {
                            $("#div_MethondResult").html(response.d);
                        },
                        failure: function (response) {
                            $("#div_MethondResult").html(response.d);
                        }
                    });
                    return false;
                });
            });
        </script>
    
                <asp:Button ID="Button1" runat="server" Text="Button" />
                <div id="div_MethondResult">
                </div>

    Code in web page code behind:

            protected void Page_Load(object sender, EventArgs e)
            {
                Session["Date"] = DateTime.Now.ToString();
            }

    Code in web Service:

        [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 WebService1 : System.Web.Services.WebService
        {
            [WebMethod(EnableSession =true)]
            public string SayHello(string name)
            {
                string datetime = "Null";
                if (Session["Date"] != null)
                    datetime = Session["Date"].ToString();
    
                return string.Format("Hello {0}, Login time: {1}", name, datetime);
            }
        }

    The screenshot as below:

    Besides, if you want to share the session value from different project, I suggest you could try to use DataBase.

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 10, 2017 8:38 AM