8 Replies - 277 Views - Last Post: 30 January 2020 - 01:18 PM Rate Topic: -----

#1 fearfulsc2   User is offline

  • D.I.C Regular

Reputation: 18
  • View blog
  • Posts: 301
  • Joined: 25-May 16

C# Synchronous function calling async function

Posted 30 January 2020 - 09:14 AM

Hi everyone, hopefully this is a quick topic.

I am trying to call an async method in a synchronous way and I believe I have achieved that but I fear that I may eventually run into a deadlock.

The other thing I noticed and have a concern for is memory usage/consumption

When I was doing debugging, I noticed that every time I called the method, there was an object that still had values from the previous call. So what I did was instantiate it as a new object on each call (Will these objects ever get collected during GC?)

// object created in global instance of the class 
public class CustomAuthorize : AuthorizationFilterAttribute
{
 public CustomerAuthorize()
 {
   // some stuff here
 }

private IvrEventLog EventLog; // object declared outside of methods
 public override void OnAuthorization(HttpActionContext actionContext)
 {
 // stuff happens here
 EventLog = new IvrEventLog(); // this is instantiated every time this is called. Will this take up all the memory?
 
 var task = Task.Run(async () => { await _ivrEventLogBo.InsertEventLog(IvrEventLog); });
 task.Wait(); // WILL THIS CAUSE A DEADLOCK?
 }
}



Is there a way to ensure that the memory is properly cleaned up and to ensure that I don't have a potential deadlock?

Is This A Good Question/Topic? 0
  • +

Replies To: C# Synchronous function calling async function

#2 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 3070
  • View blog
  • Posts: 11,777
  • Joined: 03-December 12

Re: C# Synchronous function calling async function

Posted 30 January 2020 - 10:56 AM

Why are you calling asynchronous methods in an synchronous way? That completely defeats the purpose.
Was This Post Helpful? 0
  • +
  • -

#3 fearfulsc2   User is offline

  • D.I.C Regular

Reputation: 18
  • View blog
  • Posts: 301
  • Joined: 25-May 16

Re: C# Synchronous function calling async function

Posted 30 January 2020 - 11:02 AM

The method above is a custom authorize attribute and authenticates/authorizes requests to our API.

Our leadership wanted to add logging to the database through here to get more information/data on what is being passed in and so forth.

This is a synchronous function while the logging class has only asynchronous functionality. So in order for me to not mess with the existing functionality, I figured it was best to call it in a synchronous manner.
Was This Post Helpful? 0
  • +
  • -

#4 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 3070
  • View blog
  • Posts: 11,777
  • Joined: 03-December 12

Re: C# Synchronous function calling async function

Posted 30 January 2020 - 11:36 AM

Since your naming conventions don't follow common practice I have questions.
private IvrEventLog EventLog; 

Is that an interface or just named with a leading "I"?

The instantiation should be move to the constructor and not be in the method itself.

This should suffice otherwise,
AsyncContext.Run(_ivrEventLogBo.InsertEventLog(IvrEventLog)).ConfigureAwait(false);


But with the code you trimmed out there are a few flags being thrown that are likely irrelevant.
Was This Post Helpful? 0
  • +
  • -

#5 fearfulsc2   User is offline

  • D.I.C Regular

Reputation: 18
  • View blog
  • Posts: 301
  • Joined: 25-May 16

Re: C# Synchronous function calling async function

Posted 30 January 2020 - 11:44 AM

The first line is a class that begins with an I as the I is part of the acronym for the object.

I originally put it in the constructor but after subsequent calls, the object had data from the previous calls.

So if an error event was logged and the next call is successful, you will still see the error message in the logs in the db.

But if I put it in the method as I have in the post above, I am concerned about memory management and if that new object being instantiated every time will continue to suck up resources until the app pool is recycled or something else happens.
Was This Post Helpful? 0
  • +
  • -

#6 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5524
  • View blog
  • Posts: 14,526
  • Joined: 18-April 07

Re: C# Synchronous function calling async function

Posted 30 January 2020 - 11:48 AM

Well typically async methods lead to a pattern where all callers up the chain also have to be async. However, there are valid cases where you have an simple synchronous method that needs to run methods elsewhere asynchronously. All it means is that you are not waiting around for the answer to come back.

One post to check out is here.

https://stackoverflo...ous-method-in-c

Pay attention to solutions A & B of Stephen's answer. All solutions come with a risk, like a possible deadlock, but this answer goes into great depth to show the various types of solutions to your question. You can also find a ton of other articles just by doing a search for "calling async method from sync" on Google.

Edit: Another great post is from Chris Pratt's blog... https://cpratt.co/async-tips-tricks/

This post has been edited by Martyr2: 30 January 2020 - 11:50 AM

Was This Post Helpful? 0
  • +
  • -

#7 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 3070
  • View blog
  • Posts: 11,777
  • Joined: 03-December 12

Re: C# Synchronous function calling async function

Posted 30 January 2020 - 12:08 PM

I'm trying to understand why the class would be holding on to anything unless the code is holding a reference. It isn't a god class is it?

https://dotnetfiddle.net/bIT9i3
Was This Post Helpful? 0
  • +
  • -

#8 fearfulsc2   User is offline

  • D.I.C Regular

Reputation: 18
  • View blog
  • Posts: 301
  • Joined: 25-May 16

Re: C# Synchronous function calling async function

Posted 30 January 2020 - 01:16 PM

It's an attribute to an API controller.

For whatever reason, when I did this

private IvrEventLog _eventLog;

public GuidAuthorize()
{
 _eventLog = new IvrEventLog();
}

public override void OnAuthorization(HttpActionContext actionContext)
{
 // stuff works

 // log the stuff
 if(authorized)
 {
  _eventLog.SOMETHING = NEWVALUE; // if this is called a second time because it failed the first time, the error message below will still exist in this second call.
 }

 else
 {
  _eventLog.Error = ERRORMESSAGE;
 }
 
}



So for example, if I called the API and the user gave us invalid information, the IvrEventLog object will have its Error property given a value.

If they call the endpoint a second time and it is successful, that Error property still had a value from the previous call.

I resolved this by taking out the instantiation from the constructor and put it in the OnAuthorization function, but my concern was then that if the API is called hundreds of times, would the application continue to create these objects without ever having disposed of any of them?
Was This Post Helpful? 0
  • +
  • -

#9 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 3070
  • View blog
  • Posts: 11,777
  • Joined: 03-December 12

Re: C# Synchronous function calling async function

Posted 30 January 2020 - 01:18 PM

You can always flush it and may be able to call dispose to clean up.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1