Async and Wait

  • Thread starter Thread starter MrSnert
  • Start date Start date
M

MrSnert

Guest
A little background. I did programming 20 years ago. I wrote DLLs which mediated the data from a classic ASP page into a database and returned data back to the user. So coding knowledge of new practices is self taught so could be wrong.

A year ago I came back to programming as my new job required it. The requirement was to create a web API that takes post requests containing XML documents, and posts them into a Sage system. The XML is deserialised and I take out what I need, create a new object, and post it in as a JSON request through Sage's web API. The details of the work done in the controller to deal with the XML and output the JSON aren't important. What's important is that under load my web API has trouble. I think it is probably my code is not written well as I've been somewhat self taught in this endeavour in learning how to perform post requests, and living in a multi-threaded world. I developed the API in .NET Core 2.1.

My controller function definition looks like this:

public string Post([FromBody] bDataEntity bData, [FromQuery] string s = "")



In this function I have this call:

ProcessBFile(APIURI + sageCompanyName + "/", bData).Wait();



From what I've been reading, this is bad practice because in the ProcessBFile function this is ultimately where it pulls the data from the bData XML and creates the JSON data that it will post into Sage, there are some external processes that get called. So towards the end of ProcessBFile, after the data is pushed into Sage, I call an external process to do some additional work. The call is done like this:


await RunPostReadinessAndPost(...);



It seems I am mixing .Wait() with await and from what I've been reading, that is not good. "Async all the way down" is what I've been reading. So, does that mean in the Post handler in the controller, I should change it to:

public async Task<string> Post([FromBody] bDataEntity bData, [FromQuery] string s = "")
{
.
.
.
await ProcessBFile(APIURI + sageCompanyName + "/", bData);





In the RunPostReadinessAndPost function, I start a new Process() as it launches an external exe file which I need to wait for to get the exit code as this forms part of my response to the client in the request/response cycle.

I think over the last year in doing this, although it works, it is probably not well written, but when you're on your own, up against the clock you search for answers that work (or seem to) and don't research whether it is the right way or best way to do the job. Any thoughts or input?

Another query is what happens when a second post request comes in before the other one has completed? If, during the part where it calls an external process. will it just create another running instances of that called executable?

Continue reading...
 
Back
Top