c# - My override of Async signature of GetWebResponse throws a runtime exception when setting property on response -


this related question asked couple weeks ago never got answer on. need override asynchronous signature of system.net.webclient.getwebresponse, set contenttype utf-8. (https://msdn.microsoft.com/en-us/library/c2xze5ez(v=vs.110).aspx)

i'm able fine synchronous call, asynchronous implementation throws runtime exception if try set same property - though can see property if examine response in debugger.

i'm hoping it's dumb i've overlooked.

in example below, i've created web reference (yes, old kind - no, can't change service reference right now) tempconvert service found here, , called oldschoolreference.tempconvert: http://www.w3schools.com/webservices/tempconvert.asmx

then inherited reference, follows:

using system; using system.collections.generic; using system.linq; using system.net; using system.text; using system.threading.tasks;  namespace windowsformsapplication1.oldschoolreference {     public partial class serviceproxy : tempconvert     {         protected override system.net.webresponse getwebresponse(system.net.webrequest request)         {             webresponse response = base.getwebresponse(request);             response.headers.set("content-type", "text/xml; charset=utf-8");    // works fine!             return response;         }          protected override system.net.webresponse getwebresponse(system.net.webrequest request, iasyncresult result)         {             httpwebresponse response = base.getwebresponse(request, result) httpwebresponse;             response.headers.set("content-type", "text/xml; charset=utf-8");              // note implementation system.net.httpwebresponse whereas sychronous 1 above system.net.webresponse             return response;         }     } } 

i'm exercising behavior simple one-button windows form calls service.

    private void button1_click(object sender, eventargs e)     {         oldschoolreference.serviceproxy service = new oldschoolreference.serviceproxy();          service.celsiustofahrenheitcompleted += callback;         service.celsiustofahrenheitasync("0"); // returns 32, naturally     }      private static void callback(object sender, oldschoolreference.celsiustofahrenheitcompletedeventargs e)     {         messagebox.show(e.result);     } 

but it's throwing exception :(

exception

as can see in screenshot, there's contenttype property on response, seems setter method property isn't implemented. yes, know it's set utf-8, that's not issue. need force utf-8 via override.

debugger view

lastly, of no consequence me, synchronous implementation i've included override work - need on async version.

does know how can set property on asynchronous response? obliged!

if being httpwebresponse have tried doing var response = base.getwebresponse(request, result) webresponseor var response = base.getwebresponse(request, result)


Comments

Popular posts from this blog

java - Andrioid studio start fail: Fatal error initializing 'null' -

android - Gradle sync Error:Configuration with name 'default' not found -

StringGrid issue in Delphi XE8 firemonkey mobile app -