【问题标题】:Ninject w/ ASMX web service in a MVC3/Ninject 3 environment在 MVC3/Ninject 3 环境中使用 ASMX Web 服务的 Ninject
【发布时间】:2012-09-04 20:59:00
【问题描述】:

我正在寻找将经典 asmx Web 服务整合到我的 MVC3 环境中的最佳方式。我希望在两者之间共享内核/容器。

在过去使用 ASP.NET Web 表单时,我能够使用以下内容:

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class Service : WebServiceBase
{
    [Inject]
    public IContextProvider ContextProvider { get; set; }
...

这使用旧的 Ninject.Web 来共享内核,使用在 WebServiceBase、PageBase 等之间共享的 KernelContainer 等。

现在我有一个使用 Ninject.Web.MVC 的 MVC3 应用程序,我需要一个较旧的 Web 服务在相同的空间中运行,使用相同的内核/绑定。我找不到任何有关启动和运行此类设置的信息。

有没有人有任何想法、示例或帖子可以指向我(除了不使用 asmx 网络服务)?

【问题讨论】:

    标签: asp.net ninject asmx ninject.web.mvc ninject.web


    【解决方案1】:

    MVC 框架支持与 Ninject for MVC 集成的 DependencyResolver。虽然无法为 ASMX Web 服务定义自定义工厂,但您可以执行以下操作:

    public class Service : WebService
    {
      public IContextProvider ContextProvider {get;set;}
    
      public Service()
      {
        ContextProvider = DependencyResolver.Current.GetService<IContextProvider>();
      }
    }
    

    虽然您无法在构建服务期间注入依赖项,但您可以在构造函数中自行解决它们。

    【讨论】:

    • 谢谢,这行得通,现在必须这样做。不幸的是,它将服务直接与 ASP.NET MVC 联系在一起。希望有更好的方法:(
    • @NateRickard 实际上,DependencyResolver 是一个试图概括依赖注入问题的组件。不是每个 IoC 框架都实现自己的系统,而是能够绑定到 DependencyResolver 系统以与其他库进行交互。
    • 是的,在这方面它很好,不会把我和 Ninject 联系起来。我遇到的问题是该服务是/可以在不同的环境中使用 - ASP.NET Web 表单、MVC 等。因此使用 DependencyResolver 添加了 MVC 的依赖项,因为它位于该命名空间中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-26
    相关资源
    最近更新 更多