Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I resolve a service based upon generic type argument in Castle Windsor?
    primarykey
    data
    text
    <p>I have an installer like this:</p> <pre><code>public void Install(IWindsorContainer container, IConfigurationStore store) { //Services container.Register( Classes.FromAssemblyNamed(ASSEMBLY_NAME) .BasedOn&lt;IService&gt;() .WithServiceFirstInterface() .LifestyleTransient()); //Repository container.Register( Component.For(typeof(IRepository&lt;&gt;)) .ImplementedBy(typeof(Repository&lt;&gt;)) .LifestyleTransient()); //Contexts container.Register( Component.For(typeof(Context&lt;IGlobalObject&gt;)) .ImplementedBy(typeof(GlobalContext&lt;&gt;)).LifestyleTransient()); } </code></pre> <p>The repository is an open generic, and it has a Context constructor injected, which is a wrapper around an EF DbContext, but takes a type argument to indicate the database it needs to connect to. The idea is that I have several DbContexts as I need to connect to multiple databases, and I want windsor to resolve the appropriate DBcontext based upon the type argument passed to the repository.</p> <p>The repositories type argument is constrained to the following (GlobalObject and GlobalContext refer to types associated with 1 such database):</p> <pre><code>public interface IGlobalObject : IObject {} public interface IObject { int Key { get; set; } } </code></pre> <p>However, Windsor cannot resolve the context, and I cannot work out why? It is registered and in the container, but it cannot resolve.</p> <p><strong>EDIT:</strong></p> <p>Code for GlobalContext:</p> <pre><code>public class GlobalContext&lt;T&gt; : Context&lt;T&gt; where T : IGlobalObject { private const string GLOBAL_CSTR = "Global"; public GlobalContext() : base(ConfigurationManager.ConnectionStrings[GLOBAL_CSTR].ConnectionString) {} public DbSet&lt;Company&gt; Companies { get; set; } public DbSet&lt;ConnectionString&gt; ConnectionStrings { get; set; } public DbSet&lt;Server&gt; Servers { get; set; } } </code></pre> <p>Context:</p> <pre><code>//Wrapper around dbcontext which enforces type public abstract class Context&lt;T&gt; : DbContext where T : IObject { protected Context() {} protected Context(string connectionString) : base(connectionString){} } </code></pre> <p><strong>Edit 2:</strong></p> <p>If i specify the concrete types for every scenario it works, so it is clearly something to do with matching on the interface.</p> <pre><code>//Contexts container.Register( Component.For(typeof(Context&lt;Server&gt;)) .ImplementedBy(typeof(GlobalContext&lt;Server&gt;)).LifestyleTransient()); </code></pre>
    singulars
    1. This table or related slice is empty.
    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. 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