Unity configuration and nested generic types

I am new to Unity and have a problem with the configuration and nested generic types in the WCF service. Everything worked fine until I got into a situation where I needed to define a factory that returns type instances based on a common interface.

The code looks something like this:

public interface IFactory<T, TResult> { TResult CreateInstance(T input); } public interface IFilter<T> { // throws an exception if an item is to be filtered void Filter(T itemToFilter); } // classes implementing this interface can have multiple filters public interface IFilterRunner<T> { void RunFilters(T itemToFilter); } public class FilterRunnerFactory : IFactory<BaseType, IFilterRunner<BaseType>> { public IFilterRunner<BaseType> CreateInstance(BaseType input) { if (input is SubType) { return new SubTypeFilterRunner(); } throw new InvalidOperationException("Could not create an IFilterRunner for the input."); } } public class Service { private readonly IFactory<BaseType, IFilterRunner<BaseType>> _filterRunnerFactory; public Service(IFactory<BaseType, IFilterRunner<BaseType>> filterRunnerFactory) { _filterRunnerFactory = filterRunnerFactory; } } 

Unity configuration looks something like this:

 <unity xmlns="http://schemas.microsoft.com/practices/2010/unity"> <alias alias="BaseType" type="SomeNamespace.BaseType, SomeAssembly, Version=1.0.0.0, Culture=neutral" /> <alias alias="IFactory" type="SomeNamespace.IFactory`2, SomeAssembly, Version=1.0.0.0, Culture=neutral" /> <alias alias="IFilterRunner" type="SomeNamespace.IFilterRunner`1, SomeAssembly, Version=1.0.0.0, Culture=neutral" /> <alias alias="IService" type="SomeNamespace.IService, SomeAssembly, Version=1.0.0.0, Culture=neutral" /> <containers> <container> <register type="IFactory[BaseType,IFilterRunner]" mapTo="SomeNamespace.FilterRunnerFactory, SomeAssembly, Version=1.0.0.0, Culture=neutral"/> <register type="IService" mapTo="SomeNamespace.Service, SomeAssembly, Version=1.0.0.0, Culture=neutral"> <constructor> <param name="filterRunnerFactory" /> </constructor> </register> </container> </containers> </unity> 

The configuration seems valid, but when the WCF service itself is created, I get the following error:

InvalidOperationException - the current type, SomeNamespace.IFactory 2[SomeNamespace.BaseType,SomeNamespace.IFilterRunner 1 [SomeNamespace.BaseType]], is an interface and cannot be built. Are you missing the display type?

Ive tried all sorts of options, but they all lead to an invalid configuration. In addition, I had other factory definitions that were defined to return either abstract base types or type instances based on non-common interfaces, and they worked just fine. The difference is that this factory returns IFilterRunner - a common interface.

Does anyone have any suggestions? Thanks in advance from the bewilderment of the developer.

+4
source share
1 answer

I fixed your problem by specifying a generic argument for the alias IFilterRunner.

 <unity xmlns="http://schemas.microsoft.com/practices/2010/unity"> <alias alias="BaseType" type="SomeNamespace.BaseType, SomeAssembly, Version=1.0.0.0, Culture=neutral" /> <alias alias="IFactory" type="SomeNamespace.IFactory`2, SomeAssembly, Version=1.0.0.0, Culture=neutral" /> <alias alias="IFilterRunner" type="SomeNamespace.IFilterRunner`1[[SomeNamespace.BaseType, SomeAssembly, Version=1.0.0.0, Culture=neutral]], SomeAssembly, Version=1.0.0.0, Culture=neutral" /> <alias alias="IService" type="SomeNamespace.IService, SomeAssembly, Version=1.0.0.0, Culture=neutral" /> <containers> <container> <register type="IFactory[BaseType,IFilterRunner]" mapTo="SomeNamespace.FilterRunnerFactory, SomeAssembly, Version=1.0.0.0, Culture=neutral"/> <register type="IService" mapTo="SomeNamespace.Service, SomeAssembly, Version=1.0.0.0, Culture=neutral"> <constructor> <param name="filterRunnerFactory" /> </constructor> </register> </container> </containers> </unity> 
+2
source

Source: https://habr.com/ru/post/1333933/


All Articles