c# - How to change xsd:date format in svcutil.exe generated classes -
i generated c# classes .wsdl file , works. have following issue. service formats xsd:date
types in response incorrect. example:
<date xsi:type="xsd:date">2016-01-27 14:20:30</date>
but should 1 of these:
<date xsi:type="xsd:date">2016-01-27</date> <date xsi:type="xsd:datetime">2016-01-27t14:20:30</date>
and because of exception
unhandled exception: system.servicemodel.communicationexception: error in deserializing body of reply message operation 'createvacature'. ---> system.invalidoperationexception: there error in xml document (2, 646). ---> system.formatexception: string not recognized valid datetime.
how can override date parsing? or other way fix it? implementing manually without svcutil.exe overkill.
here solution. intercept service response before parsing , manually edit it.
here date fixing functionality:
public class messagedatefixer : iclientmessageinspector { public object beforesendrequest(ref message request, iclientchannel channel) { return null; } public void afterreceivereply(ref message reply, object correlationstate) { xmldocument document = new xmldocument(); memorystream memorystream = new memorystream(); xmlwriter xmlwriter = xmlwriter.create(memorystream); reply.writemessage(xmlwriter); xmlwriter.flush(); memorystream.position = 0; document.load(memorystream); fixmessage(document); memorystream.setlength(0); xmlwriter = xmlwriter.create(memorystream); document.writeto(xmlwriter); xmlwriter.flush(); memorystream.position = 0; xmlreader xmlreader = xmlreader.create(memorystream); reply = message.createmessage(xmlreader, int.maxvalue, reply.version); } private static void fixmessage(xmldocument document) { fixallnodes(document.childnodes); } private static void fixallnodes(xmlnodelist list) { foreach (xmlnode node in list) { fixnode(node); } } private static void fixnode(xmlnode node) { if (node.attributes != null && node.attributes["xsi:type"] != null) { if (node.attributes["xsi:type"].value == "xsd:date") { node.attributes["xsi:type"].value = "xsd:datetime"; node.innerxml = node.innerxml.replace(" ", "t"); } } fixallnodes(node.childnodes); } }
here auxiliary class:
public class datefixerbehavior : iendpointbehavior { public void applyclientbehavior(serviceendpoint endpoint, clientruntime clientruntime) { clientruntime.messageinspectors.add(new messagedatefixer()); } public void validate(serviceendpoint endpoint) { } public void addbindingparameters(serviceendpoint endpoint, bindingparametercollection bindingparameters) { } public void applydispatchbehavior(serviceendpoint endpoint, endpointdispatcher endpointdispatcher) { } }
here usage:
postertoolclient poster = new postertoolclient(); poster.endpoint.behaviors.add(new datefixerbehavior());
Comments
Post a Comment