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> {
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.