我有一個Nancy API並創建了一個自定義的從DefaultNancyBootstrapper
繼承的Bootstraper。使用TinyIoC解析具有構造函數參數的具體類型
我也有具體的類型ConcreteFoo
,我想在請求範圍內綁定到self並傳遞一個特定的構造函數參數。
public class ConcreteFoo {
private readonly int _baseInteger;
public ConcreteFoo(int baseInteger) {
_baseInteger = baseInteger;
}
}
我定製Bootstraper如下:
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}});
}
}
爲了和我已經使用在TinyIoC Wiki提供的示例所需的構造函數的參數來解決ConcreteFoo
。我注意到代碼從我的自定義引導程序中的覆蓋中傳遞,如果我寫下面的代碼,則response
爲真。
var response = container.CanResolve<ConcreteFoo>(new NamedParameterOverloads {{"baseInteger", 100}});
但是,我得到一個異常在該應用的Bootstrap。下面列出內部內部內部(內部很多)異常,並聲明它不能創建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)
我發現this相關的問題,但它指的是註冊一個接口,而不是一個具體類型。
我做錯了什麼,或者你是否熟悉另一種解決具體類型和提供構造函數參數的方法?
我已經試過了,失敗了。然後我切換到解決。然而,真正的問題來自這樣的事實:在SetApplicationContainer()中,我有一個Singleton(默認爲Interfaces),它試圖使用InRequestScope ConcreteFoo。我將Singleton更改爲MultiInstance,並像註冊表一樣工作。感謝您的幫助。 – Manolis