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コンポーネントとして必要です。 IRegisterAutoMapper型によって実装されますRegisterAutoMapperが、型のコンストラクターにはとのRegisterAutoMapper2 つの引数が必要なので、それらを指定する必要があります。RegisterMappingsRegisterCustomMappings

例えば:

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