2014-02-17 40 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; 
     } 
    } 
}