2014-02-17 37 views
1

我的網站是MVC和WebAPI的混合體。當我第一次加載網站時,似乎我的控制器正在構建,我的IOC容器已滿載。我收到以下錯誤,當我第一次調試網站:StructureMap ActivationException on site load

An exception of type 'Microsoft.Practices.ServiceLocation.ActivationException' occurred in Microsoft.Practices.ServiceLocation.dll but was not handled in user code 

Additional information: Activation error occured while trying to get instance of type PendingCoursesController, key "" 

我用StructureMap作爲我的容器,並從的NuGet包「structuremap」和「StructureMap.MVC4」扯了下來。引發錯誤的控制器是WebAPI控制器。如果我刷新頁面,則控制器構建正確,並且按預期在頁面上加載所有內容。這只是錯誤引發的初始加載,所以我的頁面缺少數據。

public static class IoC { 
    public static IContainer Initialize() { 
     ObjectFactory.Initialize(x => 
        { 
         x.Scan(scan => 
           { 
            scan.TheCallingAssembly(); 
            scan.WithDefaultConventions(); 
           }); 
        }); 
     return ObjectFactory.Container; 
    } 
} 

[assembly: WebActivator.PreApplicationStartMethod(typeof(App_Start.StructuremapMvc), "Start")] 
public static class StructuremapMvc { 
    public static void Start() { 
     IContainer container = IoC.Initialize(); 
     DependencyResolver.SetResolver(new StructureMapDependencyResolver(container)); 
     GlobalConfiguration.Configuration.DependencyResolver = new StructureMapDependencyResolver(container); 
    } 
} 

回答

1

添加了這個,它修復了它。從Dependency Injection Structuremap ASP.NET Identity MVC 5回答

using MVC.CFC.Domain; 
using MVC.CFC.Domain.Data; 
using MVC5.CFC.Web.Infrastructure; 
using MVC5.CFC.Web.Models; 
using StructureMap; 

namespace MVC5.CFC.Web.DependencyResolution 
{ 
    public static class IoC 
    { 
     public static IContainer Initialize() 
     { 
      ObjectFactory.Initialize(x => 
         { 

          x.Scan(scan => 
            { 
             scan.TheCallingAssembly(); 
             scan.WithDefaultConventions(); 
            }); 
          x.For<IUserDataSource>().HttpContextScoped().Use<IUserDb>(); 
          x.For<Microsoft.AspNet.Identity.IUserStore<ApplicationUser>>().Use<Microsoft.AspNet.Identity.EntityFramework.UserStore<ApplicationUser>>(); 
          x.For<System.Data.Entity.DbContext>().Use(() => new ApplicationDbContext()); 
         }); 



      return ObjectFactory.Container; 
     } 
    } 
}