简体   繁体   English

使用Unity 2.1解析接口

[英]Resolve an Interface using Unity 2.1

I have a basic class that is trying to resolve an interface using Unity. 我有一个基本类正在尝试使用Unity解析接口。

            IUnityContainer containter = new UnityContainer();

            containter.RegisterType<IRepositoryFactory>();

            IRepositoryFactory repositoryFactory = containter.Resolve<IRepositoryFactory>();

My config file is as follows: 我的配置文件如下:

<unity>
    <containers>
        <container>
            <types>
                <type   name="IRepositoryFactory"
                        type="QuinntyneBrown.Interfaces.IRepositoryFactory"
                        mapTo="QuinntyneBrown.Photography.Factories.RepositoryFactory" />
            </types>
        </container>
    </containers>
</unity>

I'm getting the following error: 我收到以下错误:

Resolution of the dependency failed, type = "QuinntyneBrown.Interfaces.IRepositoryFactory", name = "(none)". 解决依赖关系失败,类型=“ QuinntyneBrown.Interfaces.IRepositoryFactory”,名称=“(none)”。 Exception occurred while: while resolving. 发生以下异常:正在解决。

Exception is: InvalidOperationException - The current type, QuinntyneBrown.Interfaces.IRepositoryFactory, is an interface and cannot be constructed. 异常是:InvalidOperationException-当前类型QuinntyneBrown.Interfaces.IRepositoryFactory是一个接口,无法构造。 Are you missing a type mapping? 您是否缺少类型映射?

At the time of the exception, the container was: 在发生异常时,容器为:

Resolving QuinntyneBrown.Interfaces.IRepositoryFactory,(none) 解决QuinntyneBrown.Interfaces.IRepositoryFactory,(无)

containter.RegisterType<IRepositoryFactory>();

This doesn't make any binding. 这没有任何约束力。 Load bindings from your config file instead: 从您的配置文件中加载绑定:

containter.LoadConfiguration();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM