This way accessing _userService will be thread safe but calling
LoadCurrentUser will not; that method could actually be executed by
multiple threads in parallel thus breaking the thread safety.
Imho using a synchronization object would do the trick and make that
method really thread safe.

Frans, what do you think?

--
Efran Cobisi
http://www.cobisi.com

Frans Bouma wrote:
        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

===================================
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