0

I'm trying to inject my dependencies to a legacy ASP.NET .asmx web service, but I find them null. Here's my code:

Ninject binding module:

public class NinjectBindings : Ninject.Modules.NinjectModule
{
    public override void Load()
    {
        Bind<IClientContextFactory>().To<ClientContextFactory>();
        Bind<IMapper>().ToMethod(AutoMapper).InSingletonScope();
    }

    private IMapper AutoMapper(Ninject.Activation.IContext context)
    {
        Mapper.Initialize(config =>
        {
            config.ConstructServicesUsing(type => context.Kernel.Get(type));

            // Users
            config.CreateMap<User, UserDto>();

            // Usergrid 
            config.CreateMap<UserDto, UserGridDetails>();

            // Supervisor
            config.CreateMap<User, SupervisorDto>();

            // Branch
            config.CreateMap<Branch, BranchDto>();

        });

        Mapper.AssertConfigurationIsValid(); // optional
        return Mapper.Instance;
    }
}

Then I create a kernel instance in tGlobal.asax:

    protected override IKernel CreateKernel()
    {
        IKernel kernel = new StandardKernel(new NinjectBindings());
        return kernel;
    }

In my web service (.asmx):

[Inject]
    public IClientContextFactory contextFactory { get; set; }

    [Inject]
    public IMapper mapper { get; set; }

    public GridService()
    {
        contextFactory = DependencyResolver.Current.GetService<IClientContextFactory>();
        mapper = DependencyResolver.Current.GetService<IMapper>();
    }

Later on, I try to to use my dependencies as follows:

  List<UserGridDetails> userList = new User(contextFactory, mapper).GetUsers();

Here, I find both contextFactory and mapper instances are null. I debugged the application and made sure all of the above code get executed. What am I doing wrong?

devC
  • 1,384
  • 5
  • 32
  • 56

1 Answers1

0

Resolved it using the solution suggested here: Ninject inject a service interface in a web service

You need to extend your webservice class from WebServiceBase in Ninject.Web.WebServiceBase

devC
  • 1,384
  • 5
  • 32
  • 56