2016-12-23 28 views
1

我有一個ASP.NET API版本2.0與VB.Net在後端,我想在Global.asax文件中初始化Automapper。在這裏,我使用Auto Mapper 5.2版。我可以初始化使用C#代碼,但我不太確定VB.Net。谷歌上搜索,我發現something這裏以後就是我現在想:如何將Automapper註冊到vb.net global.asax文件中?

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

重載決策失敗,因爲沒有可訪問的「新」可以用這些參數調用:「公共重載的Sub New(configurationExpression作爲MapperConfigurationExpression)':因爲'MapperConfigurationExpression'不是委託類型,所以Lambda表達式不能轉換爲'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>(); 
    }); 

在APPLICAT ion_Start()很好地工作,但現在即使我轉換上面的代碼行然後我仍然面臨問題。我希望你對上述的幫助或建議。

+0

難道你不只是發表這個問題? – DavidG

+0

是的,但遺憾的是我無法檢索到這個帳戶密碼,而且那個帳戶是新帳戶,所以我要關閉該主題。我想沒有人會注意到,我真的需要緊急幫助。謝謝 – barsan

+0

我認爲它期望一個返回值。爲你的lambda嘗試使用'Function'而不是'Sub'。 – TyCobb

回答

1

無論出於何種原因,當爲行動內聯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使用正確的構造函數重載。

+0

非常感謝您的詳細解釋和不錯的答案。它真的爲我節省了很多時間。 – barsan

+1

沒問題。很高興我能幫上忙。這是一種痛苦和意外。 – TyCobb