Ninject w/ ASMX web service in a MVC3/Ninject 3 environment

帅比萌擦擦* 提交于 2019-11-28 08:41:39

问题


I'm looking for the best way to incorporate a classic asmx web service into my MVC3 environment. I'd like the kernel/container to be shared between the two.

In the past with ASP.NET web forms I was able to use the following:

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

This used the older Ninject.Web to shared the kernel using the KernelContainer that was shared between WebServiceBase, PageBase, etc. etc.

Now I've got a MVC3 application using Ninject.Web.MVC and I need an older web service to function in the same space, using the same kernel/bindings. I haven't been able to find any information on getting a setup like this up and running.

Does anyone have any ideas, samples, or posts they can point me to (other than don't use asmx web services)?


回答1:


The MVC framework supports the DependencyResolver which integrates with Ninject for MVC. Although it is not possible to define a custom factory for ASMX web services, you could do the following:

public class Service : WebService
{
  public IContextProvider ContextProvider {get;set;}

  public Service()
  {
    ContextProvider = DependencyResolver.Current.GetService<IContextProvider>();
  }
}

While you cannot inject dependencies during construction of your service, you are able to resolve them yourself within the constructor.



来源:https://stackoverflow.com/questions/12378026/ninject-w-asmx-web-service-in-a-mvc3-ninject-3-environment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!