Note that there are some explanatory texts on larger screens.

plurals
  1. POError passing constructor args to StructureMap named instance
    primarykey
    data
    text
    <p>I have an object:</p> <pre><code>public class TestEvent : IEvent { private string _id; public TestEvent() { } public TestEvent(string eventId) { _id = eventId; } } </code></pre> <p>And I have a StructureMap registry:</p> <pre><code>public class TheRegistry : Registry { public TheRegistry() { Scan(_ =&gt; { _.TheCallingAssembly(); _.AddAllTypesOf&lt;IEvent&gt;().NameBy(t =&gt; t.Name.ToUpper()); }); } } </code></pre> <p>I'm trying to get a named instance of IEvent using the StructureMap container and passing in a constructor argument for "eventId":</p> <pre><code>var id = "TESTEVENT"; var args = new ExplicitArguments(); args.SetArg("eventId", id); var eventInstance = _container.GetInstance&lt;IEvent&gt;(args, id); </code></pre> <p>I <em>think</em> the docs suggest it should work, however I'm getting an ArgumentNullException: </p> <blockquote> <p>{"Trying to find an Instance of type MyProject.IEvent, MyProject, Version=1.0.0.0, Culture=neutral, publicKeyToken=null\r\nParameter name: instance"} </p> </blockquote> <p>All the code works properly if I remove the second constructor, and just grab the named instance.</p> <p><strong>UPDATE:</strong></p> <p>Following Kirk's investigation, I've been able to workaround this "issue" by creating a simple object to hold any required arguments. It works now.</p> <pre><code>public class EventArguments { public string EventId { get; set; } } ... var eventName = Context.Parameters.EventTypeName.ToString().ToUpper(); var args = new ExplicitArguments(); args.SetArg("args", new EventArguments { EventId = eventName }); var eventInstance = _container.GetInstance&lt;IEvent&gt;(args, eventName); </code></pre>
    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.
 

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