Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are two ways to pass parameters in Autofac:</p> <p><strong><a href="http://docs.autofac.org/en/latest/register/parameters.html" rel="nofollow noreferrer">When you are registering the component</a>:</strong></p> <p>When you register components, you have the ability to provide a set of parameters that can be used during the resolution of services based on that component. Autofac offers several different parameter matching strategies:</p> <ul> <li><code>NamedParameter</code> - match target parameters by name </li> <li><code>TypedParameter</code> - match target parameters by type (exact type match required)</li> <li><p><code>ResolvedParameter</code> - flexible parameter matching</p> <pre><code>// Using a NAMED parameter: builder.RegisterType&lt;ConfigReader&gt;() .As&lt;IConfigReader&gt;() .WithParameter("configSectionName", "sectionName");// parameter name, parameter value. It's the same of this: new NamedParameter("configSectionName", "sectionName") // Using a TYPED parameter: builder.RegisterType&lt;ConfigReader&gt;() .As&lt;IConfigReader&gt;() .WithParameter(new TypedParameter(typeof(string), "sectionName")); // Using a RESOLVED parameter: builder.RegisterType&lt;ConfigReader&gt;() .As&lt;IConfigReader&gt;() .WithParameter( new ResolvedParameter( (pi, ctx) =&gt; pi.ParameterType == typeof(string) &amp;&amp; pi.Name == "configSectionName", (pi, ctx) =&gt; "sectionName")); </code></pre> <p><code>NamedParameter</code> and <code>TypedParameter</code> can supply constant values only.</p> <p><code>ResolvedParameter</code> can be used as a way to supply values dynamically retrieved from the container, e.g. by resolving a service by name.</p></li> </ul> <p>In case you want to pass as parameter a service that is already registered, eg, <code>IConfiguration</code>, you can resolve the parameter as I show below:</p> <pre><code> builder.RegisterType&lt;Service&gt;() .As&lt;Iervice&gt;() .WithParameter((pi, ctx) =&gt; pi.ParameterType == typeof(IConfiguration) &amp;&amp; pi.Name == "configuration", (pi, ctx) =&gt; ctx.Resolve&lt;IConfiguration&gt;()); </code></pre> <p><strong><a href="http://docs.autofac.org/en/latest/resolve/parameters.html" rel="nofollow noreferrer">When you are resolving the component</a>:</strong></p> <p>One way to pass parameter at runtime in Autofac is using the <code>Resolve</code> method. You could create a class like this:</p> <pre><code>public class ContainerManager { public IContainer Container {get;set;} //... public T[] ResolveAllWithParameters&lt;T&gt;(IEnumerable&lt;Parameter&gt; parameters) { return Container.Resolve&lt;IEnumerable&lt;T&gt;&gt;(parameters).ToArray(); } } </code></pre> <p><code>Parameter</code> is an abstract class that belongs to Autofac, you can use the <code>NamedParameter</code> class to pass the parameters that you need. You can use the <code>ContainerManager</code> class as I show below:</p> <pre><code> public T[] ResolveAllWithParameters&lt;T&gt;(IDictionary&lt;string,object&gt; parameters ) { var _parameters=new List&lt;Parameter&gt;(); foreach (var parameter in parameters) { _parameters.Add( new NamedParameter(parameter.Key, parameter.Value)); } return ContainerManager.ResolveAllWithParameters&lt;T&gt;(_parameters); } </code></pre> <p>This way you can pass the parameters at runtime using a <code>Dictionary&lt;string, object&gt;</code> when you are resolving an specific component.</p> <p>Using an <a href="https://msdn.microsoft.com/en-us/library/bb383977.aspx" rel="nofollow noreferrer">Extension Method</a> could be even more simple:</p> <pre><code>public static class ContainerExtensions { public static T[] ResolveAllWithParameters&lt;T&gt;(this IContainer Container, IDictionary&lt;string, object&gt; parameters) { var _parameters = new List&lt;Parameter&gt;(); foreach (var parameter in parameters) { _parameters.Add(new NamedParameter(parameter.Key, parameter.Value)); } return Container.Resolve&lt;IEnumerable&lt;T&gt;&gt;(_parameters).ToArray(); } } </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.
    1. This table or related slice is empty.
    1. VO
      singulars
      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