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)?
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