2011-12-13 68 views
1

我正在尝试使用.NET 4.0创建一个简单的REST服务,该服务使用ninject为服务注入任何依赖关系。REST 4.0和Ninject.Extensions.Wcf 2.3 NullReferenceException

这是我的服务是什么样子目前:

[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(); 
    } 
} 

这是我的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()); 
    } 
} 

而且我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(); 
    } 
} 

我不断收到以下例外,当我尝试运行我的服务:

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.

源错误:

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.

堆栈跟踪:

[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 

编辑

我已经更新了我的服务的最新版本..我用Ninject 2.3和Ninject.Extensions.Wcf 2.3从here但我仍然没有运气..我做错了什么?我只是我使用REST服务,而不是一个.SVC WCF服务...

+0

我对你使用的版本有点困惑,因为你有一个'KernelContainer'和'NinjectWebServiceHostFactory'。最新的stable没有'NinjectWebServiceHostFactory',目前的beta没有'KernelContainer'。你能澄清一下吗? –

+0

我正在使用版本2.2,它是github上最新发布的版本。我目前正在试图为2.3版本提供所有内容,但这是一个过程。 – shuniar

+0

所以你说你使用2.2和2.3的一些文件?这对我来说似乎相当混乱,并且几乎不可能帮助你。 –

回答

2

您不应该调用Application_Start。相反,重写OnApplicationStarted

+0

好的,我将更新我的应用程序来执行此操作。谢谢。 – shuniar

0

下载的Ninject.Web.CommonNinject.Extensions.Wcf来源我能够追查问题后遵循一切从WcfTimeService例子。

为了得到这个没有你需要调用NinjectHttpApplicationApplication_Start()方法在的Global.asax.cs文件中Application_Start()结束,像这样一个* .svc文件的工作:

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()); 
    } 
} 

如果不这样做,内核永远不会被创建,因此这就是导致NullReferenceException的原因。在我做完这件事后,所有事情都开始顺利运作