How to use autofac register with parameters - the Lambda Expression kind
<p><a href="http://docs.autofac.org/en/latest/resolve/parameters.html#parameters-with-lambda-expression-components" rel="nofollow">Autofac documentation</a></p>

<p>I read the documentation and figured to register a type with parameter I have to make the call this way:</p>

<pre><code>builder.Register((c, p) =&gt; new HealthController(c.Resolve&lt;IServiceGroupFactory&gt;(), p.Named&lt;int&gt;("currentServiceGroupId"))).As&lt;IHealthStateController&gt;();
</code></pre>

<p>from the example I thought this will register the component together with expectation that there's one int parameter named currentServiceGroupId</p>

<p>but it doesn't seem to work that way</p>

<p>I'm getting "Sequence contains no elements" as autofac is calling IEnumerable.First and trying to find that element in the list. I guess p is empty hence the error. </p>

<p>I'm not sure why it's trying to find it I thought that this syntax will create the sequence and place the parameter it in the IEnumerable collection</p>

<p>So what's the proper way to make this work?</p>

<p>and the finding part is later when resolution happens</p>

<p>stack trace:</p>

<pre><code>at System.Linq.Enumerable.First[TSource](IEnumerable`1 source)
at Autofac.ParameterExtensions.ConstantValue[TParameter,TValue](IEnumerable`1 parameters, Func`2 predicate)
at Autofac.ParameterExtensions.Named[T](IEnumerable`1 parameters, String name)
at NPP.ServiceLayer.ServiceHost.IoCCompositionRoot.&lt;&gt;c.&lt;RegisterExplicitly&gt;b__2_0(IComponentContext c, IEnumerable`1 p) in C:\tfs2012\NPP\Dev\NPP\NPP.ServiceLayer.ServiceHost\App_Start\IoCCompositionRoot.cs:line 116
at Autofac.Builder.RegistrationBuilder.&lt;&gt;c__DisplayClass1`1.&lt;ForDelegate&gt;b__0(IComponentContext c, IEnumerable`1 p)
at Autofac.Core.Activators.Delegate.DelegateActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters)
at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters)
at Autofac.Core.Resolving.InstanceLookup.Execute()
</code></pre>

<p>Autofac's definition for the "Named" extension</p>

<pre><code>public static class ParameterExtensions
{
    /// &lt;summary&gt;
    /// Retrieve a named parameter value from a &lt;see cref="T:Autofac.NamedParameter"/&gt; instance.
    /// 
    /// &lt;/summary&gt;
    /// &lt;typeparam name="T"&gt;The type to which the returned value will be cast.&lt;/typeparam&gt;&lt;param name="parameters"&gt;The available parameters to choose from.&lt;/param&gt;&lt;param name="name"&gt;The name of the parameter to select.&lt;/param&gt;
    /// &lt;returns&gt;
    /// The value of the selected parameter.
    /// &lt;/returns&gt;
    /// &lt;seealso cref="T:Autofac.NamedParameter"/&gt;
    public static T Named&lt;T&gt;(this IEnumerable&lt;Parameter&gt; parameters, string name)
    {
      if (parameters == null)
        throw new ArgumentNullException("parameters");
      Enforce.ArgumentNotNullOrEmpty(name, "name");
      return ParameterExtensions.ConstantValue&lt;NamedParameter, T&gt;(parameters, (Func&lt;NamedParameter, bool&gt;) (c =&gt; c.Name == name));
    }

private static TValue ConstantValue&lt;TParameter, TValue&gt;(IEnumerable&lt;Parameter&gt; parameters, Func&lt;TParameter, bool&gt; predicate) where TParameter : ConstantParameter
{
  if (parameters == null)
    throw new ArgumentNullException("parameters");
  if (predicate == null)
    throw new ArgumentNullException("predicate");
  return Enumerable.First&lt;TValue&gt;(Enumerable.Cast&lt;TValue&gt;((IEnumerable) Enumerable.Select&lt;TParameter, object&gt;(Enumerable.Where&lt;TParameter&gt;(Enumerable.OfType&lt;TParameter&gt;((IEnumerable) parameters), predicate), (Func&lt;TParameter, object&gt;) (p =&gt; p.Value))));
}
}
</code></pre>