c# - Using DTO in Azure Mobile Service .NET throws target invocation exception -
i use azure mobile services .net backend , know have use entity framework classes map database creation/migration. need use dtos serialize properties want, computed properties etc. i'm following field engineer example. automapper gave me pain although did supposed to.
i have checked couple of others blog , site, use automapper, others not, example this one. feel more comfortable not using automapper , create dtos on fly select() doing before when implementing web api.
i reverted tablecontroller class post, use entitydomainmanager , left getallposts method following.
public class postcontroller : tablecontroller<post> { private mobileservicecontext _context; protected override void initialize(httpcontrollercontext controllercontext) { base.initialize(controllercontext); _context = new mobileservicecontext(); domainmanager = new entitydomainmanager<post>(_context, request, services); } //[expandproperty("user")] // tables/post public iqueryable<postdto> getallpost() { return query().include("user").select(x => new postdto()); } }
i following error.
{"message":"an error has occurred.","exceptionmessage":"exception has been thrown target of invocation.","exceptiontype":"system.reflection.targetinvocationexception","stacktrace":" @ system.runtimemethodhandle.invokemethod(object target, object[] arguments, signature sig, boolean constructor)\r\n @ system.reflection.runtimemethodinfo.unsafeinvokeinternal(object obj, object[] parameters, object[] arguments)\r\n @ system.reflection.runtimemethodinfo.invoke(object obj, bindingflags invokeattr, binder binder, object[] parameters, cultureinfo culture)\r\n @ system.reflection.methodbase.invoke(object obj, object[] parameters)\r\n @ system.web.http.odata.query.odataqueryoptions.limitresults(iqueryable queryable, int32 limit, boolean& resultslimited)\r\n @ system.web.http.odata.query.odataqueryoptions.applyto(iqueryable query, odataquerysettings querysettings)\r\n @ system.web.http.odata.enablequeryattribute.applyquery(iqueryable queryable, odataqueryoptions queryoptions)\r\n @ system.web.http.odata.enablequeryattribute.executequery(object response, httprequestmessage request, httpactiondescriptor actiondescriptor)\r\n @ system.web.http.odata.enablequeryattribute.onactionexecuted(httpactionexecutedcontext actionexecutedcontext)\r\n @ system.web.http.filters.actionfilterattribute.onactionexecutedasync(httpactionexecutedcontext actionexecutedcontext, cancellationtoken cancellationtoken)\r\n--- end of stack trace previous location exception thrown ---\r\n @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task)\r\n @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task)\r\n @ system.runtime.compilerservices.taskawaiter.getresult()\r\n @ system.web.http.filters.actionfilterattribute.<callonactionexecutedasync>d__5.movenext()\r\n--- end of stack trace previous location exception thrown ---\r\n @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task)\r\n @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task)\r\n @ system.runtime.compilerservices.taskawaiter`1.getresult()\r\n @ system.web.http.filters.actionfilterattribute.<executeactionfilterasynccore>d__0.movenext()\r\n--- end of stack trace previous location exception thrown ---\r\n @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task)\r\n @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task)\r\n @ system.runtime.compilerservices.taskawaiter`1.getresult()\r\n @ system.web.http.filters.actionfilterattribute.<callonactionexecutedasync>d__5.movenext()\r\n--- end of stack trace previous location exception thrown ---\r\n @ system.runtime.exceptionservices.exceptiondispatchinfo.throw()\r\n @ system.web.http.filters.actionfilterattribute.<callonactionexecutedasync>d__5.movenext()\r\n--- end of stack trace previous location exception thrown ---\r\n @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task)\r\n @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task)\r\n @ system.runtime.compilerservices.taskawaiter`1.getresult()\r\n @ system.web.http.filters.actionfilterattribute.<executeactionfilterasynccore>d__0.movenext()\r\n--- end of stack trace previous location exception thrown ---\r\n @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task)\r\n @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task)\r\n @ system.runtime.compilerservices.taskawaiter`1.getresult()\r\n @ system.web.http.controllers.actionfilterresult.<executeasync>d__2.movenext()\r\n--- end of stack trace previous location exception thrown ---\r\n @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task)\r\n @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task)\r\n @ system.runtime.compilerservices.taskawaiter`1.getresult()\r\n @ system.web.http.filters.authorizationfilterattribute.<executeauthorizationfilterasynccore>d__2.movenext()\r\n--- end of stack trace previous location exception thrown ---\r\n @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task)\r\n @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task)\r\n @ system.runtime.compilerservices.taskawaiter`1.getresult()\r\n @ system.web.http.controllers.authenticationfilterresult.<executeasync>d__0.movenext()\r\n--- end of stack trace previous location exception thrown ---\r\n @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task)\r\n @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task)\r\n @ system.runtime.compilerservices.taskawaiter`1.getresult()\r\n @ system.web.http.controllers.exceptionfilterresult.<executeasync>d__0.movenext()\r\n--- end of stack trace previous location exception thrown ---\r\n @ system.runtime.exceptionservices.exceptiondispatchinfo.throw()\r\n @ system.web.http.controllers.exceptionfilterresult.<executeasync>d__0.movenext()\r\n--- end of stack trace previous location exception thrown ---\r\n @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task)\r\n @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task)\r\n @ system.runtime.compilerservices.taskawaiter`1.getresult()\r\n @ system.web.http.dispatcher.httpcontrollerdispatcher.<sendasync>d__1.movenext()","innerexception":{"message":"an error has occurred.","exceptionmessage":"the specified type member 'dateposted' not supported in linq entities. initializers, entity members, , entity navigation properties supported.","exceptiontype":"system.notsupportedexception","stacktrace":" @ system.data.entity.core.objects.elinq.expressionconverter.memberaccesstranslator.typedtranslate(expressionconverter parent, memberexpression linq)\r\n @ system.data.entity.core.objects.elinq.expressionconverter.typedtranslator`1.translate(expressionconverter parent, expression linq)\r\n @ system.data.entity.core.objects.elinq.expressionconverter.translateexpression(expression linq)\r\n @ system.data.entity.core.objects.elinq.expressionconverter.translatelambda(lambdaexpression lambda, dbexpression input)\r\n @ system.data.entity.core.objects.elinq.expressionconverter.translatelambda(lambdaexpression lambda, dbexpression input, dbexpressionbinding& binding)\r\n @ system.data.entity.core.objects.elinq.expressionconverter.methodcalltranslator.onelambdatranslator.translate(expressionconverter parent, methodcallexpression call, dbexpression& source, dbexpressionbinding& sourcebinding, dbexpression& lambda)\r\n @ system.data.entity.core.objects.elinq.expressionconverter.methodcalltranslator.onelambdatranslator.translate(expressionconverter parent, methodcallexpression call)\r\n @ system.data.entity.core.objects.elinq.expressionconverter.methodcalltranslator.sequencemethodtranslator.translate(expressionconverter parent, methodcallexpression call, sequencemethod sequencemethod)\r\n @ system.data.entity.core.objects.elinq.expressionconverter.methodcalltranslator.typedtranslate(expressionconverter parent, methodcallexpression linq)\r\n @ system.data.entity.core.objects.elinq.expressionconverter.typedtranslator`1.translate(expressionconverter parent, expression linq)\r\n @ system.data.entity.core.objects.elinq.expressionconverter.translateexpression(expression linq)\r\n @ system.data.entity.core.objects.elinq.expressionconverter.translateset(expression linq)\r\n @ system.data.entity.core.objects.elinq.expressionconverter.methodcalltranslator.thenbytranslatorbase.translate(expressionconverter parent, methodcallexpression call)\r\n @ system.data.entity.core.objects.elinq.expressionconverter.methodcalltranslator.sequencemethodtranslator.translate(expressionconverter parent, methodcallexpression call, sequencemethod sequencemethod)\r\n @ system.data.entity.core.objects.elinq.expressionconverter.methodcalltranslator.typedtranslate(expressionconverter parent, methodcallexpression linq)\r\n @ system.data.entity.core.objects.elinq.expressionconverter.typedtranslator`1.translate(expressionconverter parent, expression linq)\r\n @ system.data.entity.core.objects.elinq.expressionconverter.translateexpression(expression linq)\r\n @ system.data.entity.core.objects.elinq.expressionconverter.translateset(expression linq)\r\n @ system.data.entity.core.objects.elinq.expressionconverter.methodcalltranslator.thenbytranslatorbase.translate(expressionconverter parent, methodcallexpression call)\r\n @ system.data.entity.core.objects.elinq.expressionconverter.methodcalltranslator.sequencemethodtranslator.translate(expressionconverter parent, methodcallexpression call, sequencemethod sequencemethod)\r\n @ system.data.entity.core.objects.elinq.expressionconverter.methodcalltranslator.typedtranslate(expressionconverter parent, methodcallexpression linq)\r\n @ system.data.entity.core.objects.elinq.expressionconverter.typedtranslator`1.translate(expressionconverter parent, expression linq)\r\n @ system.data.entity.core.objects.elinq.expressionconverter.translateexpression(expression linq)\r\n @ system.data.entity.core.objects.elinq.expressionconverter.translateset(expression linq)\r\n @ system.data.entity.core.objects.elinq.expressionconverter.methodcalltranslator.unarysequencemethodtranslator.translate(expressionconverter parent, methodcallexpression call)\r\n @ system.data.entity.core.objects.elinq.expressionconverter.methodcalltranslator.sequencemethodtranslator.translate(expressionconverter parent, methodcallexpression call, sequencemethod sequencemethod)\r\n @ system.data.entity.core.objects.elinq.expressionconverter.methodcalltranslator.typedtranslate(expressionconverter parent, methodcallexpression linq)\r\n @ system.data.entity.core.objects.elinq.expressionconverter.typedtranslator`1.translate(expressionconverter parent, expression linq)\r\n @ system.data.entity.core.objects.elinq.expressionconverter.translateexpression(expression linq)\r\n @ system.data.entity.core.objects.elinq.expressionconverter.convert()\r\n @ system.data.entity.core.objects.elinq.elinqquerystate.getexecutionplan(nullable`1 formergeoption)\r\n @ system.data.entity.core.objects.objectquery`1.<>c__displayclass7.<getresults>b__6()\r\n @ system.data.entity.core.objects.objectcontext.executeintransaction[t](func`1 func, idbexecutionstrategy executionstrategy, boolean startlocaltransaction, boolean releaseconnectiononsuccess)\r\n @ system.data.entity.core.objects.objectquery`1.<>c__displayclass7.<getresults>b__5()\r\n @ system.data.entity.sqlserver.defaultsqlexecutionstrategy.execute[tresult](func`1 operation)\r\n @ system.data.entity.core.objects.objectquery`1.getresults(nullable`1 formergeoption)\r\n @ system.data.entity.core.objects.objectquery`1.<system.collections.generic.ienumerable<t>.getenumerator>b__0()\r\n @ system.data.entity.internal.lazyenumerator`1.movenext()\r\n @ system.collections.generic.list`1..ctor(ienumerable`1 collection)\r\n @ system.web.http.odata.query.truncatedcollection`1..ctor(iqueryable`1 source, int32 pagesize)\r\n @ system.web.http.odata.query.odataqueryoptions.limitresults[t](iqueryable`1 queryable, int32 limit, boolean& resultslimited)"}}
if convert entity framework class works. notice don't fill property, testing purposes.
i test locally iis express.
data objects , models.
public class post : entitydata { public datetimeoffset dateposted { get; set; } public string statustext { get; set; } public posttype typeofpost { get; set; } [foreignkey("id")] public virtual user user { get; set; } [foreignkey("id")] public virtual icollection<postphotourl> photourls { get; set; } } public class postdto { public postdto() { photourls = new hashset<postphotourldto>(); } public datetimeoffset dateposted { get; set; } public string statustext { get; set; } public int typeofpost { get; set; } public userdto user { get; set; } public icollection<postphotourldto> photourls { get; set; } }
searching internet couldn't find other more clear tutorial how use azure mobile services , dtos, though shouldn't introduce such difficulty. if have resources welcome.
i should mention if don't following test website raises error when trying test endpoints.
config.formatters.jsonformatter.serializersettings.referenceloophandling = referenceloophandling.ignore;
the exception snapshot:
i don't need have dtos project, if don't resolve don't understand right come , hunt me in long run of service.
i'm not sure think problem may line of code
return query().include("user").select(x => new postdto());
if youre not using automapper need manually parse properties in select statement this
return query().include("user") .select(x => new postdto() { dateposted = x.dateposted, statustext = x.statustext, typeofpost = x.typeofpost, user = new userdto { //your propertoes here //eg id = x.user.id etc } );
Comments
Post a Comment