AutoMapper Mapping Exception

929 views
Skip to first unread message

abhish...@indusnet.co.in

unread,
May 3, 2018, 7:20:34 AM5/3/18
to AutoMapper-users
I have used automapper dll version 2.2.1.0 and here is the mapping code below


 public List<T> ReadBySPWithParam<T>(string spName, List<DBParameterCommon> parameters)
       
{
           
Sqlhelper _helper = new Sqlhelper(ObjConnection);
           
List<T> varObj = null;
           
try
           
{
               
var sqlrdr = _helper.GetReaderByCmdWithParameter(spName, parameters);
               
using (_helper)
               
{
                   
if (sqlrdr.HasRows)
                   
{
                       
Mapper.Initialize(cfg => cfg.CreateMap<IDataReader, List<T>>());
                        varObj
= Mapper.DynamicMap<IDataReader, List<T>>(sqlrdr);
                   
}
               
}


               
return varObj;
           
}
           
catch (Exception ex)
           
{


               
throw ex;
           
}
       
}



now sometimes in here, it gives an exception in windows server, not in my local machine. kindly help me out here.
The error given is below

Source: BLL
Target: Utilities.ResponseContainer`1[Repository.SoilType] GetSoilTypeByLanguageID(BLL.MasterDataVM)
Type: AutoMapperMappingException
Message:

Mapping types:
IDataReader -> List`1
System.Data.IDataReader -> System.Collections.Generic.List`1[[Repository.SoilType, Repository, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]

Destination path:
List`1

Source value:
System.Data.SqlClient.SqlDataReader
Stack:    at BLL.SoilTypeBLL.GetSoilTypeByLanguageID(MasterDataVM masterDataVM)
   at RallisAPIServices.MasterDataController.MasterDataByLanguageID(MasterDataVM MasterDataVM)
   at lambda_method(Closure , Object , Object[] )
   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)
   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Filters.AuthorizationFilterAttribute.<ExecuteAuthorizationFilterAsyncCore>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Controllers.ExceptionFilterResult.<ExecuteAsync>d__0.MoveNext()
----------

ushenko...@gmail.com

unread,
Jun 19, 2018, 2:49:57 AM6/19/18
to AutoMapper-users
Hi there.
First of all you dont need to create map every time you have rows to read. Just do it only once on app initialization.
And second - automapper is a great tool but it can not read your mind. If you want to map idatareader to list of t then you need to supply some configuration. Try to use ConvertUsing method.
Reply all
Reply to author
Forward
0 new messages