I need to point out that this implementation only guarantees that
userService_ will only be initialized once;
ContainerWebAccessorUtil.Container is not thread-safe, and
ContainerType.Resolve ditto, but you can make them be thread-safe.

On 4/30/07, Stoyan Damov <[EMAIL PROTECTED]> wrote:
public static SiteUser LoadCurrentUser()
{
...
    if (userService_ == null)
    {
        lock (userServiceMx_)
        {
            if (userService_ == null)
            {
                IUserService service =
ContainerWebAccessorUtil.Container.Resolve<IUserService>();
                Thread.MemoryBarrier();
                // ^ makes sure all out-of-order writes have completed
                userService_ = service;
            }
        }
    }
    return userService_.GetSiteUserByUid(new
Guid(HttpContext.Current.User.Identity.Name));
}

...
private static object userServiceMx_ = new object();

Cheers,
Stoyan

On 4/26/07, Paul Cowan <[EMAIL PROTECTED]> wrote:
> Hi,
>
> 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?
>
> Thanks
> Paul
> [EMAIL PROTECTED]



--

Cheers,
Stoyan

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