Programming is Passion,Software Development is an Adventure- Willy David Jr

Programming is Passion,Software Development is an Adventure- Willy David Jr

June 2008 - Posts

Using ASP.NET Session State in a Web Service

 Since yesterday, I am having problem creating a Session State in a Web Service. First the session creates a unique ID everytime you invoke the method. Second you need to tell the web service method that you will use session state. I've seen a lot of example out there, but I am having trouble increment the hits of my web service method. I think there is a problem with their code. So here's what I did:

1.) First Create the Web Methods. Make sure your set the EnableSession to true:

(One is to get the value of the session changed, the second one is to change the session value as parameter)

[WebMethod(EnableSession = true)]

public object getsession()

{

return Session["MySession"];

}

 

[WebMethod(EnableSession = true)]

public void changesession(object val)

{

Session["MySession"] = val;

 

So how do you consume this web methods and use session at the same time on web service? First create a cookie that will be your ID for your service, then put this cookie on a session, so that whenever you will need this cookie with the same ID for your web service session, you can just call the session name. Here is an example:

localhost.Service myService; //declare your web service variable

protected void Button1_Click(object sender, EventArgs e)
{

System.Net.CookieContainer cookies = new System.Net.CookieContainer(); //declare your cookie as your ID for your Session's web service

object x;

Session["MyCookie"] = cookies; //put the cookie on a Session

if (myService == null)
{

myService = new localhost.Service();


myService.CookieContainer = (System.Net.CookieContainer)Session["MyCookie"]; //declare your CookieContainer for your service which is in your Session, which you can always call

Session["MySession"] = "Hello World" ;

myService.changesession(Session["MySession"]); //change your Session value on your web service method which is saved on your unique CookieContainer


}

 

protected void Button2_Click(object sender, EventArgs e)

{

if (myService == null)


{

myService = new localhost.Service();


myService.CookieContainer=(System.Net.CookieContainer)Session["MyCookie"]; //declare your CookieContainer again which is in your Session, this CookieContainer should have specific value

Session["MySession"] = myService.getsession(); //retrieve the value of your Session value on your web service method

}

 

It was first confusing, but with little practice and applying it in a mock-up application, you can understand the logic behind declaring a Session inside a web service which is important for methods that needs the theory behind it.

 

Microsoft Student Partners’ Graduation: One of my Best Day in Microsoft

 

Why is it one of the best day in Microsoft? Yesterday, June 07, 2008, was one my best day I ever had in Microsoft. I never thought that would be that fun and exciting. Though one week before that, I thought I couldn’t make it because I have an appointment on my doctor, and have scheduled my gym during weekends at Fitness First Alabang, I rather chose to be on Microsoft and take aside all my appointments.

How did the story begins? Let’s take my story a year before my graduation. I started to be as part of Microsoft Student Partners last August 2, 2007. It was my first meeting invitation from Ms. Tin. I thought the email I received was an accident because I was still on the process of applying for MSP, but Ms. Tin verified that I was included on the list, so I came on the meeting. By the way, I knew MSP program since the Imagine Cup competition. We where one of the top 5 finalist all over Philippines, and all MSPs where there and I thought that after this competition, I would try MSP too. And that how the story begins.

How’s life being an MSP? Well being an MSP is different as being a student, and a whole lot different on being an employee. First, you don’t earn money as an MSP, though it will makes sense if you apply for an employer and they will hear you’ve been part of Microsoft for a year. As an MSP, you don’t just bring your school name in Microsoft, you are also bringing yourself to all students around different Universities and Colleges. My first talk at University of the Philippines, Diliman together with Seco was one of my challenging task I’ve had. Challenging because it will be my first talk and demo on a different University. Usually, I am calm and I love to demo on my own University because I knew my classmates well, but on a different perpective, that is another way around. It was fund and exciting, and you just don’t talk, you are bonding with people, so much that I bond with MSP.

How about MSP people? Every MSP I met is supportive. It was not kind of a thing I thought that wasn’t. That was passion is made from every part of the student on the group. And that’s how MSP are different. And lastly, the bonding activity you will experience with MSP after an event or demo. The videoke part was one of my favorite.

So how this graduation goes? As I said, I can’t describe how happy I am with these people and as the graduation goes, I was awarded as best programmer/code of the year (MSPs correct me if I’m wrong, that’s how I remember it). I never expect that award but I am very thankful to them because I received it. And as the program goes, it was full of fun and laughter, and every game was cool and exciting. We received our recognition/certification as being an MSP of 2008, and a jacket of MSDN. Wow, this jacket is pretty cool than the one I received a year ago. The planning was quite tough but I bet it will be a more challenging one for this year, I hope I can help achieve this plan. After that, we have a good dinner, before everybody goes home. I am tired, but on the bus, I still remember the jokes of everyone, I remember my crush on MSP which is Cha, I remember all the past memories I’ve been as MSP, I remember every good memories I’ve been with them, I remember that this will be one my best day in Microsoft! Wink