IWindsorContainer不包含“GetService”的定义

时间:2014-02-17 14:12:39

标签: .net inversion-of-control castle-windsor castle

我将一个旧的Castle.Windsor dll更新为新的NuGet ...

其实我还有以下错误:

  

'Castle.Windsor.IWindsorContainer'不包含的定义   'GetService'并没有扩展方法'GetService'接受第一个   可以找到类型为“Castle.Windsor.IWindsorContainer”的参数   (您是否缺少using指令或程序集引用?)

这是我的代码:

/// <summary>
/// IoC Abstraction
/// </summary>
public class ServiceLocator : IServiceLocator
{
    private IWindsorContainer _container;

    private ServiceLocator(IWindsorContainer container)
    {
        _container = container;
    }

    private static IServiceLocator _current;

    public static IServiceLocator Current
    {
        get { return _current; }
        set { _current = value; }
    }

    public static void Register(IWindsorContainer container)
    {
        _current = new ServiceLocator(container);
    }

    public T GetInstance<T>()
    {
        return (T)_container.GetService(typeof(T));
    }

    public object GetInstance(Type t)
    {
        return _container.GetService(t);
    }

    public T GetService<T>()
    {
        return (T)_container.GetService(typeof(T));
    }

    public object GetService(Type t)
    {
        return _container.GetService(t);
    }
}

1 个答案:

答案 0 :(得分:3)

您应该使用Resolve方法重载:

public T GetInstance<T>()
{
    return _container.Resolve<T>();
}

public object GetInstance(Type t)
{
    return _container.Resolve(t);
}