Creating custom actives in C# .NET 6

Is there an example about how to create custom activities from code using C# on .NET 6? All the examples I have been able to find are from .NET Framework 4.6.

2 Likes

@Michael_Halpin Did you try this

1 Like

Hi!

You can create custom activities using c# in visual studio.

Have a view on this video

Regards,

Hi @Michael_Halpin

Please refer the below links

Hope this helps you

I tried watching these video but have not made much progress. Each of these examples uses the old .Net Framework and I am using .Net Core 6. I tried importing System.Activities and System.ComponentModel.Composition from .Net Framework 4.8 into my .Net Core 6 project and created the below class. After building the nuget package and importing into my UIPath project I can see the dependency to HelloUser listed under dependencies but none of the activities show up under the Activities tab in UI Path.

using System.Activities;
using System.ComponentModel;

namespace UIPathRPA
{
    public class HelloUser : CodeActivity
    {
      [Category("Input")]
      public InArgument<string> Name { get; set; }
      [Category("Output")]
      public OutArgument<string> Result { get; set; }
      protected override void Execute(CodeActivityContext context)
      {
         string input = Name.Get(context);
         string result = "Hello " + input + "!";
         Result.Set(context, result);
      }
    }
}
2 Likes

@Michael_Halpin

Hello Michael,
welcome in the UiPath Community.

Great question. :+1:

Your code looks good. To compile this code correctly to use it with the compatibility mode Windows in UiPath you must add a reference to System.Activities.dll in the UiPath Studio directory. You can find more information about that here.

Best regards
Stefan

2 Likes

@StefanSchnell that is exactly what I was missing. Thank you very much sir!

1 Like

@SefanSchnell one last thing. I am trying to test my code in C# so I can step through the library code. I am not familiar with calling activities in C# do you see what I am doing wrong to call the activity?

using System.Activities;
using System.Activities.Statements;

var seq = new Sequence();
seq.Activities.Add( new HelloUser() );
var resp = WorkflowInvoker.Invoke(seq, new Dictionary<string, object> {{“Name”, “Test message.”}});
Console.ReadLine();

The Sequence class is throwing a TypeInitializationException during initialization with message “Type constructor threw an exception.”

1 Like

I ended up being able to figure it out on my own. I had t reference UIPath.Workflow in NuGet. In my UnitTest console app I was able to test with the following code:

using System.Activities;

HelloUser user = new HelloUser() { Name = “Michael” };
var outputs = WorkflowInvoker.Invoke(user, new TimeSpan(0,0,0, 5));
var result = outputs[“Result”];
Console.ReadLine();

1 Like

@Michael_Halpin

Hello Michael,
interesting approach, I have never tried that on this way. After building the library I tried all in the context of an UiPath workflow. I will bookmark this and try it out later.
Best regards
Stefan

After doing testing I found this snippet works even better. Setting the value of Lists<> at instantiation was throwing exceptions for using List<> as a literal. Better to pass them in as arguments.

using System.Activities;

HelloUser user = new HelloUser();
var arguments = new Dictionary<String, Object>
{
{ “Name”, “Michael” }
};
var outputs = WorkflowInvoker.Invoke(user, arguments, new TimeSpan(0,0,0, 5));
var result = outputs[“Result”];
Console.ReadLine();

1 Like

@Michael_Halpin

Hello Michael,
thank you very much for sharing this excellent approach. :+1: With that it is possible to test activities for UiPath in another environment (even without UiPath).
Best regards
Stefan

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.