定义 WebActivator.PreApplicationStartMethod 类的初始化顺序

发布于 2024-12-29 13:28:44 字数 355 浏览 1 评论 0原文

我有几个 WebActivator.PreApplicationStartMethod 装饰类。

一个用于 Ninject,另一个用于 AwesomeMVC,第三个用于后台任务调度程序。

问题是调度程序类需要利用 IoC 容器解决的依赖关系。

我的问题是:

  1. 我可以有多个 WebActivator.PreApplicationStartMethod 类吗?
  2. 我可以定义它们的初始化顺序,以便最重要的 IoC 排在第一位吗?
  3. WebActivator.PreApplicationStartMethod 静态类实例可以依赖 IoC 容器来解析其构造函数定义的依赖关系吗?

I have several WebActivator.PreApplicationStartMethod decorated classes.

One is for Ninject, another class for AwesomeMVC and a third one is for background task scheduler.

The problem is that the scheduler class needs to take advantage of the dependecies, that are resolved by IoC container.

My questions are:

  1. Can I have several WebActivator.PreApplicationStartMethod classes?
  2. Can I define order, in which they are initialized, so that IoC, being the most important, comes first?
  3. Can WebActivator.PreApplicationStartMethod static class instances rely on IoC container to resolve their constructor-defined dependencies?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

十六岁半 2025-01-05 13:28:44

是的,您可以拥有任意数量的类,这些类具有指向它们的 WebActivator.PreApplicationStartMethod 程序集属性。许多 NuGet 包使用此技术使它们能够引导到您的应用程序,而无需编辑 Global.asax。

您也可以定义顺序。您可以在 PreApplicationStartMethod 调用中传递命名参数 Order。 WebActivator 框架将确保按照指定的顺序调用方法。例如,要让您的 IoC 框架首先注册,请执行以下操作:

[assembly: WebActivator.PreApplicationStartMethod(typeof(MyApp.App_Start.NinjectWebCommon), "Start", Order=1]
[assembly: WebActivator.PreApplicationStartMethod(typeof(MyApp.App_Start.BGScheduler), "Start", Order=2]

因为 WebActivator 类是静态类,所以我不知道如何在其中使用构造函数注入。但是,您可以通过使用 System.Web.Mvc.DependencyResolver.SetResolver(IDependencyResolver resolver) 将 IoC 解析器注册为 Mvc 的默认服务定位器来使用服务定位器(反?)模式/代码>。

不过,我并不是特别想在这里讨论服务定位器模式的优点和缺点!

Yes, you can have as many classes as you want which have a WebActivator.PreApplicationStartMethod assembly attribute pointing to them. Many NuGet packages use this technique to enable them to bootstrap into your application without editing Global.asax.

You can define the order too. You can pass a named parameter, Order in the PreApplicationStartMethod call. The WebActivator framework will ensure that the methods are called in the order specified. For example, to make your IoC framework register first, do something like this:

[assembly: WebActivator.PreApplicationStartMethod(typeof(MyApp.App_Start.NinjectWebCommon), "Start", Order=1]
[assembly: WebActivator.PreApplicationStartMethod(typeof(MyApp.App_Start.BGScheduler), "Start", Order=2]

Because WebActivator classes are static classes, I don't see how you can use constructor injection in them. You can however, use the service locator (anti?)-pattern by registering your IoC resolver as Mvc's default service locator, using System.Web.Mvc.DependencyResolver.SetResolver(IDependencyResolver resolver).

I don't particularly want to go into the benefits and drawbacks of the service locator pattern here though!

思慕 2025-01-05 13:28:44

如果您知道 PreAppStart 方法 A 需要在 PreAppStart 方法 B 之后运行,那么实现这一点的唯一方法是在 A 的主体内显式添加对 B 的调用。

为了使该策略正常工作,您还应该确保您的 PreAppStart方法实现是幂等的,即它们可以安全地被多次调用。通常,这可以通过跟踪该方法是否已在静态布尔变量中调用并且如果该值为 true 则不执行任何操作来实现。

If you know that PreAppStart method A needs to run after PreAppStart method B, then the only way to achive that is to explicitly add a call to B inside the body of A.

For that strategy to work correctly you should also make sure that your PreAppStart method implementations are indempotent i.e. they can safely be called multiple times. Usually this can be achieved by keeping track of whether the method has already been called in a static boolean variable and not doing anything if that vale is true.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文