method being called to quickly?

K

Kevin Blount

I have a situation where I think I'm calling a method too quickly, but
I'm not sure a) how to prove that theory and b) slow it down if I'm
right. Here's the code:

CODEFILE.ASPX.CS
public void updateCookie(string cookieName, string cookiePairName,
string cookiePairValue) {
if (Request.Cookies[cookieName] != null) {
string currentCookie = Request.Cookies[cookieName].Value;
currentCookie += "&" + cookiePairName + "=" + cookiePairValue;
Response.Cookies[cookieName].Value = currentCookie;
}
else
{
Response.Cookies[cookieName][cookiePairName] = cookiePairValue;
}
Response.Cookies[cookieName].Expires = DateTime.Now.AddDays(30);
}

SETCOOKIE1.ASPX
....
<%
updateCookie("Communities","LoggedIn","true");
updateCookie("Communities","member_id","120");
%>
....

SETCOOKIE2.ASPX
....
<%
updateCookie("Communities","banner","1");
updateCookie("Communities","emailaddress","(e-mail address removed)")
%>
....

VIEWCOOKIE.ASPX
....
Response.Write(Request.Cookies["Communities"].Value);
....


The issue is this:
- initially I delete all cookies from I.E. and view 'viewcookie.aspx'
to confirm that no cookie value is displayed.
- Next I view 'setcookie1.aspx', then go back to 'viewcookie.aspx',
refresh and I see something like 'LoggedIn=true&member_id=120', as I'd
expect.
- I then view 'setcookie2.aspx', go back to 'viewcookie.aspx' and
refresh...

this time I see
'LoggedIn=true&member_id=120&[email protected]', whereas
I would expect to see '&banner=1' before '&emailaddress...', i.e. 4
name-value pairs set by the two aspx files, rather than just 3 pairs

if I comment out the 'updateCookie' lines one at a time in
setcookie2.aspx, they both work individually, but when they are both
uncommented, only one of the two calls to updateCookie actually work.

the same is true if I run setcookie2.aspx before I run
setcookie1.aspx.. i.e. both values from setcookie2.aspx are in the
cookie, but only one of those from setcookie1.aspx gets entered.

also, if I go back to the setcookie file where both entries were added
and run it again while the cookie exists, only one of the two
previously working entries is appended


Am I calling the method too quickly? can I slow it down? Is there a
better way to do the cookie part that won't cause this??

any help would be very appreciated (let me know if my explaination is
too irratic <g>)
 
K

Kevin Blount

I've discovered that what's happening isn't that the method is being
used to quickly, but rather the creation of the new cookie isn't being
commited between each call to the method.

In my testing I found that I can update the cookie in setCookie.aspx,
and then if I try to access that cookie in the same page, using
Request.Cookies, it will show the old, un-updated value.

This means that when I call the method for the second time, the cookie
value I am reading and appending to is the old one, not the new one.

The only time I see the new value of the cookie is when I either reload
the page, or go to another page, such as viewcookie.aspx.

Any clues why the cookies is not 'instantly' being written??

Kevin

Kevin said:
I have a situation where I think I'm calling a method too quickly, but
I'm not sure a) how to prove that theory and b) slow it down if I'm
right. Here's the code:

CODEFILE.ASPX.CS
public void updateCookie(string cookieName, string cookiePairName,
string cookiePairValue) {
if (Request.Cookies[cookieName] != null) {
string currentCookie = Request.Cookies[cookieName].Value;
currentCookie += "&" + cookiePairName + "=" + cookiePairValue;
Response.Cookies[cookieName].Value = currentCookie;
}
else
{
Response.Cookies[cookieName][cookiePairName] = cookiePairValue;
}
Response.Cookies[cookieName].Expires = DateTime.Now.AddDays(30);
}

SETCOOKIE1.ASPX
...
<%
updateCookie("Communities","LoggedIn","true");
updateCookie("Communities","member_id","120");
%>
...

SETCOOKIE2.ASPX
...
<%
updateCookie("Communities","banner","1");
updateCookie("Communities","emailaddress","(e-mail address removed)")
%>
...

VIEWCOOKIE.ASPX
...
Response.Write(Request.Cookies["Communities"].Value);
...


The issue is this:
- initially I delete all cookies from I.E. and view 'viewcookie.aspx'
to confirm that no cookie value is displayed.
- Next I view 'setcookie1.aspx', then go back to 'viewcookie.aspx',
refresh and I see something like 'LoggedIn=true&member_id=120', as I'd
expect.
- I then view 'setcookie2.aspx', go back to 'viewcookie.aspx' and
refresh...

this time I see
'LoggedIn=true&member_id=120&[email protected]', whereas
I would expect to see '&banner=1' before '&emailaddress...', i.e. 4
name-value pairs set by the two aspx files, rather than just 3 pairs

if I comment out the 'updateCookie' lines one at a time in
setcookie2.aspx, they both work individually, but when they are both
uncommented, only one of the two calls to updateCookie actually work.

the same is true if I run setcookie2.aspx before I run
setcookie1.aspx.. i.e. both values from setcookie2.aspx are in the
cookie, but only one of those from setcookie1.aspx gets entered.

also, if I go back to the setcookie file where both entries were added
and run it again while the cookie exists, only one of the two
previously working entries is appended


Am I calling the method too quickly? can I slow it down? Is there a
better way to do the cookie part that won't cause this??

any help would be very appreciated (let me know if my explaination is
too irratic <g>)
 
D

Dave Sexton

Hi Kevin,

Try buffering all changes to cookies until immediately before the request is completed.

using System.Collections.Generic;

/// <summary><see cref="Dictionary" /> of string keys and values for buffering cookies until
the <see cref="Page" /> is rendered, at which time the buffered cookies are written to the
<see cref="Response" /> in the <see cref="Page.PreRender" /> event.</summary>
private readonly void Dictionary<string, Dictionary<string, string>>
cookieBuffer = new Dictionary<string, Dictionary<string, string>>();

--
Dave Sexton

Kevin Blount said:
I've discovered that what's happening isn't that the method is being
used to quickly, but rather the creation of the new cookie isn't being
commited between each call to the method.

In my testing I found that I can update the cookie in setCookie.aspx,
and then if I try to access that cookie in the same page, using
Request.Cookies, it will show the old, un-updated value.

This means that when I call the method for the second time, the cookie
value I am reading and appending to is the old one, not the new one.

The only time I see the new value of the cookie is when I either reload
the page, or go to another page, such as viewcookie.aspx.

Any clues why the cookies is not 'instantly' being written??

Kevin

Kevin said:
I have a situation where I think I'm calling a method too quickly, but
I'm not sure a) how to prove that theory and b) slow it down if I'm
right. Here's the code:

CODEFILE.ASPX.CS
public void updateCookie(string cookieName, string cookiePairName,
string cookiePairValue) {
if (Request.Cookies[cookieName] != null) {
string currentCookie = Request.Cookies[cookieName].Value;
currentCookie += "&" + cookiePairName + "=" + cookiePairValue;
Response.Cookies[cookieName].Value = currentCookie;
}
else
{
Response.Cookies[cookieName][cookiePairName] = cookiePairValue;
}
Response.Cookies[cookieName].Expires = DateTime.Now.AddDays(30);
}

SETCOOKIE1.ASPX
...
<%
updateCookie("Communities","LoggedIn","true");
updateCookie("Communities","member_id","120");
%>
...

SETCOOKIE2.ASPX
...
<%
updateCookie("Communities","banner","1");
updateCookie("Communities","emailaddress","(e-mail address removed)")
%>
...

VIEWCOOKIE.ASPX
...
Response.Write(Request.Cookies["Communities"].Value);
...


The issue is this:
- initially I delete all cookies from I.E. and view 'viewcookie.aspx'
to confirm that no cookie value is displayed.
- Next I view 'setcookie1.aspx', then go back to 'viewcookie.aspx',
refresh and I see something like 'LoggedIn=true&member_id=120', as I'd
expect.
- I then view 'setcookie2.aspx', go back to 'viewcookie.aspx' and
refresh...

this time I see
'LoggedIn=true&member_id=120&[email protected]', whereas
I would expect to see '&banner=1' before '&emailaddress...', i.e. 4
name-value pairs set by the two aspx files, rather than just 3 pairs

if I comment out the 'updateCookie' lines one at a time in
setcookie2.aspx, they both work individually, but when they are both
uncommented, only one of the two calls to updateCookie actually work.

the same is true if I run setcookie2.aspx before I run
setcookie1.aspx.. i.e. both values from setcookie2.aspx are in the
cookie, but only one of those from setcookie1.aspx gets entered.

also, if I go back to the setcookie file where both entries were added
and run it again while the cookie exists, only one of the two
previously working entries is appended


Am I calling the method too quickly? can I slow it down? Is there a
better way to do the cookie part that won't cause this??

any help would be very appreciated (let me know if my explaination is
too irratic <g>)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top