Link to home
Start Free TrialLog in
Avatar of mikha
mikhaFlag for United States of America

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.GetService<AuthService>(); ;
 
   protected override bool AuthorizeCore (HttpContextBase httpContext) {
        if (httpContext.User.Identity.IsAuthenticated == false)
            return false;
       
       return  authService.IsAuthorized();
   }
}
ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mikha

ASKER

@ste5an - thanks for your comment.

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?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial