【问题标题】:How to initialize automapper in vb.net global.asax如何在 vb.net global.asax 中初始化 automapper
【发布时间】:2016-12-23 20:54:56
【问题描述】:

我正在尝试使用 ASP.NET API 版本 2.0 中的 vb.net 在 Global.asax 文件中初始化 Automapper。我正在使用 Auto Mapper 5.2 版。我可以使用 C# 代码进行初始化,但我对 VB.Net 不太确定。谷歌搜索后,我发现了一些东西,这就是我现在正在尝试的:

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

重载解析失败,因为无法使用以下参数调用可访问的“新建”: “Public Overloads Sub New(configurationExpression As MapperConfigurationExpression)”:Lambda 表达式无法转换为“MapperConfigurationExpression”,因为“MapperConfigurationExpression”不是委托类型。

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

然后我从 Application_Start() 调用了这个模块

  AutoMapperConfiguration.Configure()

在这里我没有遇到任何错误,但在上一行我遇到了导致问题的错误。 但是上次我在 global.asax 文件中使用 C# 和以下代码行完成了这项工作

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

在 Application_Start() 下运行良好,但现在即使我转换了上面的代码行,我仍然面临问题。 在这里,我想提一下,我从以下Link 找到了 VB.Net 代码,如果有人可以在上面提供帮助或建议,我将不胜感激。谢谢。

【问题讨论】:

    标签: asp.net asp.net-mvc vb.net automapper-5


    【解决方案1】:

    这个 C# 的 VB 等价物:

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

    这是:

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

    你试过了吗?

    【讨论】:

    • 这是与上面代码一起使用的配置文件的定义 Public Class EntityMapProfile Inherits Profile Public sub New() CreateMap(Of BudgetChangeRequest, BCRViewModel)() End sub End Class
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-05
    • 2021-09-22
    • 1970-01-01
    相关资源
    最近更新 更多