Showing posts with label safety. Show all posts
Showing posts with label safety. Show all posts

Saturday, March 24, 2012

This message has been blocked for your safety

Have you tried to click on "Learn more" ?!!!

It looks like you are using a software (not sure if this is a server side
firewall software or if you try to read the message in a client) that
consider this as a potential threat. IMO your best is to look for support at
this third party product site as it looks like this issue is not related to
..NET...

--
Patrice

"Jassim Rahma" <jrahma@dotnet.itags.org.hotmail.coma crit dans le message de news:
F12C8C18-20DE-4A06-A415-2FC181265544@dotnet.itags.org.microsoft.com...

Quote:

Originally Posted by

>I am getting this message when sending using System.Net.Mail... I have
attached the print screen and here is the complete code:
>
NetworkCredential network_credential = new
NetworkCredential("info@dotnet.itags.org.zatoor.com", "xxxxxx");
MailMessage mail_message = new MailMessage();
// SmtpClient smtp_client = new SmtpClient("localhost");
SmtpClient smtp_client = new SmtpClient("smtp.zatoor.com");
smtp_client.UseDefaultCredentials = false;
smtp_client.Port = 25;
smtp_client.Credentials = network_credential;
>
string html_message = null;
>
html_message = "<html>";
html_message += "<head>";
html_message += "<meta http-equiv=Content-Type
content='text/html; charset=windows-1252'>";
html_message += "</head>";
html_message += "<body>";
html_message += "Thank you for registering with zatoor.com
and welcome to the world of free classifieds. We hope you will enjoy using
our website.";
html_message += "<br>";
html_message += "<br><br>";
html_message += "zatoor.com Team";
html_message += "</body>";
html_message += "</html>";
>
mail_message.From = new MailAddress("info@dotnet.itags.org.zatoor.com",
"zatoor.com");
mail_message.To.Add(txtEmailAddress.Text);
mail_message.CC.Add("jrahma@dotnet.itags.org.yahoo.com");
// mail_message.Priority = MailPriority.High;
mail_message.IsBodyHtml = true;
mail_message.Subject = "welcome to zatoor.com";
mail_message.Body = html_message;
>
mail_message.DeliveryNotificationOptions =
DeliveryNotificationOptions.OnFailure;
>
smtp_client.Send(mail_message);
>

can you please expian what do you mean?

"Patrice" <http://www.chez.com/scribe/wrote in message
news:uKsNceTyHHA.4712@.TK2MSFTNGP04.phx.gbl...

Quote:

Originally Posted by

Have you tried to click on "Learn more" ?!!!
>
It looks like you are using a software (not sure if this is a server side
firewall software or if you try to read the message in a client) that
consider this as a potential threat. IMO your best is to look for support
at this third party product site as it looks like this issue is not
related to .NET...
>
--
Patrice
>
"Jassim Rahma" <jrahma@.hotmail.coma crit dans le message de news:
F12C8C18-20DE-4A06-A415-2FC181265544@.microsoft.com...

Quote:

Originally Posted by

>>I am getting this message when sending using System.Net.Mail... I have
>attached the print screen and here is the complete code:
>>
> NetworkCredential network_credential = new
>NetworkCredential("info@.zatoor.com", "xxxxxx");
> MailMessage mail_message = new MailMessage();
> // SmtpClient smtp_client = new SmtpClient("localhost");
> SmtpClient smtp_client = new
>SmtpClient("smtp.zatoor.com");
> smtp_client.UseDefaultCredentials = false;
> smtp_client.Port = 25;
> smtp_client.Credentials = network_credential;
>>
> string html_message = null;
>>
> html_message = "<html>";
> html_message += "<head>";
> html_message += "<meta http-equiv=Content-Type
>content='text/html; charset=windows-1252'>";
> html_message += "</head>";
> html_message += "<body>";
> html_message += "Thank you for registering with zatoor.com
>and welcome to the world of free classifieds. We hope you will enjoy
>using
>our website.";
> html_message += "<br>";
> html_message += "<br><br>";
> html_message += "zatoor.com Team";
> html_message += "</body>";
> html_message += "</html>";
>>
> mail_message.From = new MailAddress("info@.zatoor.com",
>"zatoor.com");
> mail_message.To.Add(txtEmailAddress.Text);
> mail_message.CC.Add("jrahma@.yahoo.com");
> // mail_message.Priority = MailPriority.High;
> mail_message.IsBodyHtml = true;
> mail_message.Subject = "welcome to zatoor.com";
> mail_message.Body = html_message;
>>
> mail_message.DeliveryNotificationOptions =
>DeliveryNotificationOptions.OnFailure;
>>
> smtp_client.Send(mail_message);
>>


>
>


AFAIK this message is not produced by the DOTNET framework but by a third
party product that blocks the message you try to send for some reason.. In
this message you have a "Learn more" link that you could click to see why
this third party software choosed to block this transmission.

So for now it looks like to me this is not at all related to a programming
issue but to a third party software (firewall, antivirus ?)

Or is this that your mail provider replaced the screenshot by a message of
its own ;-) - Generally just reproducing the text should be enough. The
message I see is the "This message has been blocked for your safety"" whih
is not a DOTNET message...

--
Patrice

"Jassim Rahma" <jrahma@.hotmail.coma crit dans le message de news:
uOjWFueyHHA.5380@.TK2MSFTNGP04.phx.gbl...

Quote:

Originally Posted by

can you please expian what do you mean?
>
>
>
"Patrice" <http://www.chez.com/scribe/wrote in message
news:uKsNceTyHHA.4712@.TK2MSFTNGP04.phx.gbl...

Quote:

Originally Posted by

>Have you tried to click on "Learn more" ?!!!
>>
>It looks like you are using a software (not sure if this is a server side
>firewall software or if you try to read the message in a client) that
>consider this as a potential threat. IMO your best is to look for support
>at this third party product site as it looks like this issue is not
>related to .NET...
>>
>--
>Patrice
>>
>"Jassim Rahma" <jrahma@.hotmail.coma crit dans le message de news:
>F12C8C18-20DE-4A06-A415-2FC181265544@.microsoft.com...

Quote:

Originally Posted by

>>>I am getting this message when sending using System.Net.Mail... I have
>>attached the print screen and here is the complete code:
>>>
>> NetworkCredential network_credential = new
>>NetworkCredential("info@.zatoor.com", "xxxxxx");
>> MailMessage mail_message = new MailMessage();
>> // SmtpClient smtp_client = new SmtpClient("localhost");
>> SmtpClient smtp_client = new
>>SmtpClient("smtp.zatoor.com");
>> smtp_client.UseDefaultCredentials = false;
>> smtp_client.Port = 25;
>> smtp_client.Credentials = network_credential;
>>>
>> string html_message = null;
>>>
>> html_message = "<html>";
>> html_message += "<head>";
>> html_message += "<meta http-equiv=Content-Type
>>content='text/html; charset=windows-1252'>";
>> html_message += "</head>";
>> html_message += "<body>";
>> html_message += "Thank you for registering with
>>zatoor.com
>>and welcome to the world of free classifieds. We hope you will enjoy
>>using
>>our website.";
>> html_message += "<br>";
>> html_message += "<br><br>";
>> html_message += "zatoor.com Team";
>> html_message += "</body>";
>> html_message += "</html>";
>>>
>> mail_message.From = new MailAddress("info@.zatoor.com",
>>"zatoor.com");
>> mail_message.To.Add(txtEmailAddress.Text);
>> mail_message.CC.Add("jrahma@.yahoo.com");
>> // mail_message.Priority = MailPriority.High;
>> mail_message.IsBodyHtml = true;
>> mail_message.Subject = "welcome to zatoor.com";
>> mail_message.Body = html_message;
>>>
>> mail_message.DeliveryNotificationOptions =
>>DeliveryNotificationOptions.OnFailure;
>>>
>> smtp_client.Send(mail_message);
>>>


>>
>>


>

This message has been blocked for your safety

I am getting this message when sending using System.Net.Mail... I have
attached the print screen and here is the complete code:
NetworkCredential network_credential = new
NetworkCredential("info@dotnet.itags.org.zatoor.com", "xxxxxx");
MailMessage mail_message = new MailMessage();
// SmtpClient smtp_client = new SmtpClient("localhost");
SmtpClient smtp_client = new SmtpClient("smtp.zatoor.com");
smtp_client.UseDefaultCredentials = false;
smtp_client.Port = 25;
smtp_client.Credentials = network_credential;
string html_message = null;
html_message = "<html>";
html_message += "<head>";
html_message += "<meta http-equiv=Content-Type
content='text/html; charset=windows-1252'>";
html_message += "</head>";
html_message += "<body>";
html_message += "Thank you for registering with zatoor.com
and welcome to the world of free classifieds. We hope you will enjoy using
our website.";
html_message += "<br>";
html_message += "<br><br>";
html_message += "zatoor.com Team";
html_message += "</body>";
html_message += "</html>";
mail_message.From = new MailAddress("info@dotnet.itags.org.zatoor.com",
"zatoor.com");
mail_message.To.Add(txtEmailAddress.Text);
mail_message.CC.Add("jrahma@dotnet.itags.org.yahoo.com");
// mail_message.Priority = MailPriority.High;
mail_message.IsBodyHtml = true;
mail_message.Subject = "welcome to zatoor.com";
mail_message.Body = html_message;
mail_message.DeliveryNotificationOptions =
DeliveryNotificationOptions.OnFailure;
smtp_client.Send(mail_message);Have you tried to click on "Learn more" ?!!!
It looks like you are using a software (not sure if this is a server side
firewall software or if you try to read the message in a client) that
consider this as a potential threat. IMO your best is to look for support at
this third party product site as it looks like this issue is not related to
.NET...
Patrice
"Jassim Rahma" <jrahma@.hotmail.com> a crit dans le message de news:
F12C8C18-20DE-4A06-A415-2FC181265544@.microsoft.com...
>I am getting this message when sending using System.Net.Mail... I have
> attached the print screen and here is the complete code:
> NetworkCredential network_credential = new
> NetworkCredential("info@.zatoor.com", "xxxxxx");
> MailMessage mail_message = new MailMessage();
> // SmtpClient smtp_client = new SmtpClient("localhost");
> SmtpClient smtp_client = new SmtpClient("smtp.zatoor.com");
> smtp_client.UseDefaultCredentials = false;
> smtp_client.Port = 25;
> smtp_client.Credentials = network_credential;
> string html_message = null;
> html_message = "<html>";
> html_message += "<head>";
> html_message += "<meta http-equiv=Content-Type
> content='text/html; charset=windows-1252'>";
> html_message += "</head>";
> html_message += "<body>";
> html_message += "Thank you for registering with zatoor.com
> and welcome to the world of free classifieds. We hope you will enjoy using
> our website.";
> html_message += "<br>";
> html_message += "<br><br>";
> html_message += "zatoor.com Team";
> html_message += "</body>";
> html_message += "</html>";
> mail_message.From = new MailAddress("info@.zatoor.com",
> "zatoor.com");
> mail_message.To.Add(txtEmailAddress.Text);
> mail_message.CC.Add("jrahma@.yahoo.com");
> // mail_message.Priority = MailPriority.High;
> mail_message.IsBodyHtml = true;
> mail_message.Subject = "welcome to zatoor.com";
> mail_message.Body = html_message;
> mail_message.DeliveryNotificationOptions =
> DeliveryNotificationOptions.OnFailure;
> smtp_client.Send(mail_message);
>
can you please expian what do you mean?
"Patrice" <http://www.chez.com/scribe/> wrote in message
news:uKsNceTyHHA.4712@.TK2MSFTNGP04.phx.gbl...
> Have you tried to click on "Learn more" ?!!!
> It looks like you are using a software (not sure if this is a server side
> firewall software or if you try to read the message in a client) that
> consider this as a potential threat. IMO your best is to look for support
> at this third party product site as it looks like this issue is not
> related to .NET...
> --
> Patrice
> "Jassim Rahma" <jrahma@.hotmail.com> a crit dans le message de news:
> F12C8C18-20DE-4A06-A415-2FC181265544@.microsoft.com...
>
AFAIK this message is not produced by the DOTNET framework but by a third
party product that blocks the message you try to send for some reason.. In
this message you have a "Learn more" link that you could click to see why
this third party software choosed to block this transmission.
So for now it looks like to me this is not at all related to a programming
issue but to a third party software (firewall, antivirus ?)
Or is this that your mail provider replaced the screenshot by a message of
its own ;-) - Generally just reproducing the text should be enough. The
message I see is the "This message has been blocked for your safety"" whih
is not a DOTNET message...
Patrice
"Jassim Rahma" <jrahma@.hotmail.com> a crit dans le message de news:
uOjWFueyHHA.5380@.TK2MSFTNGP04.phx.gbl...
> can you please expian what do you mean?
>
> "Patrice" <http://www.chez.com/scribe/> wrote in message
> news:uKsNceTyHHA.4712@.TK2MSFTNGP04.phx.gbl...
>
but I am not getting this problem when sending to yahoo address?!! it only
shows when sending to hotmail? any parameters I should set in order to make
the email trusted?
"Patrice" <http://www.chez.com/scribe/> wrote in message
news:ey3I1qfyHHA.1176@.TK2MSFTNGP05.phx.gbl...
> AFAIK this message is not produced by the DOTNET framework but by a third
> party product that blocks the message you try to send for some reason.. In
> this message you have a "Learn more" link that you could click to see why
> this third party software choosed to block this transmission.
> So for now it looks like to me this is not at all related to a programming
> issue but to a third party software (firewall, antivirus ?)
> Or is this that your mail provider replaced the screenshot by a message of
> its own ;-) - Generally just reproducing the text should be enough. The
> message I see is the "This message has been blocked for your safety"" whih
> is not a DOTNET message...
> --
> Patrice
> "Jassim Rahma" <jrahma@.hotmail.com> a crit dans le message de news:
> uOjWFueyHHA.5380@.TK2MSFTNGP04.phx.gbl...
>
Don't know. Once again for now it doesn't look to me this is a problem at
the DOTNET framework level.
Don't you have a window title or something that could tell you what is the
product that display this message ? Don't you know if this looks like
screens displayed by your antivirus, firewall software etc... Don't you see
a productname somewhere ?
"Jassim Rahma" <jrahma@.hotmail.com> a crit dans le message de news:
35430664-840E-460A-ACEC-545E37C8A0A0@.microsoft.com...
> but I am not getting this problem when sending to yahoo address?!! it only
> shows when sending to hotmail? any parameters I should set in order to
> make the email trusted?
>
> "Patrice" <http://www.chez.com/scribe/> wrote in message
> news:ey3I1qfyHHA.1176@.TK2MSFTNGP05.phx.gbl...
>
no, it dosn't show any name
"Patrice" <http://www.chez.com/scribe/> wrote in message
news:OrUdNUsyHHA.1184@.TK2MSFTNGP04.phx.gbl...
> Don't know. Once again for now it doesn't look to me this is a problem at
> the DOTNET framework level.
> Don't you have a window title or something that could tell you what is the
> product that display this message ? Don't you know if this looks like
> screens displayed by your antivirus, firewall software etc... Don't you
> see a productname somewhere ?
> "Jassim Rahma" <jrahma@.hotmail.com> a crit dans le message de news:
> 35430664-840E-460A-ACEC-545E37C8A0A0@.microsoft.com...
>
I gave this a look. It looks like you might as well have this when you read
this mail in hotmail ? What didn't you said that ?!
Click on Open Message to read the mail. Click On "Learn more" or the FAQ to
find out why Hotmail classified this message as potentially dangerous. For
example an option could simply black HTML mails (that could contains
JavaScript exploits) ?
So it looks like an hotmail issue. Nothing to do with DOTNET IMO.
Patrice
"Jassim Rahma" <jrahma@.hotmail.com> a crit dans le message de news:
FF331512-9F1A-49F6-AC24-6D2BBB39314B@.microsoft.com...
> no, it dosn't show any name
>
> "Patrice" <http://www.chez.com/scribe/> wrote in message
> news:OrUdNUsyHHA.1184@.TK2MSFTNGP04.phx.gbl...
>

Tuesday, March 13, 2012

thread safety

Hi,
I have a web app which has the following design to get to the DB
Web Page (uses)
-> .net DLL library
--> has a class with static methods
--> static method to-do basic DB operations like create a
DataReader for given sql, get Dataset etc
Big question is... Is this design safe when a lot of users are hitting the
web site from the asp.net threading perspective?As long as the static methods dont access static fields or properties then i
t
should be thread safe.
Ciaran O''Donnell
http://wannabedeveloper.space.live.com
"tjain" wrote:

> Hi,
> I have a web app which has the following design to get to the DB
> Web Page (uses)
> -> .net DLL library
> --> has a class with static methods
> --> static method to-do basic DB operations like create a
> DataReader for given sql, get Dataset etc
> Big question is... Is this design safe when a lot of users are hitting th
e
> web site from the asp.net threading perspective?
>

Thread safety advice

My application uses a singleton static class for writing entries to a log
file. The location and name of the log-file is read from web.config each time
an entry is written, but has the current date inserted into its name. For
example, the string:

c:\inetpub\wwwroot\myapp\log.txt

will be changed to:

c:\inetpub\wwwroot\myapp\log_20060216.txt

However, as we have many concurrent users on the site at once, each
generating hundreds of log file entries a minute, we're finding that multiple
threads are calling the same code at the same time and we're getting filename
with multiple date stamps. Ie:

c:\inetpub\wwwroot\myapp\log_20060216_20060216.txt

I'm sure this is to do with the following code not being thread-safe despite
the "lock" critical section statement:

string sLog_File_Path = ConfigurationManager.AppSettings["logfilepath"];

lock (FoLockObject) {
sLog_File_Path = Path.Combine(Path.GetDirectoryName(sLog_File_Path) ,
Path.GetFileNameWithoutExtension(sLog_File_Path) + "_" +
DateTime.Now.ToString("yyyyMMdd") +
Path.GetExtension(sLog_File_Path));
}

Can anyone offer advice on the correct way to protect code that is
vulnerable to threading issues. I'm aware of synchronization objects such as
Mutex's but am unsure how to use them with static classes. A stright forward
example in C# without being too clever would be very handy

Thanks

BenIs sLog_File_Path local or member variable?
What about FoLockObject?

Unfortunately it' hard to say where is the problem you need to show us full
class.

George.

"Ben Fidge" <BenFidge@.discussions.microsoft.com> wrote in message
news:27A2FF7B-4565-40A5-8D95-CE197DE94166@.microsoft.com...
> My application uses a singleton static class for writing entries to a log
> file. The location and name of the log-file is read from web.config each
> time
> an entry is written, but has the current date inserted into its name. For
> example, the string:
> c:\inetpub\wwwroot\myapp\log.txt
> will be changed to:
> c:\inetpub\wwwroot\myapp\log_20060216.txt
> However, as we have many concurrent users on the site at once, each
> generating hundreds of log file entries a minute, we're finding that
> multiple
> threads are calling the same code at the same time and we're getting
> filename
> with multiple date stamps. Ie:
> c:\inetpub\wwwroot\myapp\log_20060216_20060216.txt
> I'm sure this is to do with the following code not being thread-safe
> despite
> the "lock" critical section statement:
> string sLog_File_Path = ConfigurationManager.AppSettings["logfilepath"];
> lock (FoLockObject) {
> sLog_File_Path = Path.Combine(Path.GetDirectoryName(sLog_File_Path) ,
> Path.GetFileNameWithoutExtension(sLog_File_Path) + "_" +
> DateTime.Now.ToString("yyyyMMdd") +
> Path.GetExtension(sLog_File_Path));
> }
> Can anyone offer advice on the correct way to protect code that is
> vulnerable to threading issues. I'm aware of synchronization objects such
> as
> Mutex's but am unsure how to use them with static classes. A stright
> forward
> example in C# without being too clever would be very handy
> Thanks
> Ben
Here's an abbreviated version of the full class.

public class CLog {
private static string FsLog_File_Path = "";
private static object FoLockObject = new object();

private static void RefreshSettings() {
FsLog_File_Path = ConfigurationSettings.AppSettings["logfilepath"];

lock (FoLockObject) {
FsLog_File_Path =
Path.Combine(Path.GetDirectoryName(FsLog_File_Path ),
Path.GetFileNameWithoutExtension(FsLog_File_Path) + "_" +
DateTime.Now.ToString("yyyyMMdd") +
Path.GetExtension(FsLog_File_Path));
}
}

public static void WriteInfo(string sDescription) {
RefreshSettings();

StreamWriter oStream = null;

try {
if (File.Exists(FsLog_File_Path)) {
oStream = File.AppendText(FsLog_File_Path);
}
else {
oStream = File.CreateText(FsLog_File_Path);
}

oStream.AutoFlush = true;

string sSession = "";
if (HttpContext.Current != null) sSession =
HttpContext.Current.Session.SessionID + " - ";
else sSession = "";

oStream.WriteLine(string.Format("{0} - {1} - {2}{3}",
DateTime.Now.ToString("dd MMM yyy H:mm:ss"), "INFO", sSession,
sDescription));
}
catch (IOException) {

}
finally {
if (oStream != null) oStream.Close();
oStream = null;
}
}
}

"George Ter-Saakov" wrote:

> Is sLog_File_Path local or member variable?
> What about FoLockObject?
> Unfortunately it' hard to say where is the problem you need to show us full
> class.
> George.
>
> "Ben Fidge" <BenFidge@.discussions.microsoft.com> wrote in message
> news:27A2FF7B-4565-40A5-8D95-CE197DE94166@.microsoft.com...
> > My application uses a singleton static class for writing entries to a log
> > file. The location and name of the log-file is read from web.config each
> > time
> > an entry is written, but has the current date inserted into its name. For
> > example, the string:
> > c:\inetpub\wwwroot\myapp\log.txt
> > will be changed to:
> > c:\inetpub\wwwroot\myapp\log_20060216.txt
> > However, as we have many concurrent users on the site at once, each
> > generating hundreds of log file entries a minute, we're finding that
> > multiple
> > threads are calling the same code at the same time and we're getting
> > filename
> > with multiple date stamps. Ie:
> > c:\inetpub\wwwroot\myapp\log_20060216_20060216.txt
> > I'm sure this is to do with the following code not being thread-safe
> > despite
> > the "lock" critical section statement:
> > string sLog_File_Path = ConfigurationManager.AppSettings["logfilepath"];
> > lock (FoLockObject) {
> > sLog_File_Path = Path.Combine(Path.GetDirectoryName(sLog_File_Path) ,
> > Path.GetFileNameWithoutExtension(sLog_File_Path) + "_" +
> > DateTime.Now.ToString("yyyyMMdd") +
> > Path.GetExtension(sLog_File_Path));
> > }
> > Can anyone offer advice on the correct way to protect code that is
> > vulnerable to threading issues. I'm aware of synchronization objects such
> > as
> > Mutex's but am unsure how to use them with static classes. A stright
> > forward
> > example in C# without being too clever would be very handy
> > Thanks
> > Ben
>
You do have a problem in your code.
This line is not thread safe.

FsLog_File_Path = ConfigurationSettings.AppSettings["logfilepath"];

You are modifying the variable while some other thread could have been doing
the code that is in lock {} section.
So move that line inside (but read further)

---------------
I am not sure why your are doing it this way. Because I do not see any gain
in FsLog_File_Path beign global/member variable.
I would rewrite the code to avoid any synchronization

private static void RefreshSettings() {
string sPath =
ConfigurationSettings.AppSettings["logfilepath"];
sPath = Path.Combine(Path.GetDirectoryName(sPath),
Path.GetFileNameWithoutExtension(sPath) + "_" +
DateTime.Now.ToString("yyyyMMdd") +
Path.GetExtension(sPath));
}
}

As you can see all manipulations are made to local variable so you do not
need lock.

--------------------

The only place where you will need to lock is where you are opening the file
and writing to it. Since that is not thread safe.
And this will fail if you try to open/write into file from multiple threads.

George

"Ben Fidge" <BenFidge@.discussions.microsoft.com> wrote in message
news:B6D5463F-61F6-41BB-B39D-7679E2587C0A@.microsoft.com...
> Here's an abbreviated version of the full class.
> public class CLog {
> private static string FsLog_File_Path = "";
> private static object FoLockObject = new object();
> private static void RefreshSettings() {
> FsLog_File_Path = ConfigurationSettings.AppSettings["logfilepath"];
> lock (FoLockObject) {
> FsLog_File_Path =
> Path.Combine(Path.GetDirectoryName(FsLog_File_Path ),
> Path.GetFileNameWithoutExtension(FsLog_File_Path) + "_" +
> DateTime.Now.ToString("yyyyMMdd") +
> Path.GetExtension(FsLog_File_Path));
> }
> }
> public static void WriteInfo(string sDescription) {
> RefreshSettings();
> StreamWriter oStream = null;
> try {
> if (File.Exists(FsLog_File_Path)) {
> oStream = File.AppendText(FsLog_File_Path);
> }
> else {
> oStream = File.CreateText(FsLog_File_Path);
> }
> oStream.AutoFlush = true;
> string sSession = "";
> if (HttpContext.Current != null) sSession =
> HttpContext.Current.Session.SessionID + " - ";
> else sSession = "";
>
> oStream.WriteLine(string.Format("{0} - {1} - {2}{3}",
> DateTime.Now.ToString("dd MMM yyy H:mm:ss"), "INFO", sSession,
> sDescription));
> }
> catch (IOException) {
> }
> finally {
> if (oStream != null) oStream.Close();
> oStream = null;
> }
> }
> }
> "George Ter-Saakov" wrote:
>> Is sLog_File_Path local or member variable?
>> What about FoLockObject?
>>
>> Unfortunately it' hard to say where is the problem you need to show us
>> full
>> class.
>>
>> George.
>>
>>
>> "Ben Fidge" <BenFidge@.discussions.microsoft.com> wrote in message
>> news:27A2FF7B-4565-40A5-8D95-CE197DE94166@.microsoft.com...
>> > My application uses a singleton static class for writing entries to a
>> > log
>> > file. The location and name of the log-file is read from web.config
>> > each
>> > time
>> > an entry is written, but has the current date inserted into its name.
>> > For
>> > example, the string:
>>> > c:\inetpub\wwwroot\myapp\log.txt
>>> > will be changed to:
>>> > c:\inetpub\wwwroot\myapp\log_20060216.txt
>>> > However, as we have many concurrent users on the site at once, each
>> > generating hundreds of log file entries a minute, we're finding that
>> > multiple
>> > threads are calling the same code at the same time and we're getting
>> > filename
>> > with multiple date stamps. Ie:
>>> > c:\inetpub\wwwroot\myapp\log_20060216_20060216.txt
>>> > I'm sure this is to do with the following code not being thread-safe
>> > despite
>> > the "lock" critical section statement:
>>> > string sLog_File_Path =
>> > ConfigurationManager.AppSettings["logfilepath"];
>>> > lock (FoLockObject) {
>> > sLog_File_Path = Path.Combine(Path.GetDirectoryName(sLog_File_Path) ,
>> > Path.GetFileNameWithoutExtension(sLog_File_Path) + "_" +
>> > DateTime.Now.ToString("yyyyMMdd") +
>> > Path.GetExtension(sLog_File_Path));
>> > }
>>> > Can anyone offer advice on the correct way to protect code that is
>> > vulnerable to threading issues. I'm aware of synchronization objects
>> > such
>> > as
>> > Mutex's but am unsure how to use them with static classes. A stright
>> > forward
>> > example in C# without being too clever would be very handy
>>> > Thanks
>>> > Ben
>>>
>>
>
Hi George,

I see the error of my ways and have changed it as you suggested. This was
basically a quick hack to include the date in the filename, as this wasn't
the origianl intention.

Thanks for the advice

Ben

"George Ter-Saakov" <gt-nsp@.cardone.com> wrote in message
news:OOv8TCwMGHA.3732@.TK2MSFTNGP10.phx.gbl...
> You do have a problem in your code.
> This line is not thread safe.
> FsLog_File_Path = ConfigurationSettings.AppSettings["logfilepath"];
> You are modifying the variable while some other thread could have been
> doing the code that is in lock {} section.
> So move that line inside (but read further)
> ---------------
> I am not sure why your are doing it this way. Because I do not see any
> gain in FsLog_File_Path beign global/member variable.
> I would rewrite the code to avoid any synchronization
> private static void RefreshSettings() {
> string sPath =
> ConfigurationSettings.AppSettings["logfilepath"];
> sPath = Path.Combine(Path.GetDirectoryName(sPath),
> Path.GetFileNameWithoutExtension(sPath) + "_" +
> DateTime.Now.ToString("yyyyMMdd") +
> Path.GetExtension(sPath));
> }
> }
>
> As you can see all manipulations are made to local variable so you do not
> need lock.
> --------------------
> The only place where you will need to lock is where you are opening the
> file and writing to it. Since that is not thread safe.
> And this will fail if you try to open/write into file from multiple
> threads.
> George
>
> "Ben Fidge" <BenFidge@.discussions.microsoft.com> wrote in message
> news:B6D5463F-61F6-41BB-B39D-7679E2587C0A@.microsoft.com...
>> Here's an abbreviated version of the full class.
>>
>> public class CLog {
>> private static string FsLog_File_Path = "";
>> private static object FoLockObject = new object();
>>
>> private static void RefreshSettings() {
>> FsLog_File_Path =
>> ConfigurationSettings.AppSettings["logfilepath"];
>>
>> lock (FoLockObject) {
>> FsLog_File_Path =
>> Path.Combine(Path.GetDirectoryName(FsLog_File_Path ),
>> Path.GetFileNameWithoutExtension(FsLog_File_Path) + "_" +
>> DateTime.Now.ToString("yyyyMMdd") +
>> Path.GetExtension(FsLog_File_Path));
>> }
>> }
>>
>> public static void WriteInfo(string sDescription) {
>> RefreshSettings();
>>
>> StreamWriter oStream = null;
>>
>> try {
>> if (File.Exists(FsLog_File_Path)) {
>> oStream = File.AppendText(FsLog_File_Path);
>> }
>> else {
>> oStream = File.CreateText(FsLog_File_Path);
>> }
>>
>> oStream.AutoFlush = true;
>>
>> string sSession = "";
>> if (HttpContext.Current != null) sSession =
>> HttpContext.Current.Session.SessionID + " - ";
>> else sSession = "";
>>
>>
>> oStream.WriteLine(string.Format("{0} - {1} - {2}{3}",
>> DateTime.Now.ToString("dd MMM yyy H:mm:ss"), "INFO", sSession,
>> sDescription));
>> }
>> catch (IOException) {
>>
>> }
>> finally {
>> if (oStream != null) oStream.Close();
>> oStream = null;
>> }
>> }
>> }
>>
>> "George Ter-Saakov" wrote:
>>
>>> Is sLog_File_Path local or member variable?
>>> What about FoLockObject?
>>>
>>> Unfortunately it' hard to say where is the problem you need to show us
>>> full
>>> class.
>>>
>>> George.
>>>
>>>
>>> "Ben Fidge" <BenFidge@.discussions.microsoft.com> wrote in message
>>> news:27A2FF7B-4565-40A5-8D95-CE197DE94166@.microsoft.com...
>>> > My application uses a singleton static class for writing entries to a
>>> > log
>>> > file. The location and name of the log-file is read from web.config
>>> > each
>>> > time
>>> > an entry is written, but has the current date inserted into its name.
>>> > For
>>> > example, the string:
>>>>> > c:\inetpub\wwwroot\myapp\log.txt
>>>>> > will be changed to:
>>>>> > c:\inetpub\wwwroot\myapp\log_20060216.txt
>>>>> > However, as we have many concurrent users on the site at once, each
>>> > generating hundreds of log file entries a minute, we're finding that
>>> > multiple
>>> > threads are calling the same code at the same time and we're getting
>>> > filename
>>> > with multiple date stamps. Ie:
>>>>> > c:\inetpub\wwwroot\myapp\log_20060216_20060216.txt
>>>>> > I'm sure this is to do with the following code not being thread-safe
>>> > despite
>>> > the "lock" critical section statement:
>>>>> > string sLog_File_Path =
>>> > ConfigurationManager.AppSettings["logfilepath"];
>>>>> > lock (FoLockObject) {
>>> > sLog_File_Path = Path.Combine(Path.GetDirectoryName(sLog_File_Path) ,
>>> > Path.GetFileNameWithoutExtension(sLog_File_Path) + "_" +
>>> > DateTime.Now.ToString("yyyyMMdd") +
>>> > Path.GetExtension(sLog_File_Path));
>>> > }
>>>>> > Can anyone offer advice on the correct way to protect code that is
>>> > vulnerable to threading issues. I'm aware of synchronization objects
>>> > such
>>> > as
>>> > Mutex's but am unsure how to use them with static classes. A stright
>>> > forward
>>> > example in C# without being too clever would be very handy
>>>>> > Thanks
>>>>> > Ben
>>>>>
>>>
>>>

Thread safety advice

My application uses a singleton static class for writing entries to a log
file. The location and name of the log-file is read from web.config each tim
e
an entry is written, but has the current date inserted into its name. For
example, the string:
c:\inetpub\wwwroot\myapp\log.txt
will be changed to:
c:\inetpub\wwwroot\myapp\log_20060216.txt
However, as we have many concurrent users on the site at once, each
generating hundreds of log file entries a minute, we're finding that multipl
e
threads are calling the same code at the same time and we're getting filenam
e
with multiple date stamps. Ie:
c:\inetpub\wwwroot\myapp\log_20060216_20
060216.txt
I'm sure this is to do with the following code not being thread-safe despite
the "lock" critical section statement:
string sLog_File_Path = ConfigurationManager.AppSettings["logfilepath"];
lock (FoLockObject) {
sLog_File_Path = Path.Combine(Path.GetDirectoryName(sLog_File_Path),
Path. GetFileNameWithoutExtension(sLog_File_Pa
th) + "_" +
DateTime.Now.ToString("yyyyMMdd") +
Path.GetExtension(sLog_File_Path));
}
Can anyone offer advice on the correct way to protect code that is
vulnerable to threading issues. I'm aware of synchronization objects such as
Mutex's but am unsure how to use them with static classes. A stright forward
example in C# without being too clever would be very handy
Thanks
BenIs sLog_File_Path local or member variable?
What about FoLockObject?
Unfortunately it' hard to say where is the problem you need to show us full
class.
George.
"Ben Fidge" <BenFidge@.discussions.microsoft.com> wrote in message
news:27A2FF7B-4565-40A5-8D95-CE197DE94166@.microsoft.com...
> My application uses a singleton static class for writing entries to a log
> file. The location and name of the log-file is read from web.config each
> time
> an entry is written, but has the current date inserted into its name. For
> example, the string:
> c:\inetpub\wwwroot\myapp\log.txt
> will be changed to:
> c:\inetpub\wwwroot\myapp\log_20060216.txt
> However, as we have many concurrent users on the site at once, each
> generating hundreds of log file entries a minute, we're finding that
> multiple
> threads are calling the same code at the same time and we're getting
> filename
> with multiple date stamps. Ie:
> c:\inetpub\wwwroot\myapp\log_20060216_20
060216.txt
> I'm sure this is to do with the following code not being thread-safe
> despite
> the "lock" critical section statement:
> string sLog_File_Path = ConfigurationManager.AppSettings["logfilepath"];
> lock (FoLockObject) {
> sLog_File_Path = Path.Combine(Path.GetDirectoryName(sLog_File_Path),
> Path. GetFileNameWithoutExtension(sLog_File_Pa
th) + "_" +
> DateTime.Now.ToString("yyyyMMdd") +
> Path.GetExtension(sLog_File_Path));
> }
> Can anyone offer advice on the correct way to protect code that is
> vulnerable to threading issues. I'm aware of synchronization objects such
> as
> Mutex's but am unsure how to use them with static classes. A stright
> forward
> example in C# without being too clever would be very handy
> Thanks
> Ben
>
Here's an abbreviated version of the full class.
public class CLog {
private static string FsLog_File_Path = "";
private static object FoLockObject = new object();
private static void RefreshSettings() {
FsLog_File_Path = ConfigurationSettings.AppSettings["logfilepath"];
lock (FoLockObject) {
FsLog_File_Path =
Path.Combine(Path.GetDirectoryName(FsLog_File_Path),
Path. GetFileNameWithoutExtension(FsLog_File_P
ath) + "_" +
DateTime.Now.ToString("yyyyMMdd") +
Path.GetExtension(FsLog_File_Path));
}
}
public static void WriteInfo(string sDescription) {
RefreshSettings();
StreamWriter oStream = null;
try {
if (File.Exists(FsLog_File_Path)) {
oStream = File.AppendText(FsLog_File_Path);
}
else {
oStream = File.CreateText(FsLog_File_Path);
}
oStream.AutoFlush = true;
string sSession = "";
if (HttpContext.Current != null) sSession =
HttpContext.Current.Session.SessionID + " - ";
else sSession = "";
oStream.WriteLine(string.Format("{0} - {1} - {2}{3}",
DateTime.Now.ToString("dd MMM yyy H:mm:ss"), "INFO", sSession,
sDescription));
}
catch (IOException) {
}
finally {
if (oStream != null) oStream.Close();
oStream = null;
}
}
}
"George Ter-Saakov" wrote:

> Is sLog_File_Path local or member variable?
> What about FoLockObject?
> Unfortunately it' hard to say where is the problem you need to show us fu
ll
> class.
> George.
>
> "Ben Fidge" <BenFidge@.discussions.microsoft.com> wrote in message
> news:27A2FF7B-4565-40A5-8D95-CE197DE94166@.microsoft.com...
>
>
You do have a problem in your code.
This line is not thread safe.
FsLog_File_Path = ConfigurationSettings.AppSettings["logfilepath"];
You are modifying the variable while some other thread could have been doing
the code that is in lock {} section.
So move that line inside (but read further)
---
I am not sure why your are doing it this way. Because I do not see any gain
in FsLog_File_Path beign global/member variable.
I would rewrite the code to avoid any synchronization
private static void RefreshSettings() {
string sPath =
ConfigurationSettings.AppSettings["logfilepath"];
sPath = Path.Combine(Path.GetDirectoryName(sPath),
Path.GetFileNameWithoutExtension(sPath) + "_" +
DateTime.Now.ToString("yyyyMMdd") +
Path.GetExtension(sPath));
}
}
As you can see all manipulations are made to local variable so you do not
need lock.
----
The only place where you will need to lock is where you are opening the file
and writing to it. Since that is not thread safe.
And this will fail if you try to open/write into file from multiple threads.
George
"Ben Fidge" <BenFidge@.discussions.microsoft.com> wrote in message
news:B6D5463F-61F6-41BB-B39D-7679E2587C0A@.microsoft.com...
> Here's an abbreviated version of the full class.
> public class CLog {
> private static string FsLog_File_Path = "";
> private static object FoLockObject = new object();
> private static void RefreshSettings() {
> FsLog_File_Path = ConfigurationSettings.AppSettings["logfilepath"];
> lock (FoLockObject) {
> FsLog_File_Path =
> Path.Combine(Path.GetDirectoryName(FsLog_File_Path),
> Path. GetFileNameWithoutExtension(FsLog_File_P
ath) + "_" +
> DateTime.Now.ToString("yyyyMMdd") +
> Path.GetExtension(FsLog_File_Path));
> }
> }
> public static void WriteInfo(string sDescription) {
> RefreshSettings();
> StreamWriter oStream = null;
> try {
> if (File.Exists(FsLog_File_Path)) {
> oStream = File.AppendText(FsLog_File_Path);
> }
> else {
> oStream = File.CreateText(FsLog_File_Path);
> }
> oStream.AutoFlush = true;
> string sSession = "";
> if (HttpContext.Current != null) sSession =
> HttpContext.Current.Session.SessionID + " - ";
> else sSession = "";
>
> oStream.WriteLine(string.Format("{0} - {1} - {2}{3}",
> DateTime.Now.ToString("dd MMM yyy H:mm:ss"), "INFO", sSession,
> sDescription));
> }
> catch (IOException) {
> }
> finally {
> if (oStream != null) oStream.Close();
> oStream = null;
> }
> }
> }
> "George Ter-Saakov" wrote:
>
Hi George,
I see the error of my ways and have changed it as you suggested. This was
basically a quick hack to include the date in the filename, as this wasn't
the origianl intention.
Thanks for the advice
Ben
"George Ter-Saakov" <gt-nsp@.cardone.com> wrote in message
news:OOv8TCwMGHA.3732@.TK2MSFTNGP10.phx.gbl...
> You do have a problem in your code.
> This line is not thread safe.
> FsLog_File_Path = ConfigurationSettings.AppSettings["logfilepath"];
> You are modifying the variable while some other thread could have been
> doing the code that is in lock {} section.
> So move that line inside (but read further)
> ---
> I am not sure why your are doing it this way. Because I do not see any
> gain in FsLog_File_Path beign global/member variable.
> I would rewrite the code to avoid any synchronization
> private static void RefreshSettings() {
> string sPath =
> ConfigurationSettings.AppSettings["logfilepath"];
> sPath = Path.Combine(Path.GetDirectoryName(sPath),
> Path.GetFileNameWithoutExtension(sPath) + "_" +
> DateTime.Now.ToString("yyyyMMdd") +
> Path.GetExtension(sPath));
> }
> }
>
> As you can see all manipulations are made to local variable so you do not
> need lock.
> ----
> The only place where you will need to lock is where you are opening the
> file and writing to it. Since that is not thread safe.
> And this will fail if you try to open/write into file from multiple
> threads.
> George
>
> "Ben Fidge" <BenFidge@.discussions.microsoft.com> wrote in message
> news:B6D5463F-61F6-41BB-B39D-7679E2587C0A@.microsoft.com...
>

Thread Safety and ASP.NET

I use theHybrid Dictionary Object often and was advised not to use it in ASP.NET because it is not Thread Safe.

What does it really mean to not be Thread Safe; will some other thread modify the contents of the collection by mistake?ASP.NEt is multithreaded environment. In fact each user session uses its own thread. Something that is accessible to multiply threads (like static fields, application object items etc.) could be accessed simultaneously from different threads. Classes, which designed to allow this kind of access and still behave correctly, are called Thread Safe.
So, why would someone recommend that i not use an object that isn't thread safe with asp.net? Is it because the object was not designed to run in a multi-threaded environment [and may not always work]? Or is it because the object (which is not multi threaded) could reduce the scaleability of my code?
It can lock; it can crash or behave unexpected.
No no no no. The person who said that does not know what he's talking about.
Non thread safe objects should not be exchanged between threads without some precautions. But as long as you stay in one thread, you're perfectly safe to use them.
For example, from within a page request, it's perfectly ok. It starts to be dangerous if you put such an object in Session or Application where it can be accessed by several threads simultaneously.
Hi Bertrand,
Even within separate thread using such an object could be dangerous. For example if it make use of internal static resources (some kind of singleton pattern).

Thread safety issue

In my VB.Net ASP project, I have a function in a module (same thing as a
static function in C#'s terms) that is shared by all the sessions. What is
does once called is to write messages into a common log file that's also
shared by all the sessions. To make sure it is thread safe, I use the Monitor
class to sync it up. Here is the simplified version of that function"

Public Sub WriteLog(ByVal strMsg As String)
Dim oWriter As StreamWriter
Dim strLogPath As String = "C:\Logs\test.log"

If Not File.Exists(strLogPath) Then
oWriter = File.CreateText(strLogPath)
Else
oWriter = File.AppendText(strLogPath)
End If

Monitor.Enter(oWriter)
Try
oWriter.WriteLine(strMsg)
oWriter.Flush()
oWriter.Close()
Finally
Monitor.Exit(oWriter)
End Try
End Sub

My problem is that this code doesn't seem to do what I want it to do. I am
still getting error that says "The process cannot access the file
'C:\Logs\test.log' because it is used by another process.". Why is this
happening? I must be doing something wrong. Can some one tell me how I should
do this?

Thanks a million!

Fengthe object you are locking on is local to the call, so threads do not block
each other because they are all locking different objects.

you need to lock on a static object. you could use the class type, but there
is a performance hit for this, better to create an actual object to lock on.
also the File.Exists need to be in lock.

-- bruce (sqlwork.com)

"Feng" <Feng@.discussions.microsoft.com> wrote in message
news:159CE973-735B-4ED4-BC21-EA7BA0760E20@.microsoft.com...
> In my VB.Net ASP project, I have a function in a module (same thing as a
> static function in C#'s terms) that is shared by all the sessions. What is
> does once called is to write messages into a common log file that's also
> shared by all the sessions. To make sure it is thread safe, I use the
> Monitor
> class to sync it up. Here is the simplified version of that function"
> Public Sub WriteLog(ByVal strMsg As String)
> Dim oWriter As StreamWriter
> Dim strLogPath As String = "C:\Logs\test.log"
> If Not File.Exists(strLogPath) Then
> oWriter = File.CreateText(strLogPath)
> Else
> oWriter = File.AppendText(strLogPath)
> End If
> Monitor.Enter(oWriter)
> Try
> oWriter.WriteLine(strMsg)
> oWriter.Flush()
> oWriter.Close()
> Finally
> Monitor.Exit(oWriter)
> End Try
> End Sub
> My problem is that this code doesn't seem to do what I want it to do. I am
> still getting error that says "The process cannot access the file
> 'C:\Logs\test.log' because it is used by another process.". Why is this
> happening? I must be doing something wrong. Can some one tell me how I
> should
> do this?
> Thanks a million!
> Feng

Thread safety when subclassing the Page class

Hi all,

I've declared a class called SecurePage that inherits from the Page class
provided by the framework.

In this class I've declared a protected member called sessionState and a
method called loadSessionState(). loadSessionState initialises the
sessionState object when it's called.

My question is, seeing as the member variable sessionState is declared
globally at the top of SecurePage, do i need to worry about threads or is
the variable isolated per every request?

Thanks all

tceThere is an instance of the SecurePage class (and this the variable)
for every request, so this will be thread safe.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Fri, 10 Dec 2004 12:21:46 -0000, "thechaosengine"
<sh856531@.microsofts_free_email_service.com> wrote:

>Hi all,
>I've declared a class called SecurePage that inherits from the Page class
>provided by the framework.
>In this class I've declared a protected member called sessionState and a
>method called loadSessionState(). loadSessionState initialises the
>sessionState object when it's called.
>My question is, seeing as the member variable sessionState is declared
>globally at the top of SecurePage, do i need to worry about threads or is
>the variable isolated per every request?
>Thanks all
>tce
Should have read:

There is an instance of the SecurePage class (and THUS the variable)
for every request, so this will be thread safe.

I'm having real problems with basic communication skills this morning,
apologies.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Fri, 10 Dec 2004 09:17:38 -0500, Scott Allen
<bitmask@.[nospam].fred.net> wrote:

>There is an instance of the SecurePage class (and this the variable)
>for every request, so this will be thread safe.

Thread safety when subclassing the Page class

Hi all,
I've declared a class called SecurePage that inherits from the Page class
provided by the framework.
In this class I've declared a protected member called sessionState and a
method called loadSessionState(). loadSessionState initialises the
sessionState object when it's called.
My question is, seeing as the member variable sessionState is declared
globally at the top of SecurePage, do i need to worry about threads or is
the variable isolated per every request?
Thanks all
tceThere is an instance of the SecurePage class (and this the variable)
for every request, so this will be thread safe.
Scott
http://www.OdeToCode.com/blogs/scott/
On Fri, 10 Dec 2004 12:21:46 -0000, "thechaosengine"
<sh856531@.microsofts_free_email_service.com> wrote:

>Hi all,
>I've declared a class called SecurePage that inherits from the Page class
>provided by the framework.
>In this class I've declared a protected member called sessionState and a
>method called loadSessionState(). loadSessionState initialises the
>sessionState object when it's called.
>My question is, seeing as the member variable sessionState is declared
>globally at the top of SecurePage, do i need to worry about threads or is
>the variable isolated per every request?
>Thanks all
>tce
>
Should have read:
There is an instance of the SecurePage class (and THUS the variable)
for every request, so this will be thread safe.
I'm having real problems with basic communication skills this morning,
apologies.
Scott
http://www.OdeToCode.com/blogs/scott/
On Fri, 10 Dec 2004 09:17:38 -0500, Scott Allen
<bitmask@.[nospam].fred.net> wrote:

>There is an instance of the SecurePage class (and this the variable)
>for every request, so this will be thread safe.

Thread Safety?

i have an asp.net application which almost all of its methods are static methods.
is it prone for bugs and thread collisions or something?

No. There is nothing like that a static function is prone to bugs or collisions. Firstly, static member functions cannot access non-static members directly. A class member declared with the keywordstatic is a static member. A static member is owned by the class, not by its instances (objects of the class).
Note that static members are actually class members, while non-static members are instance members (means they are owned by the instances). Both in C# & VB.NET, we may create static/shared events, properties, fields and functions.

Due to the independent nature of static functions, it wont be affected by other threads at all.

HTH

Cheers

Vishal Khanna

(pls mark as answer if reply helps)

Thread Safety?

The following code hasbeen provided to me by a partner company for use in our asp.netapplication. Here is the class that I am questioning:

public class CurrentPageStyle
{
/// <summary>
/// The PageStyleRow for the current request.
/// </summary>
private static PageStylesDefinition.PageStyleRow currentpagestylerow;

/// <param name="ctxt">This gets the current pagestyle</param>
public CurrentPageStyle(HttpContext ctxt)
{
string keyword = ctxt.Request.QueryString["DView"];
if (keyword != null)
{
currentpagestylerow = TKConfiguration.Page_Styles_Definition.FindAll(keyword);
}
else
{
PageStylesDefinition.PageStylesRow toppsr = TKConfiguration.Page_Styles_Definition.GetFirst();
currentpagestylerow = TKConfiguration.Page_Styles_Definition.FindAll(toppsr.DefaultKeyword);
}
}

/// <summary>
/// This initializes the static member
/// </summary>
static CurrentPageStyle()
{
currentpagestylerow = null;
}

/// <summary>
/// Access to the current page style row for the request.
/// </summary>
public static PageStylesDefinition.PageStyleRow CurrentPageStyleRow
{
get
{
return currentpagestylerow;
}
set
{
currentpagestylerow = value;
}
}

Here is the usage from the global.asax file:

protected void Application_AcquireRequestState(object sender, EventArgs e)
{
CurrentPageStyle.CurrentPageStyleRow = null;
string[] pagestyle = Request.QueryString.GetValues('DView');
if (pagestyle != null)
{
if (pagestyle.Length != 0)
{
PageStylesDefinition.PageStyleRow psr = TKConfiguration.Page_Styles_Definition.FindAll(pagestyle[0]);
CurrentPageStyle.CurrentPageStyleRow = psr;
}
}

}

Myconcern is that the Application_AcquireRequestState event handler iscalled for every web request and it is setting a staticCurrentPageStyleRow which is then used later in the application. Myunderstanding is that there should only be one staticCurrentPageStyleRow per application and there can be web requests onmultiple threads so all of the threads will be sharing the sameCurrentPageStyleRow. Since there can be a differentCurrentPageStyleRow for each request, I think this is not thread safeand user 1 could end up getting user 2's CurrentPageStyleRow. I wantto make sure I am correct about this before reporting the issue to thepartner company. Am I right or am I off base?

Thanks,
Eric

In my modest opinion you should move that to an HttpModule to capture the request, and yes do not use an static class member! Why can't be an instance like the class?
Nice finding Eric! However some more people here may want to comment! Don't take my word for it

Al,

I am in the process of moving this to an HttpModule. As I said, the code was supplied by a partner company. I am in the process of adapting it to meet our needs. That was when I ran across this bit of code that does not appear to be thread safe. I am the only experienced .net developer on staff here, so I wanted someone else review the code and confirm my concerns before I reported it as a bug.

Thanks,
Eric

Thread Safety?

The first application I wrote using asp.net started off rather small,
and as a result, the design of the application took a "Rapid
Application Development" type of approach. By this I mean that it was
simply built using a simple 3 tier architecture, where the interface
was created using css and master pages, the middle layer or business
logic classes, consisted of some simple public classes that defined
some of the business rules, and the underlying data layer contained
all the stored procedures and database schema. Everything worked fine,
since it was a rather simple application, and it was only being used
by a few people. Well, as some of you may already know, after each
demo, the new requirements kept getting added to the application, and
now it is turning into a huge application that is expected to be used
by hundreds of people. Incorporating a design pattern into the
application would have been a good idea, but I was not familiar with
some of the patterns when I started writing the application. I am also
not experienced with thread safety. Considering I am limited on
time(only a couple of ws(2 or 3)), what are some things you experts
can suggest I take to prevent some unforeseen problems? FYI, All the
classes in my app_code folder are declared public. Nothing is static.carlos wrote:
> The first application I wrote using asp.net started off rather small,
> and as a result, the design of the application took a "Rapid
> Application Development" type of approach. By this I mean that it was
> simply built using a simple 3 tier architecture, where the interface
> was created using css and master pages, the middle layer or business
> logic classes, consisted of some simple public classes that defined
> some of the business rules, and the underlying data layer contained
> all the stored procedures and database schema.
That sounds rather good. If it's really a three tier application, where
the presentation layer is completely isolated from the data layer, it's
pretty good.
If, on the other hand, the application only pretends to be layered, it's
not very good. This is usually visible by the fact that the presentation
layer is littered with database field names.

> Everything worked fine,
> since it was a rather simple application, and it was only being used
> by a few people. Well, as some of you may already know, after each
> demo, the new requirements kept getting added to the application, and
> now it is turning into a huge application that is expected to be used
> by hundreds of people. Incorporating a design pattern into the
> application would have been a good idea, but I was not familiar with
> some of the patterns when I started writing the application. I am also
> not experienced with thread safety. Considering I am limited on
> time(only a couple of ws(2 or 3)), what are some things you experts
> can suggest I take to prevent some unforeseen problems? FYI, All the
> classes in my app_code folder are declared public. Nothing is static.
If you don't have any static variables, you are pretty safe when it
comes to thread safety. As long as each instance of an object only is
used in one thread, it's safe. Also, only one page at a time is executed
for each session, so any data that is session specific is also thread
safe in an ASP.NET application.
Gran Andersson
_____
http://www.guffa.com
I'll chime in here to say don't implement patterns for the sake of patterns
either. Patterns solve specific problems. Don't invent the problem just so
you can apply the pattern in code. I used to be guilty of that - it made my
code overly complicated. Cool for me, difficult for others to maintain.
Regards,
Alvin Bruney
---
Shameless author plug
Excel Services for .NET - MS Press
Professional VSTO 2005 - Wrox/Wiley
OWC Black Book www.lulu.com/owc
"Gran Andersson" <guffa@.guffa.com> wrote in message
news:%23OneyVI3HHA.212@.TK2MSFTNGP05.phx.gbl...
> carlos wrote:
> That sounds rather good. If it's really a three tier application, where
> the presentation layer is completely isolated from the data layer, it's
> pretty good.
> If, on the other hand, the application only pretends to be layered, it's
> not very good. This is usually visible by the fact that the presentation
> layer is littered with database field names.
>
> If you don't have any static variables, you are pretty safe when it comes
> to thread safety. As long as each instance of an object only is used in
> one thread, it's safe. Also, only one page at a time is executed for each
> session, so any data that is session specific is also thread safe in an
> ASP.NET application.
> --
> Gran Andersson
> _____
> http://www.guffa.com

Thread Safety?

The first application I wrote using asp.net started off rather small,
and as a result, the design of the application took a "Rapid
Application Development" type of approach. By this I mean that it was
simply built using a simple 3 tier architecture, where the interface
was created using css and master pages, the middle layer or business
logic classes, consisted of some simple public classes that defined
some of the business rules, and the underlying data layer contained
all the stored procedures and database schema. Everything worked fine,
since it was a rather simple application, and it was only being used
by a few people. Well, as some of you may already know, after each
demo, the new requirements kept getting added to the application, and
now it is turning into a huge application that is expected to be used
by hundreds of people. Incorporating a design pattern into the
application would have been a good idea, but I was not familiar with
some of the patterns when I started writing the application. I am also
not experienced with thread safety. Considering I am limited on
time(only a couple of weeks(2 or 3)), what are some things you experts
can suggest I take to prevent some unforeseen problems? FYI, All the
classes in my app_code folder are declared public. Nothing is static.carlos wrote:

Quote:

Originally Posted by

The first application I wrote using asp.net started off rather small,
and as a result, the design of the application took a "Rapid
Application Development" type of approach. By this I mean that it was
simply built using a simple 3 tier architecture, where the interface
was created using css and master pages, the middle layer or business
logic classes, consisted of some simple public classes that defined
some of the business rules, and the underlying data layer contained
all the stored procedures and database schema.


That sounds rather good. If it's really a three tier application, where
the presentation layer is completely isolated from the data layer, it's
pretty good.

If, on the other hand, the application only pretends to be layered, it's
not very good. This is usually visible by the fact that the presentation
layer is littered with database field names.

Quote:

Originally Posted by

Everything worked fine,
since it was a rather simple application, and it was only being used
by a few people. Well, as some of you may already know, after each
demo, the new requirements kept getting added to the application, and
now it is turning into a huge application that is expected to be used
by hundreds of people. Incorporating a design pattern into the
application would have been a good idea, but I was not familiar with
some of the patterns when I started writing the application. I am also
not experienced with thread safety. Considering I am limited on
time(only a couple of weeks(2 or 3)), what are some things you experts
can suggest I take to prevent some unforeseen problems? FYI, All the
classes in my app_code folder are declared public. Nothing is static.


If you don't have any static variables, you are pretty safe when it
comes to thread safety. As long as each instance of an object only is
used in one thread, it's safe. Also, only one page at a time is executed
for each session, so any data that is session specific is also thread
safe in an ASP.NET application.

--
Gran Andersson
_____
http://www.guffa.com
I'll chime in here to say don't implement patterns for the sake of patterns
either. Patterns solve specific problems. Don't invent the problem just so
you can apply the pattern in code. I used to be guilty of that - it made my
code overly complicated. Cool for me, difficult for others to maintain.

--
Regards,
Alvin Bruney
----------------
Shameless author plug
Excel Services for .NET - MS Press
Professional VSTO 2005 - Wrox/Wiley
OWC Black Book www.lulu.com/owc
"Gran Andersson" <guffa@.guffa.comwrote in message
news:%23OneyVI3HHA.212@.TK2MSFTNGP05.phx.gbl...

Quote:

Originally Posted by

carlos wrote:

Quote:

Originally Posted by

>The first application I wrote using asp.net started off rather small,
>and as a result, the design of the application took a "Rapid
>Application Development" type of approach. By this I mean that it was
>simply built using a simple 3 tier architecture, where the interface
>was created using css and master pages, the middle layer or business
>logic classes, consisted of some simple public classes that defined
>some of the business rules, and the underlying data layer contained
>all the stored procedures and database schema.


>
That sounds rather good. If it's really a three tier application, where
the presentation layer is completely isolated from the data layer, it's
pretty good.
>
If, on the other hand, the application only pretends to be layered, it's
not very good. This is usually visible by the fact that the presentation
layer is littered with database field names.
>

Quote:

Originally Posted by

>Everything worked fine,
>since it was a rather simple application, and it was only being used
>by a few people. Well, as some of you may already know, after each
>demo, the new requirements kept getting added to the application, and
>now it is turning into a huge application that is expected to be used
>by hundreds of people. Incorporating a design pattern into the
>application would have been a good idea, but I was not familiar with
>some of the patterns when I started writing the application. I am also
>not experienced with thread safety. Considering I am limited on
>time(only a couple of weeks(2 or 3)), what are some things you experts
>can suggest I take to prevent some unforeseen problems? FYI, All the
>classes in my app_code folder are declared public. Nothing is static.


>
If you don't have any static variables, you are pretty safe when it comes
to thread safety. As long as each instance of an object only is used in
one thread, it's safe. Also, only one page at a time is executed for each
session, so any data that is session specific is also thread safe in an
ASP.NET application.
>
--
Gran Andersson
_____
http://www.guffa.com