Domanda

Hi I am trying to add AutoMapper into my app but I seem to have some problems with it in th eintegrationg.HEre is it what I have so far.

In order to not create a direct dependency to Automapper I have created a simple maping for it's most basic functionalities:

public class AutoMapper : IAutoMapper
{
    public void CreateMap<TFrom, TTo>()
    {
        Mapper.CreateMap<TFrom, TTo>();
    }

    public TTo Map<TFrom, TTo>(TFrom data)
    {
        return Mapper.Map<TFrom, TTo>(data);
    }
}

I have created a configuration file:

 public class AutoMapperConfig
{
    private readonly IAutoMapper mapper;

    public AutoMapperConfig(IAutoMapper mapper)
    {
        this.mapper = mapper;
    }

    public void RegisterMappings()
    {
        mapper.CreateMap<ProductDTO , ProductDataContract>();
    }
}

And have added a call in my Global.Asax:

new AutoMapperConfig(new AutoMapper()).RegisterMappings();

I have this two objects beetwen I want to create a mapping:

public class ProductDTO
{
    public int ProductId { get; set; }
    public int CategoryId { get; set; }
    public int SubcategoryId { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
    public string Description { get; set; }
    public string ImagePath { get; set; }
    public int NumberOfProducts { get; set; }
}

public class ProductDataContract
{
    public int ProductId { get; set; }
    public int CategoryId { get; set; }
    public int SubcategoryId { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
    public string Description { get; set; }
    public string ImagePath { get; set; }
    public int NumberOfProducts { get; set; }
}

And in my code I placed this line for testing purposes:
var products = productCatalogService.GetProducts(); ProductDTO ceva = products.FirstOrDefault(); var productsDataContract = mapper.Map(ceva);

THe problem is that whn I run my app I imediatly get an exception in Automapper when trying to CreateMap.Here is the type initialization exception message:

This type is not supported on this platform IDictionaryFactory

What am I doing wrong?

È stato utile?

Soluzione

This sounds like a reference issue. You should have a reference to AutoMapper.dll and AutoMapper.Net4.dll from your project. If you install via nuget this should be taken care for you.

With AutoMapper 3.0 there has been a change in the packages for different platforms.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top