2016-11-22 26 views
1

我有一個像這樣的ModuleController。 (ASP Web API控制器)。 (它的正常工作)在asp.net mvc中添加BaseController給出錯誤

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

我想添加一個函數,它是通用於所有Controllers.So我加入這樣的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; 
     } 
    } 
} 

然後改變了我的ModuleController來繼承這個BaseController

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的可見性更改爲內部並從ModuleController中除去_loggerService屬性 – 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的相同實例,而不是重新聲明它。

1

BaseController 變化private readonly ILoggerService _loggerService;protected 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

Thanks @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() 
{ 
    // 
} 
相關問題