views:

621

answers:

3

Hi guys,

i have to send emails when a person recieves a personal message on my website and for that i use a StringBuilder to create the HTML markup of the email.

also since it is required at many other places as well i have made a Shared Fucntion (i am using VB.NET). now my only concern is that since shared functions are shared among all objects and maybe asp.net sessions, can it be possible that before one person email is being formed and another person access the same function, it would cause the data in the stringbuilder to be overwritten..

currently my site doesn't have many users but can this become an issue in the future...please advise me on this...is there any better way or standard procedure to follow when using shared functions.

also at one time i made the mistake of using a shared connection object and it would cause close if many people were to access it

+2  A: 

Shared functions can only access static/global variables as well as variable inside the function scope. So, if the the function is working with any number of static/shared resources then you need to synchronize between the calls to the function.

In your case, however, it doesn't sound like you're working with any shared resources, so it shouldn't be a problem.

Here's a case that illustrates the problem:

 private static myCounter As Integer = 0

 public shared function IncreaseCount() As Integer
    myCounter += 1

    for i as integer = 0 to 10 million
      //'do extensive work
    next

    return myCounter
 End Function

Imagine that you call the function for the first time, and you would expect it to return the number 1. But due to the fact that the function was called again before the first function call got to return the counter was increased once more, which means that both function calls return 2 instead of respectively 1 and 2. All the problem arrives when you want several things working on the same static resource.

Qua
thanks a lot....that clears some doubts
Pankaj Kumar
That isn't thread safe; you should be using Interlocked.Increment
Marc Gravell
I was merely pointing out the problems of having shared resources - not building a fool-proof that'd add noise to my point.
Qua
+1  A: 

Instead of using a static method you can have an EmailSender object attach to current HttpContext.This way each user will have its own EmailSender instance. Here's the code in C# :

private static EmailSender _instance;
public static EmailSender GetEmailSender()
{
if(System.Web.HttpContext.Current != null)
{
    if(! System.Web.HttpContext.Current.Items.ContainsKey("EmailSender"))
       System.Web.HttpContext.Items["EmailSender"]=new EmailSender();\
    return (EmailSender)System.Web.HttpContext.Current.Items["EmailSender"];

}
if(_instance==null)
   _instance=new EmailSender();
return _instance;
}

It will work in web and windows application. now every time you want to send an email you can do as follows:

GetEmailSender().SendMail(MailInfo mailInfo);
Beatles1692
Thanks, i will try this and get back to you
Pankaj Kumar
You're welcome!
Beatles1692
+1  A: 

Also, if you're using VB.NET on Framework 3.5, you may want to look into using XML literals to build your HTML instead of StringBuilder. XML literals will make your code SUPREMELY more readable, and allow for very easy insertion of data into your message.

As a SIMPLE example...

Dim msg = <html><body>  
                         Message sent at <%= Now.ToString() %>  
                 </body></html>

myMailMessage.IsBodyHtml = True
myMailMessage.Body = msg.ToString()
eidylon
wow, if thats possible then it will definitely make my work much easier.thanks for the tip...
Pankaj Kumar