Are static delegates thread-safe?

Posted by leypascua on Stack Overflow See other posts from Stack Overflow or by leypascua
Published on 2010-06-15T07:27:10Z Indexed on 2010/06/15 7:42 UTC
Read the original article Hit count: 224

Consider this code snippet:

public static class ApplicationContext
{
    private static Func<TService> Uninitialized<TService>()
    {
        throw new InvalidOperationException();
    }

    public static Func<IAuthenticationProvider> AuthenticationProvider = Uninitialized<IAuthenticationProvider>();
    public static Func<IUnitOfWorkFactory> UnitOfWorkFactory = Uninitialized<IUnitOfWorkFactory>();
}

//can also be in global.asax if used in a web app.
public static void Main(string[] args) 
{
    ApplicationContext.AuthenticationProvider = () => new LdapAuthenticationProvider();
    ApplicationContext.UnitOfWorkFactory = () => new EFUnitOfWorkFactory();
}

//somewhere in the code.. say an ASP.NET MVC controller
ApplicationContext.AuthenticationProvider().SignIn(username, true);

Are delegates in the static class ApplicationContext thread-safe in the sense that multiple-threads can invoke them?

What potential problems will I face if I pursue this approach?

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET