Search content within the blog

Wednesday, April 22, 2009

Enabling session in web services using EnableSession.

To enable session in web services we use the EnableSession attribute to be set to true.
once you have created your web service and added the refrence to it in your project refresh your aspx page to know how the session works.The code is self explanatory.

Webservice code
using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;

namespace SampleService
/// Summary description for EnableSession

[WebService(Namespace = "")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class EnableSession : System.Web.Services.WebService

[WebMethod(Description="Describes how to use session in web services",EnableSession=true)]
public string HelloWorld()
// get the Count out of Session State
int? Count = (int?)Session["Count"];

if (Count == null)
Count = 0;

// increment and store the count
Session["Count"] = Count;

return "Hello World - Call Number: " + Count.ToString();


ASPX.CS code
localhost.EnableSession objEnbSession;
// try to get the proxy from Session state
objEnbSession = Session["MyService"] as localhost.EnableSession;
if (objEnbSession == null)
// create the proxy
objEnbSession = new localhost.EnableSession();

// create a container for the SessionID cookie
objEnbSession.CookieContainer = new CookieContainer();

// store it in Session for next usage
Session["MyService"] = objEnbSession;

// call the Web Service function
Response.Write(objEnbSession.HelloWorld() + "<br />");

No comments:

Post a Comment