Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 352
Justin Obney Avatar asked Sep 26 '08 16:09

Justin Obney


People also ask

Can we use session in web service?

A Session is one of the server-side state management techniques that stores the user specific data across the user request. By default a session is not enabled in a web service; we need to enable it using the following procedure. The default time out of the session is 20, the same as any web application.

What is session in webservice?

The Session Manager Web Service contains operations for establishing a session with Network Gatekeeper, changing the application's password, querying the amount of time remaining in the session, refreshing the session, and terminating the session.

What is session variable used for?

Session variables are a way to store data about a user in a database and retrieve it later. Cookies are a way to store data about a user on the user's computer. Session variables are typically used in applications that need to keep track of a user's activity.

Are session variables stored on client or server?

They're generally stored on the server. Where they're stored is up to you as the developer. You can use the session. save_handler configuration variable and the session_set_save_handler to control how sessions get saved on the server.


4 Answers

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.

like image 113
Metro Avatar answered Nov 11 '22 04:11

Metro


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.

like image 24
Pablo Marambio Avatar answered Nov 11 '22 06:11

Pablo Marambio


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

like image 44
marc Avatar answered Nov 11 '22 05:11

marc


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

like image 35
Prashiddha Raj Joshi Avatar answered Nov 11 '22 04:11

Prashiddha Raj Joshi