How to invoke async activity in UIPath

随声附和 提交于 2021-02-11 13:30:03

问题


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:


Classes that derive from CodeActivity are synchronous by default. Since UiPath is based on Windows Workflow, deriving from an AsyncCodeActivity 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. You can find more information about web service integration here, for example (disclaimer: I am the author).



来源:https://stackoverflow.com/questions/52446016/how-to-invoke-async-activity-in-uipath

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!