2016-09-23 24 views
0

在global.asax中調用我的服務時出現編譯錯誤。我使用UnityMvc作爲我的DI。它在我的控制器中調用時工作,但在Global.asax中沒有。這是錯誤。Global.asax上的依賴注入編譯錯誤

編譯器錯誤消息:CS7036:沒有給定的參數對應於所要求的形式參數 'genreService' 'MvcApplication.MvcApplication(IGenreService)'

的Global.asax.cs

public class MvcApplication : System.Web.HttpApplication 
{ 
    private readonly IGenreService _genreService; 

    public MvcApplication(IGenreService genreService) 
    { 
     _genreService = genreService; 
    } 

    protected void Application_Start() 
    { 
     AreaRegistration.RegisterAllAreas(); 
     FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
     RouteConfig.RegisterRoutes(RouteTable.Routes); 
     BundleConfig.RegisterBundles(BundleTable.Bundles); 
    } 
} 

GenreService.cs

public partial class GenreService : IGenreService 
{ 
    private readonly IRepository<Genre> _genreRepository; 
    private readonly IRepository<GameGenre> _gameGenreRepository; 

    public GenreService(IRepository<Genre> genreRepository, IRepository<GameGenre> gameGenreRepository) 
    { 
     _genreRepository = genreRepository; 
     _gameGenreRepository = gameGenreRepository; 
    } 

    // methods 
} 

IGenreService.cs

public partial interface IGenreService 
{ 
    // interface 
} 

UnityConfig.cs

using System; 
using Microsoft.Practices.Unity; 
using Microsoft.Practices.Unity.Configuration; 
using System.Data.Entity; 
using Microsoft.AspNet.Identity; 
using Microsoft.Owin.Security; 
using GameCommerce.Infrastructure.Services.GameLibrary; 
using GameCommerce.Infrastructure; 
using Microsoft.AspNet.Identity.EntityFramework; 
using GameCommerce.Infrastructure.ApplicationUsers; 
using System.Web; 
using GameCommerce.Infrastructure.Services.Message; 

namespace GameCommerce.Web.App_Start 
{ 
/// <summary> 
/// Specifies the Unity configuration for the main container. 
/// </summary> 
public class UnityConfig 
{ 
    #region Unity Container 
    private static Lazy<IUnityContainer> container = new Lazy<IUnityContainer>(() => 
    { 
     var container = new UnityContainer(); 
     RegisterTypes(container); 
     return container; 
    }); 

    /// <summary> 
    /// Gets the configured Unity container. 
    /// </summary> 
    public static IUnityContainer GetConfiguredContainer() 
    { 
     return container.Value; 
    } 
    #endregion 

    /// <summary>Registers the type mappings with the Unity container.</summary> 
    /// <param name="container">The unity container to configure.</param> 
    /// <remarks>There is no need to register concrete types such as controllers or API controllers (unless you want to 
    /// change the defaults), as Unity allows resolving a concrete type even if it was not previously registered.</remarks> 
    public static void RegisterTypes(IUnityContainer container) 
    { 
     // NOTE: To load from web.config uncomment the line below. Make sure to add a Microsoft.Practices.Unity.Configuration to the using statements. 
     // container.LoadConfiguration(); 

     // TODO: Register your types here 
     // container.RegisterType<IProductRepository, ProductRepository>(); 

     container.RegisterType<DbContext, ApplicationDbContext>(new HierarchicalLifetimeManager()); 
     container.RegisterType<UserManager<ApplicationUser>>(new HierarchicalLifetimeManager()); 
     container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(new InjectionConstructor(new ApplicationDbContext())); 
     container.RegisterType<Areas.Admin.Controllers.AccountController>(new InjectionConstructor()); 
     container.RegisterType<Controllers.AccountController>(new InjectionConstructor()); 
     container.RegisterType<IAuthenticationManager>(new InjectionFactory(o => HttpContext.Current.GetOwinContext().Authentication)); 

     container.RegisterType(typeof(IRepository<>), typeof(Repository<>)); 
     container.RegisterType<IGameService, GameService>(); 
     container.RegisterType<IGenreService, GenreService>(); 
     container.RegisterType<IGameDeveloperService, GameDeveloperService>(); 
     container.RegisterType<IGamePublisherService, GamePublisherService>(); 
     container.RegisterType<IMessageService, MessageService>(); 
    } 
} 
} 

回答

0

嘗試用下面的代碼:在MVC

using Microsoft.Practices.Unity; 
public class MvcApplication : System.Web.HttpApplication 
    { 
     private readonly IGenreService _genreService; 

     public MvcApplication() 
     { 
      _genreService = GameCommerce.Web.App_Start.UnityConfig.GetConfiguredContainer().Resolve<IGenreService>(); 
     } 

     protected void Application_Start() 
     { 
      AreaRegistration.RegisterAllAreas(); 
      FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
      RouteConfig.RegisterRoutes(RouteTable.Routes); 
      BundleConfig.RegisterBundles(BundleTable.Bundles); 
     } 
    } 

DepedencyResolver默認用於分辨率滾筒,所以我們在這裏使用了'MvcApplication'中使用容器進行分辨的明確性。

+0

那麼在global.asax中實現它的最好方法是什麼? – markoverflow

+0

恰好在點統一容器被創建/註冊?我沒有看到你提供的代碼片段中的代碼。 – Boney

+0

我使用**公共靜態無效RegisterTypes(IUnityContainer容器)**註冊** container.RegisterType (); **裏面的** UnityConfig.cs ** – markoverflow