c# - Web API2 Serialization exception -
i have web api2 application have scaffolded using ef.
i trying serialize following class (which contains lazy loaded navigation properties):
namespace models { public class speciality { [key] [databasegenerated(databasegeneratedoption.identity)] public int specialityid { get; set; } public string name { get; set; } public virtual icollection<doctor> doctors { get; set; } public virtual icollection<medicalfacility> medicalfacilities { get; set; } } }
the controller:
namespace findamedicservice.controllers { public class specialitiescontroller : apicontroller { private applicationdbcontext db = new applicationdbcontext(); // get: api/specialities public iqueryable<speciality> getspeciality() { return db.speciality; } } }
when try access particular service list of "specialities", following error:
<error> <message>an error has occurred.</message> <exceptionmessage> 'objectcontent`1' type failed serialize response body content type 'application/xml; charset=utf-8'. </exceptionmessage> <exceptiontype>system.invalidoperationexception</exceptiontype> <stacktrace/> <innerexception> <message>an error has occurred.</message> <exceptionmessage> type 'system.data.entity.dynamicproxies.speciality_dc264d6dbbaf52fb19e27f20dcc47da1141620cec0ccaf6e2def4d8907fa7c8c' data contract name 'speciality_dc264d6dbbaf52fb19e27f20dcc47da1141620cec0ccaf6e2def4d8907fa7c8c:http://schemas.datacontract.org/2004/07/system.data.entity.dynamicproxies' not expected. consider using datacontractresolver if using datacontractserializer or add types not known statically list of known types - example, using knowntypeattribute attribute or adding them list of known types passed serializer. </exceptionmessage> <exceptiontype> system.runtime.serialization.serializationexception </exceptiontype> <stacktrace> @ system.runtime.serialization.xmlobjectserializerwritecontext.serializeandverifytype(datacontract datacontract, xmlwriterdelegator xmlwriter, object obj, boolean verifyknowntype, runtimetypehandle declaredtypehandle, type declaredtype) @ system.runtime.serialization.xmlobjectserializerwritecontext.serializewithxsitype(xmlwriterdelegator xmlwriter, object obj, runtimetypehandle objecttypehandle, type objecttype, int32 declaredtypeid, runtimetypehandle declaredtypehandle, type declaredtype) @ system.runtime.serialization.xmlobjectserializerwritecontext.internalserialize(xmlwriterdelegator xmlwriter, object obj, boolean isdeclaredtype, boolean writexsitype, int32 declaredtypeid, runtimetypehandle declaredtypehandle) @ writearrayofspecialitytoxml(xmlwriterdelegator , object , xmlobjectserializerwritecontext , collectiondatacontract ) @ system.runtime.serialization.collectiondatacontract.writexmlvalue(xmlwriterdelegator xmlwriter, object obj, xmlobjectserializerwritecontext context) @ system.runtime.serialization.xmlobjectserializerwritecontext.writedatacontractvalue(datacontract datacontract, xmlwriterdelegator xmlwriter, object obj, runtimetypehandle declaredtypehandle) @ system.runtime.serialization.xmlobjectserializerwritecontext.serializewithoutxsitype(datacontract datacontract, xmlwriterdelegator xmlwriter, object obj, runtimetypehandle declaredtypehandle) @ system.runtime.serialization.datacontractserializer.internalwriteobjectcontent(xmlwriterdelegator writer, object graph, datacontractresolver datacontractresolver) @ system.runtime.serialization.datacontractserializer.internalwriteobject(xmlwriterdelegator writer, object graph, datacontractresolver datacontractresolver) @ system.runtime.serialization.xmlobjectserializer.writeobjecthandleexceptions(xmlwriterdelegator writer, object graph, datacontractresolver datacontractresolver) @ system.runtime.serialization.datacontractserializer.writeobject(xmlwriter writer, object graph) @ system.net.http.formatting.xmlmediatypeformatter.writetostream(type type, object value, stream writestream, httpcontent content) @ system.net.http.formatting.xmlmediatypeformatter.writetostreamasync(type type, object value, stream writestream, httpcontent content, transportcontext transportcontext, cancellationtoken cancellationtoken) --- end of stack trace previous location exception thrown --- @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task) @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task) @ system.web.http.webhost.httpcontrollerhandler.<writebufferedresponsecontentasync>d__1b.movenext() </stacktrace> </innerexception> </error>
i didn't have error before when did not have 2 navigation properties in speciality class (doctors, medicalfacilities).
any appreciated.
just quick recap - why returning iqueryable
, entity model result?
in opinion should transform entity model kind of dto needed properties + change action signature ienumerable<t>
.
what serializer doesn't know how serialize dynamic proxies ef creates virtual
properties.
Comments
Post a Comment