c# - Where do I set the CookieContainer on a Service Reference? -


when adding webservice reference asmx service on .net 2.0 project example,

var objservice = new namespace.groupservices(); 

there exists,

objservice.cookiecontainer = new system.net.cookiecontainer(); 

when adding servicereference asmx service on .net 4.0 project example,

var objservice = new namespace.groupservicessoapclient(); 

there isn't cookiecontainer property objservice

a similar question asked here no positive solution.

could please guide find property?

in contrast asmx web services tied http transport, wcf allows various transport protocols used. therefore, not protocol-specific options (such cookies http transport) available in wcf service reference.

you can, however, add message inspector inspects messages sent between client , server. article describes way send cookies server.

i've extended sample use cookiecontainer. also, following code shows how evaluate set-cookie header sent server add new cookies container. please note sample shows basic outline, might need extension or more validation. however, in simple scenario worked.

the following snippet shows test method of wcf service hosted on iis , integrated in asp.net framework. echoes cookies sent server in string , adds 2 new ones:

public string getdata(int value) {     var reply = string.join(", ",                      x in httpcontext.current.request.cookies.allkeys                      select x + "=" + httpcontext.current.request.cookies[x].value);     httpcontext.current.response.cookies.add(new httpcookie("test", "test123"));     httpcontext.current.response.cookies.add(new httpcookie("test2", "test1234"));     return reply; } 

the following test program creates cookiecontainer cookies, adds demo cookie , registers new behavior endpoint of service:

class program {     static void main(string[] args)     {         var cookiecont = new cookiecontainer();         using(var svc = new testservicereference.testserviceclient())         {             cookiecont.add(svc.endpoint.address.uri, new cookie("testclientcookie", "cookie value 123"));             var behave = new cookiebehavior(cookiecont);             svc.endpoint.endpointbehaviors.add(behave);             var data = svc.getdata(123);             console.writeline(data);             console.writeline("---");             foreach (cookie x in cookiecont.getcookies(svc.endpoint.address.uri))                 console.writeline(x.name + "=" + x.value);         }         console.readline();     } } 

the behavior serves purpose of adding custom message inspector , handing on cookiecontainer:

public class cookiebehavior : iendpointbehavior {     private cookiecontainer cookiecont;      public cookiebehavior(cookiecontainer cookiecont)     {         this.cookiecont = cookiecont;     }      public void addbindingparameters(serviceendpoint serviceendpoint,         system.servicemodel.channels         .bindingparametercollection bindingparameters) { }      public void applyclientbehavior(serviceendpoint serviceendpoint,         system.servicemodel.dispatcher.clientruntime behavior)     {         behavior.messageinspectors.add(new cookiemessageinspector(cookiecont));     }      public void applydispatchbehavior(serviceendpoint serviceendpoint,         system.servicemodel.dispatcher         .endpointdispatcher endpointdispatcher) { }      public void validate(serviceendpoint serviceendpoint) { } } 

the message inspector both adds cookies when request sent server in beforesendrequest method , retrieves cookies should updated in afterreceivereply method. note correlationstate returned beforesendrequest used retrieve uri in afterreceivereply:

public class cookiemessageinspector : iclientmessageinspector {     private cookiecontainer cookiecont;      public cookiemessageinspector(cookiecontainer cookiecont)     {         this.cookiecont = cookiecont;     }      public void afterreceivereply(ref system.servicemodel.channels.message reply,         object correlationstate)      {         object obj;         if (reply.properties.trygetvalue(httpresponsemessageproperty.name, out obj))         {             httpresponsemessageproperty httpresponsemsg = obj httpresponsemessageproperty;             if (!string.isnullorempty(httpresponsemsg.headers["set-cookie"]))             {                 cookiecont.setcookies((uri)correlationstate, httpresponsemsg.headers["set-cookie"]);             }         }     }      public object beforesendrequest(ref system.servicemodel.channels.message request,         system.servicemodel.iclientchannel channel)     {         object obj;         if (request.properties.trygetvalue(httprequestmessageproperty.name, out obj))         {             httprequestmessageproperty httprequestmsg = obj httprequestmessageproperty;             setrequestcookies(channel, httprequestmsg);         }         else         {             var httprequestmsg = new httprequestmessageproperty();             setrequestcookies(channel, httprequestmsg);             request.properties.add(httprequestmessageproperty.name, httprequestmsg);         }          return channel.remoteaddress.uri;     }      private void setrequestcookies(system.servicemodel.iclientchannel channel, httprequestmessageproperty httprequestmessage)     {         httprequestmessage.headers["cookie"] = cookiecont.getcookieheader(channel.remoteaddress.uri);     } } 

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -