I have an object:
public class TestEvent : IEvent
{
private string _id;
public TestEvent()
{
}
public TestEvent(string eventId)
{
_id = eventId;
}
}
And I have a StructureMap registry:
public class TheRegistry : Registry
{
public TheRegistry()
{
Scan(_ =>
{
_.TheCallingAssembly();
_.AddAllTypesOf<IEvent>().NameBy(t => t.Name.ToUpper());
});
}
}
I'm trying to get a named instance of IEvent using the StructureMap container and passing in a constructor argument for "eventId":
var id = "TESTEVENT";
var args = new ExplicitArguments();
args.SetArg("eventId", id);
var eventInstance = _container.GetInstance<IEvent>(args, id);
I think the docs suggest it should work, however I'm getting an ArgumentNullException:
{"Trying to find an Instance of type MyProject.IEvent, MyProject, Version=1.0.0.0, Culture=neutral, publicKeyToken=null\r\nParameter name: instance"}
All the code works properly if I remove the second constructor, and just grab the named instance.
UPDATE:
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.
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<IEvent>(args, eventName);