I’m using VS2010, asp.net c#.
I have a WebMethod declared like this
[WebMethod(EnableSession = true)]
public String getErrorMsg()
{
if (HttpContext.Current.Session["error"] == null) return "empty";
else return ((String)(HttpContext.Current.Session["error"]));
}
Inside a System.Web.Services.WebService Class(inheritaded).
In another WebMethod, I set this message let’s just assume it’s like this
[WebMethod(EnableSession = true)]
public void firstMethod()
{
HttpContext.Current.Session["error"] ="bla";
}
In another project, I have added a Service Refrence to this WebService.
I then try to use it like this:
WebService1.WebService1SoapClient MyService=
new WebService1.WebService1SoapClient();
MyService.firstMethod(); // this calls the method that sets
//the "bla" string in the session
String str=MyService.getErrorMsg();
System.Diagnostics.Debug.WriteLine("Message "+str);
Str is “empty”;
At the first method, the “bla” string is set to the session. as long as im in this method, i can use the session stored data.
When I make another call to the WebService, the previous session data is no longer exists.
Ive looked everywhere. I found only this example :
// instantiate the proxy
localhost.MyDemo MyService = new localhost.MyDemo();
// create a container for the SessionID cookie
MyService.CookieContainer = new CookieContainer();
// call the Web Service function
Label1.Text += MyService.HelloWorld() + "<br />";
But there is 2 main problems:
1) My WebService cannot be used as an object, meaning I cannot use “new” on that class, as this example somehow does.
I use WebService1SoapClient in order to use the WebService.
2)There is no such thing CookieContainer in the WebService1SoapClient object.
I think this example must have been used in an old vs/.net version.
Does anyone knows how can I keep a Session data in a webService?
I might seems to find the answer.
In web.config you have a
Add the following under the binding name:
So it looks like this: