Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat exactly are the steps to auto inject Func<T>?
    text
    copied!<p>I'm trying to set up a service class where I would like to inject a simple <code>Func&lt;OtherService&gt;</code> factory method in its constructor. Type <code>OtherService</code> is a class, which should be self-bound (and is indeed when I inject it without factory).</p> <p>I've installed the latest Ninject core, and Ninject.Extensions.Factory NuGet packages. When I try to retrieve an instance from Ninject, I get an activation exception about not being able to activate IntPtr.</p> <p>What am I doing wrong? If I understand correctly the documentation of the Factory extension, this type of Func binding should be automatic, without any configuration. Do I need to register the Factory extension anywhere, or create an explicit binding for <code>OtherService</code>? Any tips would be appreciated.</p> <p>EDIT:</p> <p>I have Ninject and Ninject.Extensions.Factory installed in a small separate project, where I do some customizations on Ninject. These are the following:</p> <pre><code>Kernel.Components.Add&lt;IInjectionHeuristic, CustomInjectionHeuristic&gt;(); Kernel.Components.Add&lt;IActivationStrategy, TransientDisposableActivationStrategy&gt;(); </code></pre> <p>where the two custom handlers are for letting me use my custom [Service] attribute for indicating property injection, and for handling disposing of components which use my own IDisposableEx interface (with Disposed notification) for Ninject cache fine tuning.</p> <pre><code>public class CustomInjectionHeuristic : NinjectComponent, IInjectionHeuristic, INinjectComponent, IDisposable { public bool ShouldInject(MemberInfo member) { return member.IsDefined(typeof(ServiceAttribute), true); } } public class TransientDisposableActivationStrategy : ActivationStrategy { public override void Activate(IContext context, InstanceReference reference) { var scope = context.GetScope(); // care about only transient scoped objects if (scope != null) return; if (reference.Instance is Component) reference.Instance.As&lt;Component&gt;().Disposed += (sender, args) =&gt; context.Kernel.Components.Get&lt;ICache&gt;().Clear(sender); if (reference.Instance is IDisposableEx) reference.Instance.As&lt;IDisposableEx&gt;().Disposed += (sender, args) =&gt; context.Kernel.Components.Get&lt;ICache&gt;().Clear(sender); } } </code></pre> <p>And I also use the Ninject.Extensions.NamedScope extension for InCallScope() bindings.</p> <p>Any of these customizations could have effect on proper work of the factory?</p> <p>EDIT2:</p> <p>I think I got it. The problem seems to be that I have to reference the Factory extension in my startup project, where I define bindings, and not in the class library where I have my basic Ninject setup. So the key is that I have to reference any binding-related extension in that project where I define the bindings. Anyway, sounds logical... </p>
 

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