2

I'm trying to initialize Automapper in Global.asax file using vb.net in ASP.NET API Version 2.0. I'm using Auto Mapper version 5.2. I can initialize using the C# code but I am not so sure about the VB.Net. After googling I've found something and here is what I'm trying now:

Module AutoMapperConfiguration
Public MapperConfiguration As IMapper
Public Sub Configure()
    Dim config = New MapperConfiguration(//in this line I'm getting an error: 

Overload resolution failed because no accessible 'New' can be called with these arguments: 'Public Overloads Sub New(configurationExpression As MapperConfigurationExpression)': Lambda expression cannot be converted to 'MapperConfigurationExpression' because 'MapperConfigurationExpression' is not a delegate type.

        Sub(cfg)
            cfg.AddProfile(New EntityMapProfile())
        End Sub)
    MapperConfiguration = config.CreateMapper()
 End Sub
End Module

Then I've called this module from the Application_Start()

  AutoMapperConfiguration.Configure()

Here I'm not facing any error but on the previous line I'm facing error which is causing the issue. But last time I've done this using C# with the following line of code in the global.asax file

  Mapper.Initialize(x =>
        {
            x.AddProfile<EntityMapProfile>();
        });

Under Application_Start() which worked nicely but now even if I convert those above lines of code then still I'm facing issues. Here, I would like to mention that I've found the VB.Net code from the following Link I would appreciate if any one can help or suggest me on the above. Thanks.

Community
  • 1
  • 1
user7336033
  • 271
  • 3
  • 16

1 Answers1

2

The VB equivalent of this C#:

Mapper.Initialize(x =>
    {
        x.AddProfile<EntityMapProfile>();
    });

is this:

Mapper.Initialize(Sub(x)
        x.AddProfile(Of EntityMapProfile)()
    End Sub)

Did you try that?

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • Here's the def of the profile that works with the code above Public Class EntityMapProfile Inherits Profile Public sub New() CreateMap(Of BudgetChangeRequest, BCRViewModel)() End sub End Class – Brad Irby Aug 02 '19 at 09:00