この記事は、Castle Windsor で Web API を実行するための良い出発点ですが、単純な MVC コントローラーを作成するとどうなるでしょうか? 注入に依存関係がない場合にのみ機能します。
これを追加すると、つまり:
public class HomeController : Controller
{
private readonly IValuesRepository repository;
public HomeController(IValuesRepository repository)
{
this.repository = repository;
}
public ActionResult Index()
{
return View();
}
}
次のエラーが発生します。
このオブジェクトに定義されたパラメータなしのコンストラクタ
Castle Windsor を使用して、MVC と Web API ロジックを同じアプリケーションに含める方法はありますか?
に設定DependencyResolver.SetResolver(...)
した後application_start
、アプリケーションの改善に気づきませんでした。
ご覧のように。
サービス ロケーターの実装:
internal sealed class WindsorDependencyResolver
: ServiceLocatorImplBase, IDependencyResolver
{
private readonly IWindsorContainer container;
public WindsorDependencyResolver(
IWindsorContainer container)
{
if (container == null)
{
throw new ArgumentNullException("container");
}
this.container = container;
}
public object GetService(Type t)
{
return this.container.Kernel.HasComponent(t)
? this.container.Resolve(t) : null;
}
public IEnumerable<object> GetServices(Type t)
{
return this.container.ResolveAll(t)
.Cast<object>().ToArray();
}
public IDependencyScope BeginScope()
{
return new WindsorDependencyScope(this.container);
}
public void Dispose()
{
}
protected override object DoGetInstance(
Type serviceType, string key)
{
if (key != null)
return container.Resolve(key, serviceType);
return container.Resolve(serviceType);
}
protected override IEnumerable<object> DoGetAllInstances(
Type serviceType)
{
return (object[])container.ResolveAll(serviceType);
}
私を原点に戻してくれました。
やっと解決。
これが他の誰かのための解決策です
application_start で...
//mvc
DependencyResolver.SetResolver(
new WindsorMvcDependencyResolver(container));
// web api:
var httpDependencyResolver =
new WindsorHttpDependencyResolver(container);
GlobalConfiguration.Configuration.DependencyResolver =
httpDependencyResolver;
internal class WindsorMvcDependencyResolver
: WindsorDependencyScope, IDependencyResolver
{
private readonly IWindsorContainer container;
public WindsorMvcDependencyResolver(
IWindsorContainer container) : base(container)
{
this.container = container;
}
}
internal sealed class WindsorHttpDependencyResolver
: IDependencyResolver
{
private readonly IWindsorContainer container;
public WindsorHttpDependencyResolver(
IWindsorContainer container)
{
if (container == null)
{
throw new ArgumentNullException("container");
}
this.container = container;
}
public object GetService(Type t)
{
return this.container.Kernel.HasComponent(t)
? this.container.Resolve(t) : null;
}
public IEnumerable<object> GetServices(Type t)
{
return this.container.ResolveAll(t)
.Cast<object>().ToArray();
}
public IDependencyScope BeginScope()
{
return new WindsorDependencyScope(this.container);
}
public void Dispose()
{
}
}
internal class WindsorDependencyScope : IDependencyScope
{
private readonly IWindsorContainer container;
private readonly IDisposable scope;
public WindsorDependencyScope(
IWindsorContainer container)
{
if (container == null)
{
throw new ArgumentNullException("container");
}
this.container = container;
this.scope = container.BeginScope();
}
public object GetService(Type t)
{
return this.container.Kernel.HasComponent(t)
? this.container.Resolve(t) : null;
}
public IEnumerable<object> GetServices(Type t)
{
return this.container.ResolveAll(t)
.Cast<object>().ToArray();
}
public void Dispose()
{
this.scope.Dispose();
}
}
そして、mvc と Web API コントローラーの両方の登録
container.Register(Classes
.FromAssemblyContaining<HomeController>()
.BasedOn<Controller>()
.LifestylePerWebRequest());
container.Register(Classes
.FromAssemblyContaining<ValuesController>()
.BasedOn<IHttpController>()
.LifestyleScoped());