繁体   English   中英

如何将Automapper注册到vb.net global.asax文件中?

[英]How to register Automapper into vb.net global.asax file?

我有一个后端为VB.Net的ASP.NET API 2.0版,在这里我试图在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: 

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

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

终端模块

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

 AutoMapperConfiguration.Configure()

但是上一次我使用C#在global.asax文件中使用以下代码行完成了此操作

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

在Application_Start()下效果很好,但是现在即使我转换了上面的代码行,仍然面临问题。 感谢您在上述方面的帮助或建议。

无论出于何种原因,当内联动作的Sub时,VB.NET都没有使用正确的构造函数。

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

上面的代码将强制您的lambda使用正确的Action(Of IMapperConfigurationExpression)类型,从而强制VB.NET使用正确的构造函数重载。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM