我是Dependency Injection的新手,也是使用推荐的ninjectKernel作为DI框架的书,这里引用了book。我在以下方法中遇到错误:
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
return controllerType == null
? null
: (IController)ninjectKernel.Get(controllerType);
}
它抛出
无法将“System.String”类型的对象强制转换为“SportsStore.Domain.Concrete.EmailSettings”。
这个工作完全正常,直到它确实添加了绑定(下面)。
private void AddBindings() {
ninjectKernel.Bind<IProductsRepository>().To<EFProductRepository>();
EmailSettings emailSettngs = new EmailSettings {
WriteAsFile = bool.Parse(ConfigurationManager
.AppSettings["Email.WriteAsFile"] ?? "false")
};
ninjectKernel.Bind<IOrderProcessor>()
.To<EmailOrderProcessor>()
.WithConstructorArgument("settings", "emailSettings");
}
你能在这里给我一些提示吗?
这是堆栈跟踪:
[InvalidCastException:无法将“System.String”类型的对象强制转换为“SportsStore.Domain.Concrete.EmailSettings”。] DynamicInjectorc4f1546a04fc4966a278bd8198a4edff(Object [])+123 Ninject.Activation.Providers.StandardProvider.Create(IContext context)+884 Ninject.Activation.Context.ResolveInternal(对象范围)+218 Ninject.Activation.Context.Resolve()+277 Ninject。&lt;&gt; c__DisplayClass15.b__f(IBinding binding)+86 System.Linq.WhereSelectEnumerableIterator
2.MoveNext() +145 System.Linq.Enumerable.SingleOrDefault(IEnumerable
1 source)+4059897 Ninject.Planning.Targets.Target1.GetValue(Type service, IContext parent) +169 Ninject.Planning.Targets.Target
1.ResolveWithin(IContext parent)+747 Ninject.Activation.Providers.StandardProvider.GetValue(IContext context,ITarget target)+269 Ninject.Activation.Providers。&lt;&gt; c__DisplayClass4.b__2(ITarget target)+69 System.Linq.WhereSelectArrayIterator2.MoveNext() +66 System.Linq.Buffer
1..ctor(IEnumerable1 source) +323 System.Linq.Enumerable.ToArray(IEnumerable
1 source)+77 Ninject.Activation.Providers.StandardProvider.Create(IContext context)+847 Ninject.Activation.Context.ResolveInternal(对象范围)+218 Ninject.Activation.Context.Resolve()+277 Ninject。&lt;&gt; c__DisplayClass15.b__f(IBinding binding)+86 System.Linq.WhereSelectEnumerableIterator2.MoveNext() +145 System.Linq.Enumerable.Single(IEnumerable
1 source)+191 Ninject.ResolutionExtensions.Get(IResolutionRoot root,Type service,IParameter []参数)+70 在C:\ dev \ aspnet \ Workout \ SportsStore \ SportsStore.WebUI \ Infrastructure \ NinjectControllerFactory.cs中的SportsStore.WebUI.Infrastructure.NinjectControllerFactory.GetControllerInstance(RequestContext requestContext,Type controllerType):26 System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext,String controllerName)+74 System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext,IController&amp; controller,IControllerFactory&amp; factory)+193 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext,AsyncCallback回调,对象状态)+49 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext,AsyncCallback回调,对象状态)+49 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context,AsyncCallback cb,Object extraData)+16 System.Web.Mvc。&lt;&gt; c__DisplayClass7.b__6()+31 System.Web.Mvc.ServerExecuteHttpHandlerWrapper.Wrap(Func1 func) +27 System.Web.Mvc.ServerExecuteHttpHandlerAsyncWrapper.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +98 System.Web.HttpServerUtility.ExecuteInternal(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage, VirtualPath path, VirtualPath filePath, String physPath, Exception error, String queryStringOverride) +2019 System.Web.HttpServerUtility.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage) +76 System.Web.HttpServerUtility.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm) +28 System.Web.HttpServerUtilityWrapper.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm) +19 System.Web.Mvc.Html.ChildActionExtensions.ActionHelper(HtmlHelper htmlHelper, String actionName, String controllerName, RouteValueDictionary routeValues, TextWriter textWriter) +477 System.Web.Mvc.Html.ChildActionExtensions.RenderAction(HtmlHelper htmlHelper, String actionName, String controllerName) +34 ASP._Page_Views_Shared__Layout_cshtml.Execute() in c:\dev\aspnet\Workout\SportsStore\SportsStore.WebUI\Views\Shared\_Layout.cshtml:12 System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +197 System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +96 System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +88 System.Web.WebPages.<>c__DisplayClass7.<RenderPageCore>b__6(TextWriter writer) +233 System.Web.WebPages.HelperResult.WriteTo(TextWriter writer) +10 System.Web.WebPages.WebPageBase.Write(HelperResult result) +71 System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action
1 body)+64 System.Web.WebPages.WebPageBase.PopContext()+246 System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext,TextWriter writer,WebPageRenderingBase startPage)+95 System.Web.Mvc.RazorView.RenderView(ViewContext viewContext,TextWriter writer,Object instance)+259 System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext,TextWriter writer)+115 System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)+294 System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext,ActionResult actionResult)+13 System.Web.Mvc。&lt;&gt; c__DisplayClass1a.b__17()+23 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter过滤器,ResultExecutingContext preContext,Func1 continuation) +242 System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +21 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList
1过滤器,ActionResult actionResult)+175 System.Web.Mvc.Async。&lt;&gt; c__DisplayClass2a.b__20()+89 System.Web.Mvc.Async。&lt;&gt; c__DisplayClass25.b__22(IAsyncResult asyncResult)+102 System.Web.Mvc.Async.WrappedAsyncResult1.End() +57 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +43 System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +14 System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23 System.Web.Mvc.Async.WrappedAsyncResult
1.End()+62 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)+57 System.Web.Mvc.Async。&lt;&gt; c__DisplayClass4.b__3(IAsyncResult ar)+23 System.Web.Mvc.Async.WrappedAsyncResult1.End() +62 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +47 System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10 System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +25 System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23 System.Web.Mvc.Async.WrappedAsyncResult
1.End()+62 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)+47 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)+9 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+9690172 System.Web.HttpApplication.ExecuteStep(IExecutionStep step,Boolean&amp; completedSynchronously)+155
答案 0 :(得分:2)
您将字符串传递给构造函数参数而不是emailSettings
对象。取消字符串emailSettings
。
ninjectKernel.Bind<IOrderProcessor>()
.To<EmailOrderProcessor>()
.WithConstructorArgument("settings", emailSettings);