# How to execute asynchronous code from a custom code activity?

**URL:** <https://forum.uipath.com/t/how-to-execute-asynchronous-code-from-a-custom-code-activity/144757>\
**Category:** Help\
**Tags:** studio, activities\
**Created:** [August 24, 2019, 9:03pm UTC](https://forum.uipath.com/t/how-to-execute-asynchronous-code-from-a-custom-code-activity/144757 "2019-08-24T21:03:01Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bena](https://avatars.discourse-cdn.com/v4/letter/b/c57346/32.png) [@Bena](https://forum.uipath.com/u/Bena)\
**Post date:** [August 24, 2019, 9:03pm UTC](https://forum.uipath.com/t/how-to-execute-asynchronous-code-from-a-custom-code-activity/144757/1 "2019-08-24T21:03:01Z")

</div>

I created a custom CodeActivity in C# that I am using in a sequence within a state machine in UiPath. The custom CodeActivity involves calling an API through an async method.

I cannot get it to work.

I’ve tried using AsyncCodeActivity using this method: [https://stackoverflow.com/a/16964323/989148](https://stackoverflow.com/a/16964323/989148)

I’ve also tried:

- task.GetAwaiter().GetResult();
- task.Result;
- task.ConfigureAwait(false).GetAwaiter().GetResult()

Nothing works.

In some cases the robot just never ends. In other cases the robot fails with an error message: “An Activity can only get the location of arguments which it owns.”. This happens when I try to access the output value of the activity.

How can I do an async method call within a CodeActivity in UiPath?

---

<div class="post-metadata">

**Author:** ![chenderson](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/chenderson/32/90010_2.png) [@chenderson](https://forum.uipath.com/u/chenderson)\
**Post date:** [August 24, 2019, 10:58pm UTC](https://forum.uipath.com/t/how-to-execute-asynchronous-code-from-a-custom-code-activity/144757/2 "2019-08-24T22:58:04Z")

</div>

Good evening Bena,

Classes that derive from CodeActivity are synchronous by default. Since UiPath is based on Windows Workflow, deriving from an [AsyncCodeActivity](https://docs.microsoft.com/en-us/dotnet/api/system.activities.asynccodeactivity?view=netframework-4.7.2) class should work.

You’ll need to inherit from AsyncCodeActivity\<T\> (\<T\> being optional) and implement both BeginExecute and EndExecute. But besides that, they don’t differ much, as the framework takes good care of managing the async part.

---

<div class="post-metadata">

**Author:** ![HareeshMR](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/hareeshmr/32/181825_2.png) [@HareeshMR](https://forum.uipath.com/u/HareeshMR)\
**Post date:** [August 25, 2019, 4:57am UTC](https://forum.uipath.com/t/how-to-execute-asynchronous-code-from-a-custom-code-activity/144757/3 "2019-08-25T04:57:30Z")

</div>

As we have the Inherited class of Execute within the code activity, you need to use a await modifier while assigning the result of the function return @Bena…

I hope you are missing this particular thing

---

<div class="post-metadata">

**Author:** ![Bena](https://avatars.discourse-cdn.com/v4/letter/b/c57346/32.png) [@Bena](https://forum.uipath.com/u/Bena)\
**Post date:** [August 25, 2019, 9:11pm UTC](https://forum.uipath.com/t/how-to-execute-asynchronous-code-from-a-custom-code-activity/144757/4 "2019-08-25T21:11:46Z")

</div>

Here is an example of a simple test AsyncCodeActivity that waits for 1 minute and then sets an output variable.

This example uses the ApmAsyncFactory from the Nito.AsyncEx.Interop library.

I put it in my flow in UiPath and direct the output string to a variable. After the execution of the custom activity I try to popup a message box with the value of the variable.

When executing this the robot waits for 1 minute (the delay I put in my custom activity) and then I get an error.

 ![billede](https://global.discourse-cdn.com/uipath/original/3X/1/6/16c64adc34913694c6d7452114d2e57206098209.png)

---

<div class="post-metadata">

**Author:** ![fandre](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/fandre/32/182233_2.png) [@fandre](https://forum.uipath.com/u/fandre)\
**Post date:** [December 8, 2019, 9:14pm UTC](https://forum.uipath.com/t/how-to-execute-asynchronous-code-from-a-custom-code-activity/144757/6 "2019-12-08T21:14:46Z")

</div>

I am following your sample to build one of my async code but I cannot find out how to get/use AsyncTaskCodeActivity. Could you explain where it could be found in VisualStudio?

---

<div class="post-metadata">

**Author:** ![einarbr](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/einarbr/32/182258_2.png) [@einarbr](https://forum.uipath.com/u/einarbr)\
**Post date:** [November 19, 2021, 9:02am UTC](https://forum.uipath.com/t/how-to-execute-asynchronous-code-from-a-custom-code-activity/144757/8 "2021-11-19T09:02:13Z")

</div>

Hi, I just wanted to answer this if someone was looking for a solution.

For a basic http request to an API you can make the execution synchronous.  
I have an activity which fetches a list, loops through it and calls other endpoints with data from the loop. All http requests use .Result instead of await and then the activity runs inside UiPath.  
This probably isn’t the “correct way” to get this result but it gets the job done until I find a better way.

 ![image](https://global.discourse-cdn.com/uipath/original/4X/6/d/e/6de874304ec22c3e3720b16e823369c12d486ebe.png)  
And then you can use the activity inside UiPath and the robot will wait for the execution to finish and then continue with the workflow.  
I haven’t been successful creating activities with AsyncCodeActivity but this works well with my usecase which is just fetching data and returning it in a formatted way.  
 ![image](https://global.discourse-cdn.com/uipath/original/4X/2/c/b/2cb9aaf0c8e83bf33cc46ba2b938a302800f3dad.png)

Hope this helps someone.

---

<div class="post-metadata">

**Author:** ![muthukumar.murugesan](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/muthukumar.murugesan/32/465033_2.png) [@muthukumar.murugesan](https://forum.uipath.com/u/muthukumar.murugesan)\
**Post date:** [April 13, 2022, 11:15am UTC](https://forum.uipath.com/t/how-to-execute-asynchronous-code-from-a-custom-code-activity/144757/9 "2022-04-13T11:15:53Z")

</div>

You can refer this link… [Using The Activity Creator](https://docs.uipath.com/developer/docs/using-activity-creator#1-add-the-uipath-activity-creator-extension-to-visual-studio). Its already handled the Asyc/Sync operations. Its working for me.

Hope this helps someone.

---

<div class="post-metadata">

**Author:** ![Deval\_Chawda](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/deval_chawda/32/180486_2.png) [@Deval\_Chawda](https://forum.uipath.com/u/Deval_Chawda)\
**Post date:** [January 13, 2023, 7:55am UTC](https://forum.uipath.com/t/how-to-execute-asynchronous-code-from-a-custom-code-activity/144757/10 "2023-01-13T07:55:08Z")

</div>

How can we debug this code? any idea /

---

<div class="post-metadata">

**Author:** ![savasozer](https://avatars.discourse-cdn.com/v4/letter/s/439d5e/32.png) [@savasozer](https://forum.uipath.com/u/savasozer)\
**Post date:** [July 26, 2023, 2:36pm UTC](https://forum.uipath.com/t/how-to-execute-asynchronous-code-from-a-custom-code-activity/144757/11 "2023-07-26T14:36:42Z")

</div>

No. That document is not correct. I tried every step carefully million times.

- Visual Studio does not publish a nuget package using that default publish profile.
- If I build a .nupkg using project settings and load as a package into UiPath Studio, it does not show my activity in activity tree.
