C# 使用TinyIoC解析具有构造函数参数的具体类型
我有一个Nancy API,并创建了一个自定义引导程序,它继承了C# 使用TinyIoC解析具有构造函数参数的具体类型,c#,nancy,tinyioc,C#,Nancy,Tinyioc,我有一个Nancy API,并创建了一个自定义引导程序,它继承了DefaultNancyBootTrapper 我还有一个具体类型ConcreteFoo,我想在请求范围内绑定到self,并传递一个特定的构造函数参数 public class ConcreteFoo { private readonly int _baseInteger; public ConcreteFoo(int baseInteger) { _baseInteger = baseInteger
DefaultNancyBootTrapper
我还有一个具体类型ConcreteFoo
,我想在请求范围内绑定到self,并传递一个特定的构造函数参数
public class ConcreteFoo {
private readonly int _baseInteger;
public ConcreteFoo(int baseInteger) {
_baseInteger = baseInteger;
}
}
我的自定义引导程序如下所示:
public class Bootstraper : DefaultNancyBootstrapper {
protected override void ConfigureApplicationContainer(TinyIoCContainer container) {
base.ConfigureApplicationContainer(container);
}
protected override void ConfigureRequestContainer(TinyIoCContainer container, NancyContext context) {
base.ConfigureRequestContainer(container, context);
container.Resolve<ConcreteFoo>(new NamedParameterOverloads {{"baseInteger", 100}});
}
}
但是,应用程序启动时出现异常。内在的。。。下面列出了(内部批次)异常,说明它无法创建ConcreteFoo
,并且无法解析System.Int32
InnerException: Nancy.TinyIoc.TinyIoCResolutionException
HResult=-2146233088
Message=Unable to resolve type: TestService.Api.ConcreteFoo
Source=Nancy
StackTrace:
at Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType, ConstructorInfo constructor, NamedParameterOverloads parameters, ResolveOptions options)
at Nancy.TinyIoc.TinyIoCContainer.MultiInstanceFactory.GetObject(Type requestedType, TinyIoCContainer container, NamedParameterOverloads parameters, ResolveOptions options)
InnerException: Nancy.TinyIoc.TinyIoCResolutionException
HResult=-2146233088
Message=Unable to resolve type: System.Int32
Source=Nancy
StackTrace:
at Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType, ConstructorInfo constructor, NamedParameterOverloads parameters, ResolveOptions options)
at Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType, NamedParameterOverloads parameters, ResolveOptions options)
at Nancy.TinyIoc.TinyIoCContainer.ResolveInternal(TypeRegistration registration, NamedParameterOverloads parameters, ResolveOptions options)
我找到了相关的问题,但它指的是注册一个接口,而不是一个具体的类型
我做错了什么,或者您熟悉解析具体类型并提供构造函数参数的另一种方法吗?我认为您需要容器。注册
无论如何,最近我更喜欢使用IRegistration类,它是由Nancy自动连接的。这样,事情就按主题/关注点分开了,引导程序根本就没有修改。我已经尝试过了,但失败了。然后我转向解决问题。然而,真正的问题来自这样一个事实:在SetApplicationContainer()中,我有一个Singleton(接口的默认设置),它试图使用InRequestScope ConcreteFoo。我将Singleton更改为MultiInstance,并使用寄存器进行了应有的工作。谢谢你的帮助。
InnerException: Nancy.TinyIoc.TinyIoCResolutionException
HResult=-2146233088
Message=Unable to resolve type: TestService.Api.ConcreteFoo
Source=Nancy
StackTrace:
at Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType, ConstructorInfo constructor, NamedParameterOverloads parameters, ResolveOptions options)
at Nancy.TinyIoc.TinyIoCContainer.MultiInstanceFactory.GetObject(Type requestedType, TinyIoCContainer container, NamedParameterOverloads parameters, ResolveOptions options)
InnerException: Nancy.TinyIoc.TinyIoCResolutionException
HResult=-2146233088
Message=Unable to resolve type: System.Int32
Source=Nancy
StackTrace:
at Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType, ConstructorInfo constructor, NamedParameterOverloads parameters, ResolveOptions options)
at Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType, NamedParameterOverloads parameters, ResolveOptions options)
at Nancy.TinyIoc.TinyIoCContainer.ResolveInternal(TypeRegistration registration, NamedParameterOverloads parameters, ResolveOptions options)