如何将依赖项注入到global.asax.cs中

发布于 2024-12-09 14:45:15 字数 722 浏览 0 评论 0原文

如何将依赖项注入到 global.asax.cs(即 MvcApplication 类)中?

之前使用服务定位器(反)模式进行依赖注入,我尝试通过使用 IOC 容器(特别是 Unity.Mvc3,因为它附带了 IDependencyResolver 的实现)来遵循我最新的 MVC 应用程序中的最佳实践建议框)和构造函数注入。

到目前为止,除了一些障碍之外,一切似乎都很简单,其中一个是在 global.asax.cs 中(另一个是自定义属性,但已经有一个关于此的问题)。

MvcApplication 类中的 HttpApplication 事件处理程序,例如:

Application_Start()
Application_EndRequest(object sender, EventArgs e)
Application_AcquireRequestState(object sender, EventArgs e)

可能需要外部依赖项,例如对 ILogService 的依赖项。那么我如何注入它们而不诉诸服务定位器(反)模式,例如

private static ILogService LogService
{
    get
    {
        return DependencyResolver.Current.GetService<ILogService>();
    }
}

任何帮助/建议,非常感谢!

How do I inject dependencies into the global.asax.cs, i.e. the MvcApplication class?

Having previously used the Service Locator (anti-)pattern for dependency injection, I am trying to follow best practice advice in my latest MVC application by using an IOC container (specifically Unity.Mvc3 because it comes with an implementation of the IDependencyResolver out of the box) and constructor injection.

Everything seems quite straight forward so far except for a couple of snags, one of which is in the global.asax.cs (the other is for custom attributes but there's aleady a question on SO covering that).

The HttpApplication event handlers in the MvcApplication class such as:

Application_Start()
Application_EndRequest(object sender, EventArgs e)
Application_AcquireRequestState(object sender, EventArgs e)

may require external dependencies, e.g. a dependency on an ILogService. So how do I inject them without resorting to the service locator (anti-)pattern of e.g.

private static ILogService LogService
{
    get
    {
        return DependencyResolver.Current.GetService<ILogService>();
    }
}

Any help/advice greatly appreciated!

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

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

发布评论

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

评论(2

明媚殇 2024-12-16 14:45:15

global.asax.cs 中的类是您的 Composition Root,因此你不能(也不应该)从外部向其中注入任何东西。

但是,MvcApplication 类只有一个实例,因此如果您需要在其方法之一中使用服务,只需将其声明为成员字段 - 例如:

public class MvcApplication : System.Web.HttpApplication
{
    private readonly ILogService log;

    public MvcApplication()
    {
        this.log = new MyLogService();
    }

    protected void Application_Start()
    {
        // ...

        this.log.Log("Application started");
    }
}

The class in your global.asax.cs is your Composition Root, so you can't (and shouldn't) inject anything into it from the outside.

However, there's only one instance of the MvcApplication class, so if you need a service in one of its methods, you can just declare it as a member field - e.g:

public class MvcApplication : System.Web.HttpApplication
{
    private readonly ILogService log;

    public MvcApplication()
    {
        this.log = new MyLogService();
    }

    protected void Application_Start()
    {
        // ...

        this.log.Log("Application started");
    }
}
摘星┃星的人 2024-12-16 14:45:15

您必须使用 AutofacConfig.Resolve() 而不是使用 DependencyResolver.Current.GetService() 才能正确获取服务。

You must use AutofacConfig.Resolve<T>() instead using DependencyResolver.Current.GetService<T>() to get your services without errors.

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