1

Many libraries nowadays auto-register/start using the PreApplicationStartMethodAttribute approach.

I don't like it. I want to control when the init code is run, for a variety of reasons. In many cases, I'd like to call init code myself from Application_Start.

Is there some way for me to intercept these calls and stop them?

h bob
  • 3,610
  • 3
  • 35
  • 51
  • Please let's not argue about whether it's a good idea or not. To each his own. We don't like the autostart behavior. – h bob May 16 '15 at 18:51
  • Any init code i've seen comes from a code template that's dumped in my project `App_Start`. I wasn't aware there are libraries that internally use it... – Brad Christie May 16 '15 at 18:53
  • @BradChristie If you use a library that magically does stuff without you asking for it, it's probably because it's using this prestart approach. Maybe it works for you, maybe it doesn't. We find it hard to integrate into our init code, and hard to test various scenarios because we essentially don't have control. – h bob May 16 '15 at 18:59
  • Can't say I have (that I recall). Not saying it isn't possible, just most packages I'm aware of use WebActivatorEx over direct usage. And if they do, they're usually up-front and provide a `.cs.pp` file so there's visibility. If there are libraries (on NuGet) that do this, I'd file a ticket with them to refactor. – Brad Christie May 16 '15 at 19:02

1 Answers1

1

I doubt you could stop all uses of this mechanism, because it would go counter to one of the fundamental purposes of the mechanism: to allow libraries and plugins to hook into the initialization phase and run their own initialization code, without the need for placing initialization code manually--to enable those libraries autonomous control of their initialization.

Let's consider what would happen if it were possible to shut off this mechanism: it would break libraries that depend on it for their initialization to execute early in the startup, earlier than Application_Start. It would also break libraries that don't document or specify a way for users to initialize the library manually.

I'm afraid the mechanism is meant to guarantee the opposite of what you wish to accomplish.

DWright
  • 9,258
  • 4
  • 36
  • 53
  • For some libraries, you are right. But for others, this mechanism is included unnecessarily, and is a form of code magic, and can lead to a sloppy design. There are a number of libraries I want to control myself, or, that I *need to* control myself. – h bob May 16 '15 at 19:53
  • Also you stated that the fundamental purpose is to allow the library to initialize itself... that's our problem with it. I am a user of that library. I want to access it's available behavior, when I want to. I don't want automatic behavior. That is a form of magic, like magic strings and magic numbers, and automagic config. Or put differently, I don't want some library to change the behavior of *my* system, unless/until I instruct it to do so. I have a fundamental problem with this mechanism, and I believe it has no place in a serious system. – h bob May 16 '15 at 19:54
  • I think you are overstating the "magicness" of it, as providing initialization mechanisms for "plugins" is a pretty valid goal, but I get your concern. You don't have the control you want to and that is aggravating. But in order for you to have the control you want, other things would be breakable, which is why I think the control you'd like is not available. And maybe there is a way to do what you want, I just would be surprised if there is--because it has the potential to break something as fundamental as initialization. – DWright May 16 '15 at 20:02
  • 2
    @hbob: take a look at http://stackoverflow.com/a/2483193/49251. Maybe this would work for you? You could try to load and find all PreApplicationStartMethodAttribute occurrences via refection and break them (perhaps) by point them at a bogus class or method. It sounds farfetched, but you could explore it. – DWright May 16 '15 at 20:20
  • doubt I'd do something so extreme, but gotta admit, it's a really cool idea! :) – h bob May 17 '15 at 05:57