Ninject w/ ASMX web service in a MVC3/Ninject 3 environment
Asked Answered
T

1

4

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

Touslesmois answered 11/9, 2012 at 21:13 Comment(0)
P
4

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.

Pains answered 12/9, 2012 at 1:1 Comment(3)
Thanks, this works and will have to do for now. Unfortunately it ties the service directly to ASP.NET MVC. Wish there was a better way :(Touslesmois
@NateRickard Actually, DependencyResolver is a component that attempts to generalize the issue of dependency injection. Rather than each IoC framework implementing their own system, they are able to tie into the DependencyResolver system for interacting with other libraries.Pains
Yes, in that respect it's good and doesn't tie me to Ninject. The issue I have is that this service is/can be used in different environments - ASP.NET web forms, MVC, etc. So using DependencyResolver adds the dependency of MVC since it lives in that namespace.Touslesmois

© 2022 - 2024 — McMap. All rights reserved.