私は立ち往生しています。WCF Web API Preview 5 で動作する wcf web api p6 Ninjectについてここで概説した方法を使用していましたが、ベータ版の mvc 実装では状況がかなり異なります。ここに良い記事がありますhttp://www.asp.net/web-api/overview/extensibility/using-the-web-api-dependency-resolver独自のカスタム依存関係リゾルバーの構築について説明していますが、私はしたいと思います私のmvcビューコントローラーに使用しているのと同じ実装を使用します...たとえば、Ninject. この記事の IoC Unity の例に基づいていくつか試してみましたが、まだうまくいきません。私を正しい方向に向ける助けがあれば、大歓迎です。自分もどんどん掘っていきます。前もって感謝します!
ここが私の居場所です。私は WebActivator を使用してコードをブートストラップしていましたが、それを Application_Start() にドロップして、方程式からもう 1 つ取り出しました。
protected void Application_Start()
{
var kernel = new StandardKernel(new MyNinjectModule());
GlobalConfiguration.Configuration.ServiceResolver.SetResolver(new NinjectDependencyResolver(kernel));
}
次のエラーが表示されます:
タイプ Ninject.Web.Mvc.NinjectDependencyResolver は、Microsoft.Practices.ServiceLocation.IServiceLocator を実装していないようです。
パラメータ名: commonServiceLocator
解決策を見つけた
おそらくもっとエレガントな方法がある/あるでしょうが、これは今私のために働いています。ここにもカスタム メッセージ ハンドラを追加しています。
[assembly: WebActivator.PreApplicationStartMethod(typeof(MyApp.AppStart.ApiBootstrapper), "Start")]
namespace MyApp.AppStart
{
public class ApiBootstrapper
{
public static void Start()
{
var kernel = new StandardKernel(new MyNinjectModule());
var resolver = new NinjectDependencyResolver(kernel);
GlobalConfiguration.Configuration.ServiceResolver.SetResolver(resolver.GetService, resolver.GetServices);
GlobalConfiguration.Configuration.MessageHandlers.Add(new ApiAuthHandler());
}
}
}