Setting up a C# worker
Prerequisites
See Requirements.
Using the C# blank project
You can use the C# blank project as a starting point for a C# worker; it provides a minimal example of project structure and build configuration.
Setting up a worker manually
To add a C# worker to your project:
- Create a directory under
workers/
of your SpatialOS project (for example,workers/csharp
). Create a worker configuration file file (for example,
workers/csharp/spatialos.MyCsharpWorker.worker.json
) with the following contents:{ "build": { "tasks_filename": "spatialos.csharp.build.json", "generated_build_scripts_type": "csharp_msbuild" }, "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
in the same directory alongside which it will automatically generate the following build scripts and files: GeneratedCode.csproj
CsharpWorker.sln
BuildTargets.targets
CsharpWorker.targets
spatialos.csharp.build.json
spatialos_worker_packages.json
This
CsharpWorker.csproj
file is controlled by you, and should provide you with enough flexibility without the need to turn off the automatically generated build scripts. You can choose any name for the assembly - this will then be the name of the worker and the executable.We provide a seed csproj file which you should start with.
The
csproj
is set to build an executable, so you will need to provide astatic 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!"); } }
- The default build setup expects a
Build your worker using
spatial worker build
orspatial worker build <worker type>
.Add the worker to your SpatialOS application.
Assemblies produced by
spatial worker build
contain anexe
file with the assembly name set in yourcsproj
file. For example, the seed csproj produces assemblies containingCsharpWorkerName.exe
.You should launch them using
mono CsharpWorkerName.exe
. Configure managed workers to be launched this way in the launch config.
For more about the build setup, see the Building page.