Ahoj, mám ASP.NET MVC aplikaci s integrovaným Castle Windsor kontejnerem. Ten mi ale funguje pouze pro Controllery. Přidal jsem si do projektu i API Controller a nejsem si teď jistý, jak provést integraci i u něj. Při pokusu o POST na API dostávám následující chybovou hlášku "Make sure that the controller has a parameterless public constructor." Díky za případné rady. Integrace CastleWindsor v projektu:
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);
}
}
Api Controller:
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" };
}
}
|