0 votes
1 view
in RPA by (5.4k points)

 i am trying to execute custom asyncCodeActivity in UIPath. Added the package, passing all data, however UIPath just hangs when it reaches custom activity and does not throw any exceptions/or stops. I tried to create Class Library using CodeActivity and AsyncCodeActivity - my activity should make several APICalls but I get result it just stops when it reaches my custom activity and does not go to the next one. Is there any example how to create async custom activity for UIPath? My class library worked ok when I tried to test it outside of UIpath. Will appreciate any help.
My class library using CodeActivity:

public class AddInvoice : CodeActivity

 {       

    [Category("Input")]

    [RequiredArgument]

    public InArgument<string> PickupZip { get; set; }         

    [Category("Output")]

    [RequiredArgument]

    public OutArgument<String> Output { get; set; }

    public async Task<string> ApiTest(CodeActivityContext context)

    {            

        try

        { 

        var origin = await GoogleAPIWrapper.GetAddressByZip(PickupZip.Get(context));          

        string PickupAddress;

        string DeliveryAddress;

        var inv = new IList();

        if (origin.StatusId >= 0)

        {                

            invoice.PickupCity = origin.Locality;                

            invoice.PickupState = origin.AdminLevel1; 

        }

        else

        {               

            invoice.PickupCity = null;               

            invoice.PickupState = null;   

        }             

        var tkn = token.Get(context);

        var client = new HttpClient();

        HttpClientHandler handler = new HttpClientHandler();

        client = new HttpClient(handler, false);

        client.BaseAddress = new Uri("http://test.test.com/");

        client.DefaultRequestHeaders.Add("Authorization", "Bearer " + tkn);

        StringContent content = new StringContent(JsonConvert.SerializeObject(inv), Encoding.UTF8, "application/json");

        var response = await client.PostAsync("api/insert/", content);

        var resultContent = response.StatusCode;

            Output.Set(context, resultContent.ToString());

    }

        catch (Exception e)

        {                

            Output.Set(context, e.ToString());

        }

        return "ok";

}

    protected override void Execute(CodeActivityContext context)

    {

        try

        {

            string result = ApiTest(context).GetAwaiter().GetResult();

        }

        catch (Exception e)

        {

            Output.Set(context, e.ToString());

        }

    }      

    public class IList

    {            

        public string PickupState { get; set; }            

        public string PickupCity { get; set; }           

    }

}  

1 Answer

0 votes
by (9.5k points)

Classes that derive from CodeActivity are synchronous by default. Since UiPath is based on Windows Workflow, deriving from an asyncode class should work.

You didn't ask explicitly for it, but since you're essentially calling a web service, have a look at the Web Activities package, the http request in particular. This also comes with JSON deserialization.

Related questions

0 votes
0 answers
0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...