Creating a Custom Instrument

We create a virtual instrument with a single user callable activity, HelloWorld, that sends an email to a prespecified address.

This instrument is written in C#, but any Common Language Infrastructure language could be used (https://en.wikipedia.org/wiki/List_of_CLI_languages).

Preliminaries

using System;
using System.Net;
using System.Net.Mail;

BaseInstrumentClass

All virtual instrument interfaces have to be members of BaseInstrumentClass:

namespace Clarity {
  public class HelloInstrument : BaseInstrumentClass
  {

SMTP Client

Our instrument will need to connect to an SMTP server to send email, here we’ll use gmail:

var smtp = new SmtpClient
{
  Host = "smtp.gmail.com",
  Port = 587,
  EnableSsl = true,
  DeliveryMethod = SmtpDeliveryMethod.Network,
  UseDefaultCredentials = false,
  Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};

Mail Details

var fromAddress = new MailAddress("from@gmail.com", "From Name");
var toAddress = new MailAddress("to@example.com", "To Name");
const string fromPassword = "fromPassword";
const string subject = "Saying Hello       ";
const string body = "Hello World!";

Sending a Message

Now we create a method to define the activity of sending the email.

It needs to be tagged with the UserCallableMethod attribute, so that protocols can use it:

[UserCallableMethod()]
public void HelloWorld()
using
(
  var message = new MailMessage(fromAddress, toAddress)
    {
      Subject = subject,
      Body = body
    }
)
{
  smtp.Send(message);
}

Finally, we close some brackets:

  }
}

Conclusion

To finish we just compile the class:

csc /target:library HelloInstrument.cs

or

mcs -target:library HelloInstrument.cs

And place the resulting executable, HelloInstrument.dll, into the directory containing Clarity’s instrument manager.

The activity HelloWorld can then be included in protocols.