2016-11-22 5 views
1

私はこのようなModuleControllerを持っています。 (ASP Web APIコントローラ)。asp.net mvcでBaseControllerを追加するとエラーが発生します

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Net.Http; 
using System.Reflection; 
using System.Web.Http; 
using AMMS.Logger; 
using AMMS.Logger.Interfaces; 

namespace AMMS.Api.Controllers 
{ 
    public class BaseController : ApiController 
    { 
     private readonly ILoggerService _loggerService; 

     public BaseController(ILoggerService loggerService) 
     { 
      _loggerService = loggerService; 
     } 

     protected virtual bool OnError(string actionName, MethodInfo methodInfo, Exception exception) 
     { 
      _loggerService.LogInfo($"Exception : {exception}, MethodInfo : {methodInfo}, actionName: {actionName}"); 
      return false; 
     } 
    } 
} 

次に、このBaseController

を継承するために私のModuleControllerを変更:私はこのようBaseControllerを追加したすべてのControllers.Soに共通する機能を追加したい

using System.Collections.Generic; 
using System.Web.Http; 
using AMMS.Logger.Interfaces; 
using AMMS.Service.Interfaces; 
using AMMS.Service.Models; 

namespace AMMS.Api.Controllers 
{ 
    public class ModuleController: ApiController 
    { 
     private readonly IModuleService _moduleService; 

     public ModuleController(IModuleService moduleService) 
     { 
      _moduleService = moduleService; 
     } 

     public List<AmmsModule> Get(string userId) 
     { 
      return _moduleService.GetModules(userId); 
     } 
    } 
} 

が(それは正常に動作しています)

using System.Collections.Generic; 
using System.Web.Http; 
using AMMS.Logger.Interfaces; 
using AMMS.Service.Interfaces; 
using AMMS.Service.Models; 

namespace AMMS.Api.Controllers 
{ 
    public class ModuleController: BaseController 
    { 
     private readonly IModuleService _moduleService; 
     private readonly ILoggerService _loggerService; 

     public ModuleController(IModuleService moduleService, ILoggerService loggerService) : base(loggerService) 
     { 
      _moduleService = moduleService; 
      _loggerService = loggerService; 
     } 

     public List<AmmsModule> Get(string userId) 
     { 
      return _moduleService.GetModules(userId); 
     } 
    } 
} 

ただし、BaseControllerを追加した後、モジュールを読み込むことができません。 デバッグするとき、ModuleControllerのコードが実行されていないようです。 !クライアント側で 私が取得 -

は、リソースの読み込みに失敗しました:サーバーは500 (内部サーバーエラー)の状態で応答し

をこの理由ことができるもの(ただBaseControllerを追加した後は、コードはまったく実行されません)。

+0

おそらくあなたは、このBaseControllerとModuleControllerの両方にthe_loggerServiceプロパティを宣言しているので?内部にBaseControllerでthe_loggerServiceの可視性を変更してみてください、あなたの派生クラスでは全く_loggerServiceに触れるべきではありませんModuleController – bolt19

+0

から_loggerServiceプロパティを削除し、代わりに、基本クラスのコンストラクタを呼び出します。 –

+0

ありがとう、それは働いた!内部に変更@JohnM –

答えて

0

あなたのブートストラップにモジュールを登録していますか?その後、

"Bootstrapper.cs"

public class Bootstrapper 
{ 
    public static IUnityContainer Initialize() 
    { 
     var container = BuildUnityContainer(); 
     DependencyResolver.SetResolver(new UnityDependencyResolver(container)); 
     return container; 
    } 

    private static IUnityContainer BuildUnityContainer() 
    { 
     var container = new UnityContainer(); 

     // register all your components with the container here 
     //This is the important line to edit 
     container.RegisterType<IModuleService, YourModuleService>(); 
     container.RegisterType<ILoggerService, YourLoggerService>(); 

     RegisterTypes(container); 
     return container; 
    } 

    public static void RegisterTypes(IUnityContainer container) 
    { 

    } 
} 

という名前のクラスを追加しようとすると、あなたのブートストラップを初期化:

public class MvcApplication : System.Web.HttpApplication 
{ 
    protected void Application_Start() 
    { 
     AreaRegistration.RegisterAllAreas(); 
     RouteConfig.RegisterRoutes(RouteTable.Routes); 

     //Initialize bootstrapper class 
     Bootstrapper.Initialize(); 
    } 
} 

あなたを助けて幸せ!

+0

それは:)。それをRegisterTypeに追加するのを忘れた –

1

親切代わりにそれを再宣言する、BaseControllerに_loggerServiceの同じインスタンスを使用します。 protected readonly ILoggerService _loggerService;

1

BaseController 変更private readonly ILoggerService _loggerService;(あなたが派生クラスでそれにアクセスする場合)

ModuleController:

using System.Collections.Generic; 
using System.Web.Http; 
using AMMS.Logger.Interfaces; 
using AMMS.Service.Interfaces; 
using AMMS.Service.Models; 

namespace AMMS.Api.Controllers 
{ 
    public class ModuleController: BaseController 
    { 
     private readonly IModuleService _moduleService; 

     public ModuleController(IModuleService moduleService, ILoggerService loggerService) : base(loggerService) 
     { 
      _moduleService = moduleService; 
     } 

     public List<AmmsModule> Get(string userId) 
     { 
      return _moduleService.GetModules(userId); 
     } 
    } 
} 
+1

'protected'は、派生したクラスにアクセスさせたい場合にはおそらく' internal'より優れています。 – Maarten

+0

ありがとう@Maarten私の答えを更新しました – bolt19

+0

動作しません:( –

0
using System.Collections.Generic; 
using System.Web.Http; 
using AMMS.Logger.Interfaces; 
using AMMS.Service.Interfaces; 
using AMMS.Service.Models; 

namespace AMMS.Api.Controllers 
{ 
    public class ModuleController: BaseController 
    { 
     private readonly IModuleService _moduleService; 

     public ModuleController(IModuleService moduleService) 
     { 
      _moduleService = moduleService; 
     } 

     public List<AmmsModule> Get(string userId) 
     { 
      return _moduleService.GetModules(userId); 
     } 
    } 
} 

そしてBaseControllerでは、これに変更されました:

protected BaseController() 
{ 
    // 
} 
関連する問題