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.
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.
[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
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
54 Points
102 Posts
how to call session in .asmx page ?
Aug 07, 2017 07:29 AM|wibowowiwit|LINK
Hi guys,
in general we can call session using this code :
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
All-Star
191738 Points
20952 Posts
ASPInsiders
Moderator
MVP
Re: how to call session in .asmx page ?
Aug 07, 2017 07:40 AM|XIII|LINK
Hi,
should be
Kris.
Working with Azure, chatbots, ASP.NET MVC, Web API, EF, MS SQL, ...
Keep the forums clean: report to the moderation team!
Member
54 Points
102 Posts
Re: how to call session in .asmx page ?
Aug 07, 2017 07:56 AM|wibowowiwit|LINK
Hi Kris,
thank for you answer.
it does't work.
i also add [WebMethod(EnableSession = true)]
but does't work to get session.
All-Star
191738 Points
20952 Posts
ASPInsiders
Moderator
MVP
Re: how to call session in .asmx page ?
Aug 07, 2017 08:03 AM|XIII|LINK
Hi,
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.
Working with Azure, chatbots, ASP.NET MVC, Web API, EF, MS SQL, ...
Keep the forums clean: report to the moderation team!
All-Star
52971 Points
23566 Posts
Re: how to call session in .asmx page ?
Aug 07, 2017 11:29 AM|mgebhard|LINK
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.
All-Star
45489 Points
7008 Posts
Microsoft
Re: how to call session in .asmx page ?
Aug 10, 2017 08:38 AM|Zhi Lv - MSFT|LINK
Hi Wibowo Wiwit,
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:
Code in web page code behind:
Code in web Service:
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