title: C# Dependency Injection with StructureMap
body: <p>We've been using StructureMap for Dependency Injection in our ASP.Net MvC 3 web apps with C#, but today I'm unable to inject a needed type into a C# CustomResolver I've built.</p>
  
  <p>Here is the code in our StructureMapInitialization.cs file:</p>
  
  <pre><code>namespace UI.DependencyResolution
  {
  /// &lt;summary&gt;
  /// Class Structure Map IOC intialization
  /// &lt;/summary&gt;
  public static class StructureMapIocInitialization
  {
      /// &lt;summary&gt;
      /// Initialize Method for Structure Map IOC Initialization
      /// &lt;/summary&gt;
      /// &lt;returns&gt;an IContainer for Services and Repository&lt;/returns&gt;
      public static IContainer Initialize()
      {
          ObjectFactory.Initialize(cfg =&gt;
          {
              cfg.Scan(scan =&gt;
              {
                  scan.Assembly("Infrastructure");
                  scan.Assembly("Core");
                  scan.WithDefaultConventions();
                  scan.ConnectImplementationsToTypesClosing(typeof(IAlertGenerator&lt;&gt;));
                  scan.ConnectImplementationsToTypesClosing(typeof(IValidationRule&lt;&gt;));
              });
  
              cfg.For&lt;IFilterProvider&gt;().Use&lt;StructureMapFilterProvider&gt;();
              cfg.For&lt;IControllerActivator&gt;().Use&lt;StructureMapControllerActivator&gt;();
  
              cfg.SetAllProperties(c =&gt;
              {
                  c.WithAnyTypeFromNamespaceContainingType&lt;ICustomerRepository&gt;();
                  c.WithAnyTypeFromNamespaceContainingType&lt;IMachineRepository&gt;();
                  c.WithAnyTypeFromNamespaceContainingType&lt;ICurrentUserService&gt;();
                  c.WithAnyTypeFromNamespaceContainingType&lt;ICircuitRegistrationRepository&gt;();
                  c.WithAnyTypeFromNamespaceContainingType&lt;IWorkflowRequestInformationRepository&gt;();
                  c.WithAnyTypeFromNamespaceContainingType&lt;IEnumerable&lt;IAlertGenerator&lt;Customer&gt;&gt;&gt;();
  
              });
  
          });
  
          return ObjectFactory.Container;
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>And here is a snippet of the code from our AutoMapperProfile.cs file:</p>
  
  <pre><code>Mapper.CreateMap&lt;AbstractOrganization, DashboardDetails&gt;()
      .ForMember(dashboard =&gt; dashboard.AlertsDictionary,
      member =&gt; member.ResolveUsing&lt;OrganizationAlertResolver&gt;()
      .ConstructedBy(() =&gt; new OrganizationAlertResolver(
          ObjectFactory.GetInstance&lt;ICustomerRepository&gt;(), ObjectFactory.GetInstance&lt;IEnumerable&lt;IAlertGenerator&lt;Customer&gt;&gt;&gt;())));
  </code></pre>
  
  <p>And here is my CustomResolver code:</p>
  
  <pre><code>namespace UI.Models.CustomResolvers
  {
      public class OrganizationAlertResolver : ValueResolver&lt;Organization, Dictionary&lt;string,   string&gt;&gt;
      {
          private readonly ICustomerRepository _customerRepository;
  
          /// &lt;summary&gt;
          /// Storage for customer alert generator.
          /// &lt;/summary&gt;
          private readonly IEnumerable&lt;IAlertGenerator&lt;Customer&gt;&gt; _customerAlertGenerators;
  
  
          public OrganizationAlertResolver(ICustomerRepository customerRepository, 
              IEnumerable&lt;IAlertGenerator&lt;Customer&gt;&gt; customerAlertGenerators)
          {
              _customerRepository = customerRepository;
              _customerAlertGenerators = customerAlertGenerators;
          }
  
          protected override Dictionary&lt;string,string&gt; ResolveCore(Organization organization)
          {
              var customers = _customerRepository.GetActiveCustomersByOrgNumber(organization.OrgNumber);
              foreach (var generator in _customerAlertGenerators)
              {
                  generator.PopulateAlerts(customers);
              }
  
          Dictionary&lt;string, string&gt; alertsDictionary = new Dictionary&lt;string, string&gt;();
  
              foreach (var customer in customers)
              {
                  foreach (var alert in customer.Alerts)
                  {
                      if(alertsDictionary.ContainsKey(alert.ToString()))
                      {
                          alertsDictionary[alert.ToString()] = alert.Message;
                      }
                      else
                      {
                          alertsDictionary.Add(alert.ToString(), alert.Message);
                      }
                  }
              }
              return alertsDictionary;
          }
      }
  }
  </code></pre>
  
  <p>So, I'm trying to use StructureMap to inject the IEnumerable of IAlertGenerator of type Customer into my CustomResolver, but I get a StructureMap 202 Exception saying that there was no default instance of that type found, even though I created the instance of it in the last entry in the StructureMap initialization file. </p>
  
  <p>Here is a copy of the error message I'm getting when I run it:</p>
  
  <blockquote>
    <p>StructureMap Exception Code:  202 No Default Instance defined for
    PluginFamily
    System.Collections.Generic.IEnumerable<code>1[[Core.Domain.Alerts.IAlertGenerator</code>1[[Core.Domain.Model.Customer,
    Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], Core,
    Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], mscorlib,
    Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</p>
  </blockquote>
  
  <p>Can anyone help with suggestions on how to fix this problem?</p>