Starting SignalR server from inside my game

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By jahu00

I had this brilliant idea to use SignalR in my multiplayer co-op game. I had a prototype with no networking where a dummy client would return data without sending any requests to a server and I figured, my next step would be to try implementing the server (as it would help me figure out how the client should actually look like, because of server imposed constraints).

After partially implementing my server as a WebApplication and successfully testing it in a console app, I moved everything back to the godot project and that’s when thing fell apart.

I can make an instance of my SignalR client in the game (it comes from a nuget package), but I cannot make an instance of my SignalR server (it’s part of .NET Core).

Initially there was no error, _Ready, would simply not run in my main node (like at all, but _Process would run fine). After a little tinkering, I did manage to get a more concrete error, by calling the offending code from “Immediate Window” while debugging. I’ve got this error:

error CS0012: The type ‘WebApplication’ is defined in an assembly that is not referenced. You must add a reference to assembly ‘Microsoft.AspNetCore, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60’.

There is no obvious way to add a reference to Microsoft.AspNetCore assembly in my Godot project. It’s one of the framework references in a project and is dictated by the sdk attribute in the csproj file. I did try adding it by adding another section inside the csproj file:

	<FrameworkReference Include="Microsoft.AspNetCore.App" />
  </ItemGroup>```

But I only managed to deceive Visual Studio into believing that the assembly is referenced and I still get the same error.

**Is there is some way to start my WebApplication SignalR server from my Godot game or did I just lose a lot of time?**

---

For WPF and WinFroms there are special XML elements that can be added to the csproj file that make it possible to use them in a class library or ASP app, but I don't see anything like that for AspNetCore.

---

I might still be able to make this work somehow, by using an ancient version of AspNetCore that is still available on nuget, but that will probably introduce tons of extra references (MS moved everything into .NET in the newer versions)
:bust_in_silhouette: Reply From: Phosphor

You could try grabbing the dll from the test project, all the libraries will be in

$PROJECTFOLDER\bin\Debug\net6.0 (windows, linux would be / instead of )

Copy it to the Godot project folder, and then reference it in your C# project file like:

  <ItemGroup>
    <Reference Include="Microsoft.AspNetCore.App">
      <HintPath>$PATH/TO/AspNetCore.App.dll</HintPath>
    </Reference>
  </ItemGroup>

Another alternative might be to just use two c# projects. Remove the SignalIR server portion from the godot project. Create a new folder to hold both projects.

Move the Godot solution file to the new folder. Then copy the Godot project to the new folder. Edit the solution file so the path is correct for the C# project in the Godot project.

Open the Godot project in Godot, you’ll have to import it because it has moved. There will be no Build button anymore so fix that first. Go into project settings, enable Advanced, scroll down on the left till you get to DotNet, under project set the solution folder to be one step up:

res://..

Save. When you restart Godot the Build button will be back.

In Visual Studio open the solution file, you should now be able to add a class library to the solution (this is where your IR Server code goes).

Then right click the Godot project in VS, Choose Add->Reference and in the dialog select the library project that has the ir server code. Now you should be able to use the code from the IR Server in the godot project and manage it’s dependencies seperate from the Godot’s projects.

I already had multiple projects in my solution with SignalR server being in one of them and linked as a project reference. On the other hand, I wasn’t aware that I can set the VS solution directory in my Godot project (this would solve some issues that I was having with the solution).

There doesn’t appear to be a AspNetCore.App.dll file in any of the bin folders (It might still exist in some .NET SDK folder).

Another option would to keep the server as a separate executable (console app) and just run that from the Godot project (while not ideal, it should work).

Or I could rewrite my network code using Godot’s built-in functionality (rpc etc.).

To be honest, to avoid wasting time on bloated prototypes only to learn they don’t work, I’ve since moved to writing tiny prototypes to only test one thing at a time, so they fail early if they fail.

jahu00 | 2023-03-13 18:21

I tried a few more things, but still no luck. First I tried deploying my server, because sometimes, during publish additional dlls are included, but there were none.

I also tried to copy all the dlls from the Microsoft.AspNetCore.App directory in the SDK to my build folder, but that didn’t help either.

I think the problem I’m having is related to Godot project having Godot.NET.Sdk/4.0.0-beta.17 set as sdk in csproj file.

After spending some time researching Godot’s built-in network functionality, I think my best bet would be to use RPC in a similar way to how I intended to use SignalR (it might even abstract some of the non-thread-safe async stuff away that I would have to deal with when using SignalR). Or have SignlalR server in another exe file and start that.

jahu00 | 2023-03-16 21:47