c# - ContinueWith loses the SynchronizationContext -


In the snippet below, SynchronizationContext is lost, and this also causes current culture < / code> and current uk agriculture . Comes from log () .

  Public async Tasks & lt; ActionResult & gt; Index () {log ("before GetAsync"); Wait for the new HTTP client (). Continue GetAsync ("http://www.example.com/"). (Request = & gt; {Log ("Continue"); Request.InstructiveStatusSod ();}, Task Continuation Options. Attachtipant); See Return (); } Fixed zero log (string message) {var ctx = System.Threading.SynchronizationContext.Current; System.Diagnostics.Debug.Print ("{0}; Thread: {1}, Reference: {2}, Culture: {3}, Native Language: {4}", Message, System Threading Thread.Centrethread.ManagedTride, Ctx! = Null? Ctx.GetType () .name: string.optim; system threading thread.contentrath.contentculture.Name, system threading thread.contentthread.correntcurrent.name); }   

This output is:

before GetAsync; Thread: 56, Reference: ASPNet Syntax ContacTech, Culture: NL, Uriculture: NL. Prior to GetAsync , the values ​​I have defined in culture and UI culture are in I Application_BeginRequest . Inside Continue , the context is missing, the culture is set to things provided by the browser, and the UI culture is set to some default.

What I think, everything should be happening aspNetSynchronizationContext automatically, what's wrong with my code?

To determine continuity on the reference thread, you can type task scheduler Need to be specified, which should be used when determining continuity.

  Public async Tasks & lt; ActionResult & gt; Index () {log ("before GetAsync"); Wait for the new HTTP client (). Continue GetAsync ("http://www.example.com/"). (Request = & gt; {Log ("Continue"); Request.Result.EnsureSuccessStatusCode ();}, Task Continuation Options. Attachepatent, Cancellation Token.No, Task Scheduler. From Content Synchronization Contex ()); See Return (); }   

However, you are waiting for , which currently continues on code> synchronizationcontent is. You should be able to do this:

  Public ASINC works & lt; Action & gt; Index () {log ("before GetAsync"); HttpResponseMessage Request = Waiting for new HTTP client () GetAsync ("http://www.example.com/"); // Below everything below is the 'continuation' on the reference log ("Continue"); Request.EnsureSuccessStatusCode (); See Return (); }    

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -