私は次のことを行いましたが、動作します。それが役に立てば幸い。
public class StructureMapDependencyResolver : IDependencyResolver
{
private readonly IContainer _container;
public StructureMapDependencyResolver(IContainer container)
{
_container = container;
}
public object GetService(Type serviceType)
{
if (serviceType.IsAbstract || serviceType.IsInterface)
{
return _container.TryGetInstance(serviceType);
}
return _container.GetInstance(serviceType);
}
public IEnumerable<object> GetServices(Type serviceType)
{
return _container.GetAllInstances<object>().Where(s => s.GetType() == serviceType);
}
}
Global.asax:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
var container = ConfigureDependencies();
GlobalConfiguration.Configuration.ServiceResolver.SetResolver(new StructureMapDependencyResolver(container));
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
public static IContainer ConfigureDependencies()
{
IContainer container = new Container();
Database.SetInitializer(new DataContextInitializer());
var dataContext = new DataContext.DataContext();
container.Configure(x => x.For<IRepository>().Use<Repository>().Ctor<DbContext>().Is(dataContext));
container.Configure(x=>x.For<IUnitOfWork>().Use<UnitOfWork>());
return container;
}