Castle Windsorコンテナを統合したASP.NET MVCアプリケーションがあります。私がApiControllerを追加してPOSTをしようとすると、エラーメッセージが表示されます。「コントローラーにパラメータのないpublicコンストラクターがあることを確認してください」アドバイスありがとう。WEB.APIとCastle Windsorコンテナを持つASP.NET MVC
城ウィンザーの統合:
public class CastleControllerFactory : DefaultControllerFactory
{
public IWindsorContainer Container { get; protected set; }
public CastleControllerFactory(IWindsorContainer container)
{
if (container == null)
{
throw new ArgumentException("container");
}
this.Container = container;
}
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
if (controllerType == null)
{
return null;
}
return Container.Resolve(controllerType) as IController;
}
public override void ReleaseController(IController controller)
{
var disposableController = controller as IDisposable;
if (disposableController != null)
{
disposableController.Dispose();
}
Container.Release(controller);
}
}
public class DefaultCastleInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.AddFacility<TypedFactoryFacility>();
//Functions
//container.Register(Component.For<IGetIPAddressesFromRange>().ImplementedBy<GetIPAddressesFromRange>().LifestylePerWebRequest());
container.Register(Component.For<IUnitOfWork>().ImplementedBy<UnitOfWork>().LifestylePerWebRequest());
var contollers = Assembly.GetExecutingAssembly().GetTypes().Where(x => x.BaseType == typeof(Controller)).ToList();
foreach (var controller in contollers)
{
container.Register(Component.For(controller).LifestyleTransient());
}
var apiContollers = Assembly.GetExecutingAssembly().GetTypes().Where(x => x.BaseType == typeof(ApiController)).ToList();
foreach (var apiController in apiContollers)
{
container.Register(Component.For(apiController).LifestyleTransient());
}
}
}
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
//Integrace CastleWindsor
var container = new WindsorContainer();
container.Install(new DefaultCastleInstaller());
var castleControllerFactory = new CastleControllerFactory(container);
ControllerBuilder.Current.SetControllerFactory(castleControllerFactory);
}
}
がApiController:
public class UserVerificationController : ApiController
{
private readonly IUnitOfWork _unitOfWork;
/// <summary>
/// Konstruktor
/// </summary>
/// <param name="unitOfWork"></param>
public UserVerificationController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
// GET api/<controller>
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
}