Question

I just wrote my first web service so lets make the assumption that my web service knowlege is non existant. I want to try to call a dbClass function from the web service. However I need some params that are in the session. Is there any way I can get these call these session variables from the webservice??

Was it helpful?

Solution

If you are using ASP.NET web services and you want to have a session environment maintained for you, you need to embellish your web service method with an attribute that indicates you require a session.

[WebMethod(EnableSession = true)]
public void MyWebService()
{
    Foo foo;
    Session["MyObjectName"] = new Foo();
    foo = Session["MyObjectName"] as Foo;
}

Once you have done this, you may access session objects similar to aspx.

Metro.

OTHER TIPS

You should avoid increasing the complexity of the service layer adding session variables. As someone previously pointed out, think of the web services as isolated methods that take all what is needed to perform the task from their argument list.

In general web services should not rely on session data. Think of them as ordinary methods: parameters go in and an answer comes out.

if you have to want Session["username"].ToString(); as in the other C# pages behind aspx then you should simply replace [WebMethod] above the WebService method with [WebMethod(EnableSession = true)]

thanks to :) Metro

Maybe this will work HttpContext.Current.Session["Name] Or else you might have to take in some parameters or store them in a Database

Your question is a little vague, but I'll try my best to answer.

I'm assuming that your session variables exist on the server that is making the webservice call, and not on the server that hosts the webservice. In that case, you will need to pass the necessary values as parameters of your web service methods.

To use session in webservice we have to follow 2 steps-

  1. Use [WebMethod(EnableSession = true)] attribute on the method.
  2. Session["Name"] =50 (what ever you want to save) Please check the following Example.
[WebMethod(EnableSession = true)]  
public void saveName(string pname)  
{  
   Session["Name"] = pname;  

 }  

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top