1

ninject を使用してサービスの依存関係を注入する .NET 4.0 で単純な REST サービスを作成しようとしています。

これは私のサービスが現在どのように見えるかです:

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

サービスを実行しようとすると、次の例外が発生し続けます。

説明: 現在の Web 要求の実行中に未処理の例外が発生しました。エラーの詳細とコード内のどこでエラーが発生したかについては、スタック トレースを確認してください。

例外の詳細: System.NullReferenceException: オブジェクト参照がオブジェクトのインスタンスに設定されていません。

ソース エラー:

現在の Web 要求の実行中に未処理の例外が生成されました。例外の発生元と場所に関する情報は、以下の例外スタック トレースを使用して特定できます。

スタックトレース:

[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 を使用していますが、まだ運がありません..何が間違っていますか? .svc Wcf サービスではなく REST サービスを使用していることを除いて、WcfTimeService の例のすべてに従いました...

4

2 に答える 2

2

Application_Start を呼び出さないでください。代わりに OnApplicationStarted をオーバーライドします

于 2011-12-13T22:07:03.883 に答える
0

のソースをダウンロードした後Ninject.Web.CommonNinject.Extensions.Wcf問題を突き止めることができました。

これを *.svc ファイルなしで機能させるには、次のように、Global.asax.cs ファイルの最後で のApplication_Start()メソッドを呼び出す必要があります。NinjectHttpApplicationApplication_Start()

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. これを行った後、すべてが完全に機能し始めました。

于 2011-12-13T21:11:36.287 に答える