3

I have a web service in my project that I use to return json data to ajax calls.

Injecting my ClientService works fine on regular pages but how do I get it to work in the Web Service?

NinjectWebCommon.cs:

 private static void RegisterServices(IKernel kernel)
        {
            kernel.Bind<IClientService>().To<ClientService>();
        }  

Default.aspx.cs: works!

public partial class _Default : System.Web.UI.Page
    {
 [Inject]
        public IClientService clientService { get; set; } 

MyWebservice.asmx: NullReferenceException (clientService is null)

 public class MyWebService: System.Web.Services.WebService
    {     
        [Inject]
        public IClientService clientService { get; set; } 


 [WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public MyObject GetClients(int id)
        {
            var list = clientService.GetClients(id);
woggles
  • 7,444
  • 12
  • 70
  • 130
  • Have you looked at any of the samples to see if there is an example of a WebService being Injected? – Ruben Bartelink May 06 '13 at 19:59
  • The only examples I can find are http://stackoverflow.com/questions/10765424/how-to-manually-instantiate-objects-using-ninject-for-mvc-3. Although it seems that the DependencyResolver class doesn't exist in the latest version of ninject? – woggles May 07 '13 at 06:06
  • I mean on github.com/ninject - I havent looked but I'd be surprised if there isnt an asmx somewhere. Note that the DependencyResolver stuff is in a different NuGet package (again, see an MVC sample) – Ruben Bartelink May 07 '13 at 08:23
  • Have you solved this. I really need this and cant get it to work... – Patrick May 14 '13 at 16:38
  • Nope I haven't - please post a solution if you manage to find one! – woggles May 16 '13 at 05:32
  • i run into same issue .. any luck with this? – Laurence Feb 04 '14 at 11:50
  • @LaurenceNyein haven't looked at this in ages, but don't think I had any luck or I would have posted the answer – woggles Feb 04 '14 at 12:05

2 Answers2

7

I have solved this problem. I changed MyWebService class to derive from Ninject.Web.WebServiceBase:

public class MyWebService: WebServiceBase
alan
  • 6,705
  • 9
  • 40
  • 70
SnowWhite
  • 190
  • 1
  • 3
  • 9
1

I run on the same problem. Took me a while to figure it out. My solution to this problem is adding statatic Ninject instantiation of Kernel in Global.asax :

  public class Global : NinjectHttpApplication 
    {
        private static Ninject.IKernel kernel = new Ninject.StandardKernel();

        protected override Ninject.IKernel CreateKernel()
        {

            //Business TI
            kernel.Bind<IYourService>().To<YourService>();

            return kernel;
        }
}

What enables to get it directly from singleton(public) or which solution I choosed, by accesing NinjectHttpApplication's context : //in webservice parameterless constructor

yourService = ((NinjectHttpApplication)HttpContext.Current.ApplicationInstance).Kernel.Get<IPublicationService>(); 

This is the solution for working with legacy asmx web service, putting aside

mike
  • 1,202
  • 1
  • 7
  • 20