mikha
asked on
proper way to initialize a static member in C# class
I have the following code sample. I have an static member in my custom attribute class.
what is a proper way to initialize this member.
1. i'm initializing it using a UNITY dependency resolver, when the member is declared. will this work? or do i need a static constructor.
public class MyAuthorizeAttribute : AuthorizeAttribute {
public static AuthService authService = DependencyResolver.Current .GetServic e<AuthServ ice>(); ;
protected override bool AuthorizeCore (HttpContextBase httpContext) {
if (httpContext.User.Identity .IsAuthent icated == false)
return false;
return authService.IsAuthorized() ;
}
}
what is a proper way to initialize this member.
1. i'm initializing it using a UNITY dependency resolver, when the member is declared. will this work? or do i need a static constructor.
public class MyAuthorizeAttribute : AuthorizeAttribute {
public static AuthService authService = DependencyResolver.Current
protected override bool AuthorizeCore (HttpContextBase httpContext) {
if (httpContext.User.Identity
return false;
return authService.IsAuthorized()
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
regarding ASP.NET and more than one application pool - if there is a way to register the service as singleton using UNITY, then that should make it singleton.
If registering it as singleton (using any dependency injection container, not just UNITY). will that cause any problems in ASP.NET?