2

Hi I am using Unity as my IoC container and am trying to get an instance of an object using ServiceLocator.

This is the code I am trying to execute:

ServiceLocator.Current.GetInstance<IAutoMapperRegisterFactory>()

I have set in my configuration file the object that unity has to instantiate:

container.RegisterType<IAutoMapperRegisterFactory, AutoMapperRegisterFactory>();

Now my curent AutoMapperRegisterFactory looks something like this:

public class AutoMapperRegisterFactory : IAutoMapperRegisterFactory
{
    private readonly IRegisterAutoMapper m_RegisterAutoMapper;

    public AutoMapperRegisterFactory(IRegisterAutoMapper registerAutoMapper)
    {
        m_RegisterAutoMapper = registerAutoMapper;
    }

    public IEnumerable<IRegisterAutoMapper> GetRegisteredAutoMappers()
    {
        var registeredAutoMappers = new List<IRegisterAutoMapper> { m_RegisterAutoMapper };
        return registeredAutoMappers;
    }
}

And if I try to run this I get this error:

 The type IRegisterAutoMapper does not have an accessible constructor.

Previously my AutomapperRegisterFactory looked like this:

public class AutoMapperRegisterFactory : IAutoMapperRegisterFactory
{
    public IEnumerable<IRegisterAutoMapper> GetRegisteredAutoMappers()
    {
        var registeredAutoMappers = new List<IRegisterAutoMapper> { new RegisterAutoMapper(new RegisterMappings(), new RegisterCustomMappings()) };
        return registeredAutoMappers;
    }
}

And everything worked just fine.What am I doing wrong and how can I correct it?

4

1 回答 1

1

组件AutoMapperRegisterFactory要求作为依赖IRegisterAutoMapper组件。 IRegisterAutoMapperRegisterAutoMapper类型实现,但类型的构造函数RegisterAutoMapper需要两个参数:RegisterMappingsRegisterCustomMappings,因此您必须提供它们。

例如:

container.RegisterType<..., RegisterMappings>();
container.RegisterType<..., RegisterCustomMappings>();
于 2013-07-11T19:13:46.057 回答