设置构造函数注入时发生System.InvalidOperationException

时间:2018-07-19 08:29:21

标签: c# asp.net-mvc dependency-injection ninject restsharp

我正在尝试使用Ninject注入构造函数参数:

    private void AddBindings()
    {
        //_kernel.Bind<IRestClient>().ToConstructor(x => new RestClient("http://localhost:49780/api/ProductApi"));
        _kernel.Bind<IRestClient>().ToConstructor(x => new RestClient(ConfigurationManager.AppSettings["ProductsApiUrl"]));
    }

但是我得到以下System.InvalidOperationException

  

由于对象的当前状态,该操作无效。

当我改用注释行时,它可以正常工作。 我已在调试模式下检查ConfigurationManager.AppSettings["ProductsApiUrl"]等于"http://localhost:49780/api/ProductApi"

为什么会引发此异常?

这里是StackTrace

   at System.Reflection.RuntimeMethodInfo.GetGenericMethodDefinition()
   at Ninject.Planning.Bindings.BindingBuilder.AddConstructorArgument(Expression argument, String argumentName, ParameterExpression constructorArgumentSyntaxParameterExpression)
   at Ninject.Planning.Bindings.BindingBuilder.AddConstructorArguments(NewExpression ctorExpression, ParameterExpression constructorArgumentSyntaxParameterExpression)
   at Ninject.Planning.Bindings.BindingBuilder.InternalToConstructor[TImplementation](Expression`1 newExpression)
   at Ninject.Planning.Bindings.BindingBuilder`1.ToConstructor[TImplementation](Expression`1 newExpression)
   at ShoppingCart.Infrastructure.NinjectDependencyResolver.AddBindings() in C:\Users\user1\Documents\ShoppingCart\ShoppingCart\Infrastructure\NinjectDependencyResolver.cs:line 35
   at ShoppingCart.Infrastructure.NinjectDependencyResolver..ctor(IKernel kernelParam) in C:\Users\user1\Documents\ShoppingCart\ShoppingCart\Infrastructure\NinjectDependencyResolver.cs:line 20
   at ShoppingCart.App_Start.NinjectWebCommon.RegisterServices(IKernel kernel) in C:\Users\user1\Documents\ShoppingCart\ShoppingCart\App_Start\NinjectWebCommon.cs:line 56
   at ShoppingCart.App_Start.NinjectWebCommon.CreateKernel() in C:\Users\user1\Documents\ShoppingCart\ShoppingCart\App_Start\NinjectWebCommon.cs:line 46
   at Ninject.Web.Common.Bootstrapper.Initialize(Func`1 createKernelCallback)
   at ShoppingCart.App_Start.NinjectWebCommon.Start() in C:\Users\user1\Documents\ShoppingCart\ShoppingCart\App_Start\NinjectWebCommon.cs:line 25

编辑:

我只是意识到这样做可以很好地工作:

    string productsApiUrl = ConfigurationManager.AppSettings["ProductsApiUrl"];
    _kernel.Bind<IRestClient>().ToConstructor(x => new RestClient(productsApiUrl));

为什么评估ConfigurationManager.AppSettings["ProductsApiUrl"]的重要性?

我在GitHub上查看了AddConstructorArgument()。 它具有以下if语句:

if (!(argument is MethodCallExpression methodCall) ||
                !methodCall.Method.IsGenericMethod ||
                methodCall.Method.GetGenericMethodDefinition().DeclaringType != typeof(IConstructorArgumentSyntax))

它正在调用GetGenericMethodDefinition()的事实意味着!methodCall.Method.IsGenericMethodfalse,即。 IsGenericMethod返回true

但是,根据MSDNInvalidOperationException仅在IsGenericMethod返回false时抛出。

所以我对如何抛出InvalidOperationException感到困惑。

1 个答案:

答案 0 :(得分:0)

很明显,旧版本存在错误。使用Resharper进行反编译时,可以看到没有IsGenericMethod检查,只是:

  if (methodCallExpression != null && !(methodCallExpression.Method.GetGenericMethodDefinition().DeclaringType != typeof (IConstructorArgumentSyntax)))
    return;

然后GetGenericMethodDefinition()将失败,因为您的表达式-ConfigurationManager.AppSettings["ProductsApiUrl"]不是通用的。