c# - Adding and Retrieving data from request context -
i'm trying attach api key operationcontext outgoing message header follows:
public static void addapikeytoheader(string apikey, icontextchannel channel, string address) { using (operationcontextscope scope = new operationcontextscope(channel)) { messageheader header = messageheader.createheader("apikey", address, apikey); operationcontext.current.outgoingmessageheaders.add(header); } }
but have no idea how retrieve header on server side. i'm using service authorisation manager , current operating context , try retrieve header this:
public string getapikey(operationcontext operationcontext) { var request = operationcontext.requestcontext.requestmessage; var prop = (httprequestmessageproperty)request.properties[httprequestmessageproperty.name]; return prop.headers["apikey"]; }
but there no apikey header attached there. also, on debugging when inspect operationcontext cant seem see apikey header anywhere. can see i'm going wrong?
you can add custom header way :
using (channelfactory<imyservicechannel> factory = new channelfactory<imyservicechannel>(new nettcpbinding())) { using (imyservicechannel proxy = factory.createchannel(...)) { using ( operationcontextscope scope = new operationcontextscope(proxy) ) { guid apikey = guid.newguid(); messageheader<guid> mhg = new messageheader<guid>(apikey); messageheader untyped = mhg.getuntypedheader("apikey", "ns"); operationcontext.current.outgoingmessageheaders.add(untyped); proxy.dooperation(...); } } }
and service side, can header :
guid apikey = operationcontext.current.incomingmessageheaders.getheader<guid>("apikey", "ns");
Comments
Post a Comment