Justin Obney
Justin Obney

Reputation: 1078

Session Variables and Web Services

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??

Upvotes: 10

Views: 33981

Answers (7)

Debendra Dash
Debendra Dash

Reputation: 5626

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;  

 }  

Upvotes: 0

Prashiddha Raj Joshi
Prashiddha Raj Joshi

Reputation: 91

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

Upvotes: 1

Metro
Metro

Reputation: 1514

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.

Upvotes: 21

Pablo Marambio
Pablo Marambio

Reputation: 1578

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.

Upvotes: 5

Kyle Trauberman
Kyle Trauberman

Reputation: 25694

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.

Upvotes: 0

marc
marc

Reputation: 3338

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

Upvotes: 3

Yitzchok
Yitzchok

Reputation: 691

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

Upvotes: 0

Related Questions