Unity 配置和嵌套泛型类型
我是 Unity 新手,对 WCF 服务中的配置和嵌套泛型类型有疑问。一切都工作正常,直到我遇到需要定义一个工厂来返回基于通用接口的类型实例的情况。
代码看起来像这样:
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 配置看起来像这样:
<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>
配置似乎有效,但是当实例化 WCF 服务本身时,我收到以下错误:
InvalidOperationException - 该 当前类型, SomeNamespace.IFactory
2[SomeNamespace.BaseType,SomeNamespace.IFilterRunner
1[SomeNamespace.BaseType]], 是一个接口,不能是 建造的。您是否缺少类型 映射?
我尝试了各种不同的选项,但它们都会导致无效的配置。另外,我还有其他工厂定义,它们被定义为返回抽象基本类型或基于非泛型接口的类型实例,并且它们工作得很好。不同之处在于该工厂返回 IFilterRunner - 一个通用接口。
有人有什么建议吗?预先感谢一位困惑的开发人员。
I'm new to Unity and have a problem with configuration and nested generic types in a WCF service. Everything has been working fine until I hit the situation where I needed to define a factory which returns instances of types based on a generic 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;
}
}
The 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 instantiated I get the following error:
InvalidOperationException - The
current type,
SomeNamespace.IFactory2[SomeNamespace.BaseType,SomeNamespace.IFilterRunner
1[SomeNamespace.BaseType]],
is an interface and cannot be
constructed. Are you missing a type
mapping?
I’ve tried all sorts of different options but they all result in invalid configuration. Also, I have had other factory definitions that are defined to return either abstract base types or instances of types based on non-generic interfaces and they have worked fine. The difference is that this factory returns IFilterRunner - a generic interface.
Does anyone have any suggestions? Thanks in advance from a perplexed developer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过为 IFilterRunner 的别名指定通用参数解决了您的问题。
I fixed your problem by specifying generic argument for IFilterRunner's alias.