.net - Web Api Caching and "Can't Access Closed Stream" -


i trying use caching library asp.net web api (https://github.com/filipw/aspnetwebapi-outputcache)

after installing got error , narrowed down class in xmlmediatypeformatter class.

cannot access closed stream 

i believe has task.factory.starnew() or xmltextwriter closing steam somewhere in writetostreamasync method. there way handle code?

 public class customxmlformatter : xmlmediatypeformatter      {         public customxmlformatter()         {             supportedmediatypes.add(new mediatypeheadervalue("application/xml"));             supportedmediatypes.add(new mediatypeheadervalue("text/xml"));             encoding = new utf8encoding(false, true);         }          protected utf8encoding encoding { get; set; }          public override bool canreadtype(type type)         {             if (type == null)                 return false;              return true;         }         public override bool canwritetype(type type)         {             if (type == null)                 return false;              return true;         }          public override task<object> readfromstreamasync(type type, stream readstream, httpcontent content, iformatterlogger formatterlogger)         {             return task.factory.startnew(() =>             {                 using (var xmlr = new xmltextreader(readstream))                 {                     var serializer = new datacontractserializer(type);                     return serializer.readobject(xmlr);                 }             });         }          public override task writetostreamasync(type type, object value, stream writestream, httpcontent content, transportcontext transportcontext)         {             var serializer = new datacontractserializer(type, "response", "");              return task.factory.startnew(() =>             {                 using (var xmlw = new xmltextwriter(writestream, encoding))                 {                     xmlw.formatting = formatting.indented;                     serializer.writeobject(xmlw, value);                 }             });         }     } 

could share more details why need create custom xml formatter here...since web api's default xml formatter seems sufficient in case?

following xmlwritersettings web api's xmlmediatypeformatter uses when writing out xml.

var writersettings = new xmlwritersettings {     omitxmldeclaration = true,     closeoutput = false,     checkcharacters = false }; 

the above settings used following...you similar stuff in case too.

var xmlwriter = xmlwriter.create(writestream, writersettings);  serializer.writeobject(xmlwriter, value); 

just fyi...the above snippets of code taken web api's xmlmediatypeformatter...you can take @ it's source code more ideas...


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -