I need to initialize some data that will be static for all instances of my particular controller. Once its initialized, I basically add this data as a custom property on the controller's descriptor by overriding the Initialize method. My question is....apart
from doing the initialization in a static constructor of the controller, is there any other (or better) way of doing this? The nature of the data is such that it will not change until I actually change the class definition (basically, its related to a bunch
of class and method level attributes).
Thanks,
Priya
Please "Mark as Answer" if this resolves your query. Thanks!
Thanks....but it kinda gets complicated if this controller is a base controller...in which case, I can't access the child class related attributes :( I was trying to analyze the WebAPI pipeline, but it looks like controller factory etc. are all called per
request (i.e. IHttpControllerFactory.CreateController etc.). Short of adding static data via HttpRoute data tokens, I'm not sure where else I can place the data in order to avoid computation per request.
Guess I'm looking for the equivalent of something like the WCF behavior's ApplyDispatchBehavior method!
[Edit] For now, I've decided to stick with adding the data as part of the Http route's data token dictionary. Since the data is related to some custom attributes on the controller's class, its easy enough to compute this once when setting up the route.
Thanks,
Priya
Please "Mark as Answer" if this resolves your query. Thanks!
priya_marwah...
Member
81 Points
52 Posts
Best place to initialize static data for controller?
Mar 07, 2012 05:09 AM|LINK
I need to initialize some data that will be static for all instances of my particular controller. Once its initialized, I basically add this data as a custom property on the controller's descriptor by overriding the Initialize method. My question is....apart from doing the initialization in a static constructor of the controller, is there any other (or better) way of doing this? The nature of the data is such that it will not change until I actually change the class definition (basically, its related to a bunch of class and method level attributes).
Thanks,
Priya
marcind
Contributor
3344 Points
609 Posts
Microsoft
Re: Best place to initialize static data for controller?
Mar 07, 2012 05:57 AM|LINK
A static constructor sounds fine in this case.
ASP.NET Team
@marcind
Blog
priya_marwah...
Member
81 Points
52 Posts
Re: Best place to initialize static data for controller?
Mar 07, 2012 06:18 AM|LINK
Thanks....but it kinda gets complicated if this controller is a base controller...in which case, I can't access the child class related attributes :( I was trying to analyze the WebAPI pipeline, but it looks like controller factory etc. are all called per request (i.e. IHttpControllerFactory.CreateController etc.). Short of adding static data via HttpRoute data tokens, I'm not sure where else I can place the data in order to avoid computation per request.
Guess I'm looking for the equivalent of something like the WCF behavior's ApplyDispatchBehavior method!
[Edit] For now, I've decided to stick with adding the data as part of the Http route's data token dictionary. Since the data is related to some custom attributes on the controller's class, its easy enough to compute this once when setting up the route.
Thanks,
Priya