1

I am trying to make a simple REST Service with .NET 4.0 that uses ninject to inject any dependencies for the service.

This is what my service looks like currently:

[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class ReportService
{
    private readonly ReportManager _reportManager;

    public ReportService(ReportManager reportManager)
    {
        _reportManager = reportManager;
    }

    [WebInvoke(UriTemplate = "GetReports", Method = "GET")]
    public List<ReportDTO> GetReports()
    {
        var reports = _reportManager.GetReports();
        return reports.ToList();
    }
}

This is my global.asax:

public class Global : NinjectHttpApplication
{
    void Application_Start(object sender, EventArgs e)
    {
        RegisterRoutes();
    }

    private void RegisterRoutes()
    {
        RouteTable.Routes.Add(new ServiceRoute("ReportService", new NinjectWebServiceHostFactory(), typeof(ReportService)));
    }

    protected override IKernel CreateKernel()
    {
        return new StandardKernel(new ServiceModule());
    }
}

And my ServiceModule:

public class ServiceModule : NinjectModule 
{
    public override void Load()
    {
        Bind<IFRSDbContext>().To<FRSDbContext>().InRequestScope();
        Bind<IDatabaseFactory>().To<DatabaseFactory>().InRequestScope();
        Bind<IUnitOfWork>().To<UnitOfWork>().InRequestScope();
        Bind<IReportRepository>().To<ReportRepository>();
        Bind<ReportManager>().ToSelf();
        Bind<ReportService>().ToSelf();
    }
}

I keep getting the following exception when I try to run my service:

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
   System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +222
   System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +194
   System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +339
   System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +253

[HttpException (0x80004005): Object reference not set to an instance of an object.]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9079228
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +97
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +256

EDIT

I've updated to the most current version of my Service.. I am using Ninject 2.3 and Ninject.Extensions.Wcf 2.3 from here but am still having no luck.. what am I doing wrong? I've followed everything from the WcfTimeService example except that I am using a REST service and not a .svc Wcf service...

shuniar
  • 2,472
  • 5
  • 28
  • 38
  • I am a bit confused about what version you are using since you have a `KernelContainer` and `NinjectWebServiceHostFactory`. The latest stable hadn't a `NinjectWebServiceHostFactory` and the current beta has no `KernelContainer`. Can you please clarify on this? – Remo Gloor Dec 13 '11 at 16:45
  • I am using version 2.2 which is the most recent release version available on github. I am currently trying to pull everything for the 2.3 version, but this has been a process. – shuniar Dec 13 '11 at 16:53
  • So you are saying you are using 2.2 and some files from 2.3? This seems quite a mess to me and makes it nearly impossible to help you. – Remo Gloor Dec 13 '11 at 17:09
  • No I was using 2.2 but I've heard a lot of people have the same problem so I am trying to replace with 2.3 I am not mixing anything – shuniar Dec 13 '11 at 17:39
  • 2.2 does not have NinjectWebServiceHostFactory. Where is this comming from? – Remo Gloor Dec 13 '11 at 17:41
  • I've cleaned up my example everything is the latest build of Ninject 2.3 now and I am getting a different exception for a `NullReferenceException`. – shuniar Dec 13 '11 at 19:19
  • possible duplicate of [What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net) – John Saunders Dec 13 '11 at 19:32
  • @JohnSaunders not a duplicate since `NullReferenceException` was caused by third party library and not my own code. – shuniar Dec 13 '11 at 21:14
  • @RemoGloor I was able to figure out the issue, I wasn't calling the Ninject base classes `Application_Start()` after I registered my routes. See my answer below. I thought you may want to add this to documentation for future reference. – shuniar Dec 13 '11 at 21:15
  • True. I didn't notice the stack trace. – John Saunders Dec 13 '11 at 21:18
  • The latest Ninject source now has an example of using Ninject with the WCF REST Template. I also posted an example of how to do this with 2.2 here: https://github.com/chafey/Ninject-2.2-Wcf-Rest-Example – Chris Hafey Dec 21 '11 at 02:22

2 Answers2

2

You shouldn't call Application_Start. Instead override OnApplicationStarted

Remo Gloor
  • 32,609
  • 4
  • 66
  • 97
0

After downloading the sources of Ninject.Web.Common and Ninject.Extensions.Wcf I was able to track down the issue.

In order to get this to work without a *.svc file you need to call the Application_Start() method of the NinjectHttpApplication at the end of the Application_Start() in Global.asax.cs file, like so:

public class Global : NinjectHttpApplication
{
    void Application_Start(object sender, EventArgs e)
    {
        RegisterRoutes();
        Application_Start(); // added call
    }

    private void RegisterRoutes()
    {
        RouteTable.Routes.Add(new ServiceRoute("ReportService", new NinjectWebServiceHostFactory(), typeof(ReportService)));
    }

    protected override IKernel CreateKernel()
    {
        return new StandardKernel(new ServiceModule());
    }
}

Without doing this, the Kernel never gets created and therefore that is what causes the NullReferenceException. After I did this everything started working perfectly.

shuniar
  • 2,472
  • 5
  • 28
  • 38