charani

charani

  • NA
  • 1
  • 0

accessing web services from winforms and maintaining sessions

Aug 24 2006 3:21 AM
 

Hi,

    I am trying to access web services from the windows forms.

In the web services iam trying to maintain the session by setting the attribute "EnableSession = true"  of the Web Method. And then i tried to maintain sessions as Session["user"] = userName. When i tried to call the web  service methods from the winForm, iam getting the following exception

"An unhandled exception of type 'System.InvalidOperationException' occurred in system.web.services.dll

Additional information: HttpContext is not available.   This class can only be used in the context of an ASP.NET request."

Here the HttpContect.current.session object is storing null (not set an object reference). My doubt is if the web services are accessed through winForms, does the httpcontext exist? Why it is giving that error. The same code works fine for web forms.   

 I tried to search in the google for the documentation or any information, but of no use.
And one more thing, if iam trying to access the remote webservices (from the web) then i have to enable the cookies at the client end. I did this when accessing remote web services. Anyways it din't work. I think here i don't have to set the cookies.

 Here is the code of the webservice:

[WebMethod (EnableSession = true)]

public string HelloWorld(string userName, string password)

{

//HttpContext.Current.Session.Add("UserName",userName);

Session["userName"] = userName;

//return HttpContext.Current.Session["userName"].ToString();

return Session["userName"].ToString();

}

[WebMethod (EnableSession = true)]

public string checkSession()

{

if(HttpContext.Current.Session["userName"] != null)

return "success";

elseelp appreciated.

return "unsuccess";

}

My client code:

string sss = s.HelloWorld("CGayathri", "anu0Sai");

MessageBox.Show(sss);

string ss = s.checkSession();

MessageBox.Show(ss);

Any kind of help appreciated.

--Charani