1

this is my first time posting up here and trust me I have searched high and low for an answer to my question but have had very little success.

Background: I have currently started trying to re-factor our existing SOAP web service (.Net 3.5) in order to do some IOC and DI using Ninject. I have a CacheManager which I am trying to initalize in the web method, however the injection does not seem to kick in.

I have an console application that calls the webservice with the below:

static void Main(string[] args)
    {
        TestService service = new CachingService.TestService();

        DataResult result = service.GetSomething(1);
    }

The webservice is below: TestService.asmx.cs

[WebMethod(Description = "Get something")]
public DataResult GetSomething(int param)
{
    try
    {
        return this.CacheManager.Get();
    }
    catch (Exception ex)
    {
        throw;
    }
}

Base.cs (TestService.asmx.cs inherits Base to initialize CacheManager)

public class Base
{
    [Inject]
    public ICacheManager CacheManager
    {
        get
        {
            if (cacheProxy == null)
            {
                cacheProxy = new CacheProxy();
            }
            return cacheProxy.CacheManager;
        }
    }  
}

CacheProxy.cs

public class CacheProxy
{
    [Inject]
    public ICacheManager CacheManager { get; set; }
}

CacheManager.cs

public class CacheManager : ICacheManager
{
   //implements iCacheManager methods
}

App_Start/NinjectWebCommon.cs

private static void RegisterServices(IKernel kernel)
    {
kernel.Bind<ICacheManager>()
            .ToMethod(x => x.Kernel.Get<ICacheManagerFactoryBuilder>().GetCacheManagerFactory().CreateCacheManager())
            .InRequestScope();
}

CacheManagerFactoryBuilder.cs

public class CacheManagerFactoryBuilder : ICacheManagerFactoryBuilder
{
    private ICacheManagerFactory _Factory;

    public CacheManagerFactoryBuilder(ICacheManagerFactory factory)
    {
        _Factory = factory;
    }

    public ICacheManagerFactory GetCacheManagerFactory()
    {
        return _Factory;
    }
}

CacheManagerFactory.cs

public class CacheManagerFactory : ICacheManagerFactory
{
    private readonly ICacheManager Manager;

    public CacheManagerFactory(ICacheManager manager)
    {
        if (this.Manager == null)
        {
            this.Manager = manager;
        }
    }

    public ICacheManager CreateCacheManager()
    {
        return this.Manager;
    }

}  

Everytime I run the console application and it hits GetSomething, CacheManager is null. Why is it that the injection is not happening when I do a call to the web method?

Nanz
  • 179
  • 2
  • 14
  • I'm currently not seeing the initialization of your CacheManager variable which you call in the line "return this.CacheManager.Get();" It should be initialized in the constructor if you are doing constructor injection. – Kristof Nov 07 '12 at 12:57
  • Ahh sorry, I left out the Base class which initalizes the CacheManager. I have edited the post now. – Nanz Nov 07 '12 at 15:01
  • The only thing that can cause something to get Injected is a `Kernel.Get` or a `Kernel.Inject`. The `[Inject]` bits only tell it where to Inject when one of those top level methods triggers it. Buy http://manning.com/seemann now. Specific to WCF stuff, are you setting up the NinjectControllerFactory linkage in the `.svc.cs`? Have you looked at the Ninject WCF sample and attempted to pull your code in there? – Ruben Bartelink Nov 07 '12 at 20:40
  • Sorry, your [Factory Factory](http://memegenerator.net/instance/20247106) made me assume WCF. No idea if/where to find a good ASMX based sample but same principle applies - you need to derive from something etc. to get stuff hokked in for Ninject to be called to do the injection - the attribute by itself just sits there doing nothing. – Ruben Bartelink Nov 07 '12 at 20:43

1 Answers1

0

Another member of the team eventually stumbled across this in another thread here:

How can I implement Ninject or DI on asp.net Web Forms?

All I was missing was inheriting WebServiceBase on my TestService web service class!

Community
  • 1
  • 1
Nanz
  • 179
  • 2
  • 14