问题描述
我WithConstructorArgument的认识可能是错误的,因为以下不工作:
我有一个服务,让我们叫它为MyService,它的构造正在多个对象,和一个字符串参数调用testemail子。对于此字符串参数,添加以下Ninject绑定:
字符串testemail子=test@example.com;
kernel.Bind&所述; IMyService方式>()到<为MyService>()WithConstructorArgument(testemail子,testemail子)。
但是,在执行下面的代码行的时候,我得到一个异常:
VAR为myService = kernel.Get<&为MyService GT;();
下面是例外,我得到:
我在做什么错在这里?
更新
下面是为MyService构造:
[Ninject.Inject]
公共则将MyService(IMyRepository myRepository,IMyEventService myEventService,
IUnitOfWork的UnitOfWork,ILoggingService日志,
IEmailService emailService,IConfigurationManager配置,
HttpContextBase的HttpContext,串testemail子)
{
this.myRepository = myRepository;
this.myEventService = myEventService;
this.unitOfWork =的UnitOfWork;
this.log =登录;
this.emailService = emailService;
this.config =配置;
this.httpContext = HttpContext的;
this.testEmail = testemail子;
}
我有标准绑定的所有构造函数参数类型。只有'串'有没有约束力,HttpContextBase具有约束力的就是有点不同:
kernel.Bind< HttpContextBase>( ).ToMethod(上下文=>新建HttpContextWrapper(新的HttpContext(新MyHttpRequest(,,,空,新的StringWriter()))));
和MyHttpRequest定义如下:
公共类MyHttpRequest:SimpleWorkerRequest的
{
公共字符串UserHostAddress;
公共字符串RawUrl;
公共MyHttpRequest(字符串appVirtualDir,串appPhysicalDir,字符串页面,查询字符串,TextWriter的输出)
:基地(appVirtualDir,appPhysicalDir,网页,查询,输出)
{
this.UserHostAddress =127.0.0.1;
this.RawUrl = NULL;
}
}
通过声明:
VAR为myService = kernel.Get<&为MyService GT;();
您正在尝试的决心为MyService
键,因为在为MyService
类型没有在内核中Ninject就会把它当作一个自我约束类型注册。
所以不会用你的 WithConstructorArgument
来解决testemail子
,因为它会与绑定<的前提下使用; IMyService方式>()
这就是为什么你得到异常
因此,如果您已经注册为MyService
与
字符串testemail子=test@example.com;
kernel.Bind&所述; IMyService方式>()到<为MyService>()
.WithConstructorArgument(testemail子,testemail子);
那么你应该解决它通过注册接口( IMyService
):
VAR为myService = kernel.Get< IMyService>();
My understanding of WithConstructorArgument is probably erroneous, because the following is not working:
I have a service, lets call it MyService, whose constructor is taking multiple objects, and a string parameter called testEmail. For this string parameter, I added the following Ninject binding:
string testEmail = "test@example.com";
kernel.Bind<IMyService>().To<MyService>().WithConstructorArgument("testEmail", testEmail);
However, when executing the following line of code, I get an exception:
var myService = kernel.Get<MyService>();
Here is the exception I get:
What am I doing wrong here?
UPDATE:
Here is the MyService constructor:
[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;
}
I have standard bindings for all the constructor parameter types. Only 'string' has no binding, and HttpContextBase has a binding that is a bit different:
kernel.Bind<HttpContextBase>().ToMethod(context => new HttpContextWrapper(new HttpContext(new MyHttpRequest("", "", "", null, new StringWriter()))));
and MyHttpRequest is defined as follows:
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;
}
}
With the statement:
var myService = kernel.Get<MyService>();
You are trying the resolve MyService
and because the MyService
type is not registered in your kernel Ninject will treat it as a self bound type.
So it won't use your WithConstructorArgument
to resolve the "testEmail"
because it will be only used with Bind<IMyService>()
that is why you're getting the exception.
So if you have registered your MyService
with:
string testEmail = "test@example.com";
kernel.Bind<IMyService>().To<MyService>()
.WithConstructorArgument("testEmail", testEmail);
Then you should resolve it through the registered interface (IMyService
):
var myService = kernel.Get<IMyService>();
这篇关于Ninject WithConstructorArgument:没有匹配的绑定是可用的,并且类型不能自行绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!