以下が機能していないため、WithConstructorArgumentの私の理解はおそらく間違っています。
私にはサービスがあり、そのコンストラクターが複数のオブジェクトを取得しているMyServiceと、testEmailという文字列パラメーターを呼び出します。この文字列パラメーターに対して、次のNinjectバインディングを追加しました。
string testEmail = "test@example.com";
kernel.Bind<IMyService>().To<MyService>().WithConstructorArgument("testEmail", testEmail);
ただし、次のコード行を実行すると、例外が発生します。
var myService = kernel.Get<MyService>();
これが私が得る例外です:
文字列のアクティブ化中にエラーが発生しました一致するバインディングが利用できず、タイプは自己バインドできません。アクティベーションパス:
2)タイプMyServiceのコンストラクターのパラメーターtestEmailへの依存性文字列の挿入
1)MyServiceの要求提案:
1)文字列のバインディングが定義されていることを確認してください。
2)バインディングがモジュールで定義されている場合は、モジュールがカーネルにロードされていることを確認してください。
3)誤って複数のカーネルを作成していないことを確認してください。
4)コンストラクター引数を使用している場合は、パラメーター名がコンストラクターのパラメーター名と一致していることを確認してください。
5)モジュールの自動ロードを使用している場合は、検索パスとフィルターが正しいことを確認してください。
私はここで何が間違っているのですか?
更新:
MyServiceコンストラクターは次のとおりです。
[Ninject.Inject]
public MyService(IMyRepository myRepository, IMyEventService myEventService,
IUnitOfWork unitOfWork, ILoggingService log,
IEmailService emailService, IConfigurationManager config,
HttpContextBase httpContext, string testEmail)
{
this.myRepository = myRepository;
this.myEventService = myEventService;
this.unitOfWork = unitOfWork;
this.log = log;
this.emailService = emailService;
this.config = config;
this.httpContext = httpContext;
this.testEmail = testEmail;
}
すべてのコンストラクターパラメータータイプに標準のバインディングがあります。'string'のみにバインディングがなく、HttpContextBaseには少し異なるバインディングがあります。
kernel.Bind<HttpContextBase>().ToMethod(context => new HttpContextWrapper(new HttpContext(new MyHttpRequest("", "", "", null, new StringWriter()))));
MyHttpRequestは次のように定義されています。
public class MyHttpRequest : SimpleWorkerRequest
{
public string UserHostAddress;
public string RawUrl;
public MyHttpRequest(string appVirtualDir, string appPhysicalDir, string page, string query, TextWriter output)
: base(appVirtualDir, appPhysicalDir, page, query, output)
{
this.UserHostAddress = "127.0.0.1";
this.RawUrl = null;
}
}