Are static delegates thread-safe?
- by leypascua
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?