Setting up a C# worker

Using the C# blank project

The C# blank project provides a
minimal example of project structure and build configuration for C# workers. It is the best way to get something
working quickly and easily. You shouldn’t need to do any additional setting up if you choose to use it.

Manual setup

To add a C# worker to your project, create a directory under workers/ of your SpatialOS project, for example
workers/csharp and create a worker configuration file file,
e.g. workers/csharp/spatialos.MyCsharpWorker.worker.json, with the following contents:

The default build setup expects a CsharpWorker.csproj to be in the same directory. This file is
controlled by you, and should provide you with enough flexibility without the need to turn off
automatically generated build scripts. We provide a seed csproj file which you should start with.

It includes BuildTargets.targets and CsharpWorker.targets, which set up the expected platforms
and output for you. You can choose any name for the assembly - this will then be the name
of the worker and the executable. The csproj is set to build an executable, so
you will need to provide a
static int Main(string[] args) somewhere in the source code in order for the build to succeed.

For example, you can create a src/Test.cs within the worker directory with the following contents:

You should now be able to build your worker using
spatial worker build or
spatial worker build <worker type>.

You will now need to add the worker to your SpatialOS application. Assemblies produced by
spatial worker build contain an exe file with the assembly name set in your csproj file. For example, the seed project would produce assemblies containing
CsharpWorkerName.exe. You should launch them using mono CsharpWorkerName.exe. Managed workers,
if any, should be configured to be launched that way.

Note: If you receive a DllNotFoundException, make sure that you are running the 64-bit version of mono.
On macOS, mono can default to 32-bit, so please either use the --arch=64 flag by passing it as
the first argument, e.g. mono --arch=64 CsharpWorkerName.exe, or run mono64 directly, to ensure that
SpatialOS SDK native libraries are loaded correctly. Please note that this flag is only valid for macOS versions of Mono.