2011-12-13 43 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的原因。在我做完這件事後,所有事情都開始順利運作