C# Debug.WriteLine not working in server code, works in client

Hi everyone, I’m trying to create a simple client/server script but when I try doing Debug.Writeline on the Server.net.dll file it doesn’t show up in the TXAdmin panel. Any ideas why?

Server.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CitizenFX.Core;
using CitizenFX.Core.Native;

namespace CheckIdentifier.Server
{
    public class Server : BaseScript
    {
        public Server()
        {
            TriggerEvent("chatMessage", "TEXT MESSAGE");
            EventHandlers["onResouceStart"] += new Action<string>(OnResourceStart);
        }
        private void OnResourceStart(string resource)
        {
            if (string.IsNullOrEmpty(resource) || resource != API.GetCurrentResourceName())
            {
                return;
            }
            Debug.WriteLine($"Started Resource: {resource}");
        }
    }
}

Client.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CitizenFX.Core;
using CitizenFX.Core.Native;

namespace CheckIdentifier
{
    public class Client : BaseScript
    {
        public Client()
        {
            EventHandlers["onClientResourceStart"] += new Action<string>(OnClientResourceStart);
        } 

        private void OnClientResourceStart(string resource)
        {
            if (resource == null || resource != API.GetCurrentResourceName()) {
                return;
            }
            Debug.WriteLine($"Resource Started: {resource}");
        }
    }
}

To be honest I don’t really see anything wrong with the code. The only thing I can think of it’s something to do with txAdmin and the only thing I would change is the way you are registering the event. Here is the code I used

using System;
using CitizenFX.Core;
using static CitizenFX.Core.Native.API;

namespace Red.Testing.Server
{
    public class ServerMain : BaseScript
    {
        [EventHandler("onResourceStart")]
        private void OnResourceStart(string resource)
        {
            if (resource != GetCurrentResourceName())
            {
                return;
            }

            Debug.WriteLine($"Started Resource: {resource}");
        }
    }

I tried your solution but still the same thing happens… Maybe it’s how I’m building it? I’m not sure if that could be causing it.

This is what shows up in the console:

Creating script environments for CheckIdentifier
Symbol file LoadedFromMemory is not a mono symbol file
[ resources] Started resource CheckIdentifier

does it even start? (without the Debug)

I think it starts, this is what I get in the console when I run restart:

[ resources] Stopping resource CheckIdentifier
[ c-scripting-core] Creating script environments for CheckIdentifier
Symbol file LoadedFromMemory is not a mono symbol file
[ resources] Started resource CheckIdentifier
cfx> [ citizen-server-impl] server thread hitch warning: timer interval of 206 milliseconds

From the looks of it no it doesn’t. Try reinstalling the CitizenFX.Core.Server in the NuGET package installer.

edit:
It does run but still if you want to make sure that the script works as intended reinstall the package could just be Visual Studio or the IDE you’re using just being the IDE.

image
It’s showing as installed…

Interesting… Again it could just be txAdmin being txAdmin and not working, I don’t use txAdmin anymore for testing but when I make FiveM scripts I use this:
https://cookbook.fivem.net/2020/02/24/try-the-new-citizenfx-c-templates-for-fivem/

I really don’t see anything in your code that is wrong and it’s working just fine on my end.

1 Like

LOL what do you mean?!
Everything in txAdmin works, there is literally no known bugs except very small UI stuff and a couple error messages in RedM that don’t impact in anything.
Also that’s not a new thing, txAdmin has always being extremely reliable.

Instead of blaming txAdmin, how about you guys blame FXServer and who wrote it?
If you run a SERVER command, through a client (like chat or f8 console), that information will not be displayed in the txAdmin Live Console - and I have expressed that I disagree of this deliberate decision to the person that took it regarding its particular implementation.

Also, let me add that txAdmin does not do any filtering in the live console output except some terminal special characters (CSI, ANSI colors). So if the issue is you not seeing something in the live console, txAdmin is definitely not the one to blame for it.
Reading the source code to confirm it is not that hard, but I guess it’s harder than spreading misinformation on the forums.

3 Likes

Im indifferent about TXAdmin since it’s what I’ve always used, but I’m not sure why it’s not showing server output that should run on start.

How are you starting/restarting the resource?
Like, from which interface, button, terminal, etc

My apologies, you are correct I shouldn’t blame it. It seems to run it’s just nothing inside the DLL is being ran. Do you have any ideas Tabarra? What I’m starting to think it’s a project compilation issue. :thinking:

I type refresh and then start on the txAdmin console. I also tried on the FXServer cmd

Unfortunately I’m clueless of the mono ScRT and can’t help with that.
Just to make sure, go to live console and type “ensure ” and check if any error pops up.

This is what I get:

Make sure that the CFX Nuget packages are not being copied to local.

This fixed it… Any idea why this would make it not run? You guys rock btw @ChristopherM @tabarra @RedMeansWar

Classic GIF - Classic - Discover & Share GIFs

I won’t pretend to understand the finer intricacies of C# & CFX, but from my understanding, we need to have the package files in the SLN/CS Project so we have the required references to actually write the code and compile it (duh). Once it is compiled, however, FXServer uses its own local copy of CitizenFX.Core.Server.dll, located @ citizen\clr2\lib\mono\4.5; by including our own copy (i.e. the Nuget copy), we cause a conflict, and thus a silent failure. Why this happens instead of one just taking priority over the other, I do not know. Someone more informed than I may be able to explain.

But tl;dr, CFX Nuget packages, make sure ‘Copy Local’ is always false.


Some other random fun C# FiveM tips that you didn’t ask for:

  • Don’t create new LUA files in VS (editing existing ones is fine), it adds invisible characters that break the scripts upon loading in FiveM, and can be very difficult to track down.
  • If you need JSON, don’t use the Nuget version of Newtonsoft, use this version.
    • Again, I don’t fully understand why, but it’s something to do with the linked version being an older “Portable” version, vs. the Nuget version being too new, or something?
  • If Debug.Writeline is giving you grief, make sure you have not imported System.Diagnostics by accident.
  • A lot of Nuget packages won’t actually work due to FiveM sandboxing, so be prepared for that.
    • That said, some do work, so before you write them off, make sure you have included their .dll files in your resource folder, and included them in your Manifest under files.
  • C# has a resmon overhead. No it’s not a big deal, don’t let anyone tell you otherwise.
  • When using event handlers, keep in mind you can only send “simple” types; you can’t send structs for example. If you need to send “complex” data, I suggest JSON conversion on either end.
  • Shared Projects are your friend, use them if you can!
  • The Players object on the server can be indexed with an int or a string, HOWEVER, if you index with a string, it will attempt to match by player name. This is important because on the Player class on the server, the .Handle (aka the Server ID) returns a string. If for whatever reason you have stored a player’s handle and want to get their player object from Players later on, make sure to cast/parse it to an int first!!

Just some random stuff that I’ve found catches out beginners + other ramblings.

Happy travels.

2 Likes

Thanks for the pointers! I just ran into the issue with the Serialize with JSON. I used nuget to get version 12.0.2 but even when I try using your version it errors out with this:

Error invoking callback for event onClientResourceStart: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.TypeInitializationException: The type initializer for 'Newtonsoft.Json.Serialization.DefaultSerializationBinder' threw an exception. ---> System.MethodAccessException: Error verifying Newtonsoft.Json.Serialization.DefaultSerializationBinder:.ctor (): Method System.Runtime.Serialization.SerializationBinder:.ctor () is not accessible at 0x0001

Nvm… I fixed it by adding this to the .csproj file.