1

I've a ASP.NET API Version 2.0 with VB.Net in back end where I'm trying to initialize Automapper in Global.asax file. Here, 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()

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. I would appreciate your help or suggestion on the above.

Community
  • 1
  • 1
barsan
  • 2,431
  • 16
  • 45
  • 62
  • Didn't you just post this question? – DavidG Dec 23 '16 at 22:03
  • Yes, but sorry for that I was unable to retrieve this account password and that one is a new account so I'm going to close that topic. I guess that one no one will notice and I really need help on this urgently. Thanks – barsan Dec 23 '16 at 22:06
  • I think it expects a return value. Try `Function` instead of `Sub` for your lambda. – TyCobb Dec 23 '16 at 22:12
  • Thanks for the suggestion but just now I've tried with function and return value but still the error remains the same. – barsan Dec 23 '16 at 22:15
  • I've tried that as well. Actually the error is highlighting that I'm missing the parameters. But I'm not so sure what to put into the parameters. – barsan Dec 23 '16 at 22:25
  • It looks like what you have should work from what I see on GitHub. `public MapperConfiguration(Action configure)`. You may want to actually set it to a variable and pass it in because it is definitely hitting the wrong constructor – TyCobb Dec 23 '16 at 22:27
  • Could you please help me to explain this more with an example? – barsan Dec 23 '16 at 22:32
  • 1
    http://pastebin.com/c9xfhk66 – TyCobb Dec 23 '16 at 22:36
  • Thanks a lot. It's working now. You really saved my day. Please send it here as an answer so that I can accept it. Thanks again. – barsan Dec 23 '16 at 22:44

1 Answers1

3

For whatever reason VB.NET is not using the correct constructor when you inline the Sub for the Action.

Module AutoMapperConfiguration
    Public MapperConfiguration As IMapper

    Public Sub Configure()
        Dim configAction As Action(Of IMapperConfigurationExpression) = Sub(cfg) cfg.AddProfile(Of EntityMapProfile)()
        Dim config = New MapperConfiguration(configAction)

        MapperConfiguration = config
    End Sub

End Module

The above will force your lambda to the correct type of Action(Of IMapperConfigurationExpression) and thereby force VB.NET to use the correct constructor overload.

TyCobb
  • 8,909
  • 1
  • 33
  • 53