c# - WCF IClientMessageInspector.BeforeSendRequest modify request -
i'm trying modify request in wcf service.
public object beforesendrequest(ref message request, iclientchannel channel) { string xmlrequest = request.tostring(); xdocument xdoc = xdocument.parse(xmlrequest); //some request modifications //here have xml in want send request = message.createmessage(request.version, request.headers.action, whathere?); request.headers.clear(); return null; }
but don't know can set in createmessage
or maybe different way set xml request.
you can pass xmlreader object representing modified message. below example taken article how inspect , modify wcf message via custom messageinspector.
public object beforesendrequest(ref system.servicemodel.channels.message request, system.servicemodel.iclientchannel channel) { console.writeline("====simplemessageinspector+beforesendrequest called====="); //modify request send client(only customize message body) request = transformmessage2(request); return null; } //only read , modify message body part private message transformmessage2(message oldmessage) { message newmessage = null; //load old message xml messagebuffer msgbuf = oldmessage.createbufferedcopy(int.maxvalue); message tmpmessage = msgbuf.createmessage(); xmldictionaryreader xdr = tmpmessage.getreaderatbodycontents(); xmldocument xdoc = new xmldocument(); xdoc.load(xdr); xdr.close(); //transform xmldocument xmlnamespacemanager nsmgr = new xmlnamespacemanager(xdoc.nametable); nsmgr.addnamespace("a", "urn:test:datacontracts"); xmlnode node = xdoc.selectsinglenode("//a:stringvalue", nsmgr); if(node!= null) node.innertext = "[modified in simplemessageinspector]" + node.innertext; memorystream ms = new memorystream(); xmlwriter xw = xmlwriter.create(ms); xdoc.save(xw); xw.flush(); xw.close(); ms.position = 0; xmlreader xr = xmlreader.create(ms); //create new message modified xml document newmessage = message.createmessage(oldmessage.version, null,xr ); newmessage.headers.copyheadersfrom(oldmessage); newmessage.properties.copyproperties(oldmessage.properties); return newmessage; }
Comments
Post a Comment