Get SpatialOS

Sites

Menu

Getting started with the C# worker SDK

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:

{
  "build": {
    "tasks_filename": "spatialos.csharp.build.json",
    "generated_build_scripts_type": "csharp"
  },
  "bridge": {
    "worker_attribute_set": {
      "attributes": [
        "new_worker_attribute"
      ]
    },
    "entity_interest": {
      "range_entity_interest": {
        "radius": 2
      }
    },
    "streaming_query": [],
    "component_delivery": {
      "default": "RELIABLE_ORDERED",
      "checkout_all_initially": true
    }
  }
}

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:

using System;
class Test
{
    static void Main()
    {
        Console.WriteLine("Hello World!");
    }
}

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.

You can read more about the build setup in the next section.

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.

Was this page helpful?

Thanks for letting us know!

Thanks for your feedback

Need more help? Ask on the forums