Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is a byte[] of 0 Length being injected into my class?
    primarykey
    data
    text
    <p>I'm using Ninject.Web.WebApi-RC in an MVC 4 Website.</p> <p>In my NinjectWebCommon.cs file I have:</p> <pre><code>kernel.Bind&lt;IA&gt;().To&lt;A&gt;(); kernel.Bind&lt;byte[]&gt;().ToConstant(new byte[1]).WhenTargetHas&lt;AKeyAttribute&gt;(); </code></pre> <p>In my WebApi controller I'm requesting an <code>IKernel</code> in the constructor (just trying to troubleshoot). I'm calling <code>Get&lt;IA&gt;()</code> in my controller constructor. The constructor for <code>A</code> looks like:</p> <pre><code>public A([AKey] byte[] key) </code></pre> <p>where <code>AKeyAttribute</code> is a class that extends <code>Attribute</code>. My issue is that when the code gets inside of the constructor for <code>A</code> its argument is a <code>byte</code> of length 0, not 1.</p> <p>Any ideas? Thanks!</p> <p><strong>EDIT</strong></p> <p>This is incredibly weird. I've changed the constructor of <code>A</code> to:</p> <pre><code>public A([AKey] string test) </code></pre> <p>And my NinjectWebCommon.cs file to:</p> <pre><code>kernel.Bind&lt;string&gt;().ToConstant("Test").WhenTargetHas&lt;AKeyAttribute&gt;(); </code></pre> <p>And then <code>"Test"</code> is indeed being injected into the controller. However when I do:</p> <pre><code>public A([AKey] string[] test) kernel.Bind&lt;string[]&gt;().ToConstant(new string[] { "Test" }).WhenTargetHas&lt;AKeyAttribute&gt;(); </code></pre> <p>That doesn't work.</p> <p>I'm guessing when you do an array of something in a class' constructor it's expecting multi-injection? Is there a way to override this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. CO`Byte` and `string` are ambiguous types. They usually contain configuration values (such as connection strings, file paths, etc) and by the registration itself you can't say what that value is. Prevent registering such type, or a collection of that type directly into the container. What information is in that `byte[]` anyway?
      singulars
    2. COI have a class (`B`) that takes in an instance of an `IA`. My implementation of `IA` does some cryptographic stuff and needs a a key that will vary from deployment to deployment of this web app. I'm using a hash of the connection string (a `byte[]` which will vary) as the key injected into my instance of `IA`. Anyway I don't think it was the primitives as much as the array. I think Ninject saw the `byte[]` and looked for `byte`'s that were registered to do multi-injection. I figured out something that did work and posted it as my answer.
      singulars
    3. COGreat that this works out, but what do you do when you got a second byte array that you need to inject somewhere? Prevent coming in this situation, by either registering the type A using a delegate (i.e. `Bind<IA>().ToMethod(c => new A(hash))`) or using a constructor argument override: `Bind<IA>().To<A>().WithConstructorArgument("hash", hash)`. And if you need to inject this hash into multiple components, it might be better to hide this hash behind an abstraction (such as `IHashingProvider`).
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload