> I have the following class:
>
> public sealed class UserHelper
> {
>     private static IUserService _userService;
>
>     public static SiteUser LoadCurrentUser()
>     {
>         if ((HttpContext.Current.User == null) ||
> (!HttpContext.Current.User.Identity.IsAuthenticated))
>             throw new Exception("User is not authenticated.");
>         if (_userService == null)
>             _userService =
> ContainerWebAccessorUtil.Container.Resolve<IUserService>();
>         return _userService.GetSiteUserByUid(new
> Guid(HttpContext.Current.User.Identity.Name));
>       }
> }
>
> My question is, is this method thread safe from an ASP.NET application?

        no. You should do:
private static IUserService _userService =
ContainerWebAccessorUtil.Container.Resolve<IUserService>();

public static SiteUser LoadCurrentUser()
    {
        if ((HttpContext.Current.User == null) ||
(!HttpContext.Current.User.Identity.IsAuthenticated))
            throw new Exception("User is not authenticated.");
        return _userService.GetSiteUserByUid(new
Guid(HttpContext.Current.User.Identity.Name));
      }

        i.o.w.: initialize the singleton in the static declaration, otherwise
it's not threadsafe. Please see the excellent articles about this by Jon
Skeet:
http://www.yoda.arachsys.com/csharp/singleton.html

                FB

------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to