Skip to main content

Introduction to Using namespaces in code Elements

Namespaces in C#

R
Written by Riku Virtanen
Updated over a year ago

Using namespaces in code Elements

Namespaces in C# are a way to differentiate class names from each other. They work in the same way as namespaces in C++ or packages in Java. The idea is that you can use two classes with the same name in one code block if you define the namespace that the class belongs to. In addition, you are able to use classes in any code block that is in the same namespace without having to reference the namespace. For detailed information about namespaces in C# you can refer to C# documentation.

Namespaces available in Frends Processes

Since Frends Processes are just C# source code behind the scenes, which is executed in an Agent when the Process is running, these Processes already have some namespaces imported in the source code. A list of imported namespaces can be found from Frends documentation. You can use classes from these namespaces without referring to the namespace. For example, let's say that you want to read an XML document into the memory using the System.Xml.XmlDocument class. Looking at Frends documentation we can see that the namespace System.Xml is already available in Expressions. This means that we can create an object of class XmlDocument without referring to the namespace. Below is an example.

If the namespace would, for any reason, be unavailable then we would need to refer to the full name of the class. Below is an example.

Using Task classes in code Elements

When you add a Task to a Process, the Task's code will be part of the whole Process code. This also means that the namespace in which the Task code is will be added to the Process code, so the code is able to call the Task's class method. Henceforth it is possible to use classes in the Task's namespace in any code Element or Expression. For example, let's say that you want to add headers to a Frends.Web.RestRequest Task dynamically according to data that you are receiving in the Process. The example data could be something like this:

[{ "key": "Content-Type", "value": "application/json" }, { "key": "Authorization", "value": "Bearer fnwnflösangflsGANGBAG" }]

We can then use this data to generate headers for the RestRequest Task. Looking at the source code, we can see that the Task uses the Header class to pass headers to the request. We can create an array of objects of class Header and then we can pass that array to the request. Below is an example Process:

In this Process we create demo data using the example JSON above. Then we create an array of Header objects which we then use in the RestRequest Task. Let's look at each of the steps in detail so you are able to replicate this demo Process.

In the first step we create a JToken object from the demo data so we can easily use the JSON data in the Process.

Next, we create a List of Header objects so we can dynamically add objects to the list without specifying the size of the array. In this Expression you can see that we are using the namespace to define which Header class we are talking about.

Next we create a Foreach iteration to iterate the demo data.

In the iteration we create a new object of the Header class and add it to the list.

Lastly, we can pass the headers to the RestRequest Task. Since the Task takes headers as an array of Header objects, we need to transfer the list of Headers into an array. This can easily be done using the List class's ToArray() method. Also note that the input type of the headers Field has to be changed from Array to Expression. For the Options tab of the RestRequest Task we will be using the default options.

Now we have created a dynamic handling of request headers. You can use this same approach in any Process in which you are using a Task since the Task needs to be used in the Process to make the namespace available in code Elements or Expressions.

Did this answer your question?