C# Client side issues

I hope someone can help me as I would love to code with C#. I thank you already upfront for taking the time to read this! :slight_smile:

I have been stuck for a week now trying to solve this mystery. However no one seems to know or have a resolution. I feel it is locally on my side, but not sure how many actually code RedM in c#.

This is my issue:

[   1214015] [b1491_GTAProce]             MainThrd/ ^1SCRIPT ERROR in reference call: System.BadImageFormatException: Error verifying framework.Client.ClientMain:GetClientCoords (): Cannot load method from token 0x0a000014 for call at 0x0006^7

I’m getting this error messages with a lot of natives that “interact” with the game client. In this example above I have the issue with the following code:

using System;
using System.Threading.Tasks;
using CitizenFX.Core;
using CitizenFX.Core.Native;

namespace framework.Client
{
    public class ClientMain : BaseScript
    {
        public ClientMain()
        {
			API.RegisterCommand("clientcoords", new Action(GetClientCoords), false);
		}

		public void GetClientCoords()
		{
			int pedId = API.PlayerPedId();
			Vector3 playerCoords = API.GetEntityCoords(pedId, true);
			Debug.WriteLine("" + playerCoords);
		}
    }
}

Now I have the same issue whenever I try CreatePed or GiveWeaponToPed for example, also the Function.Call(Hash) methods don’t work.
However on the client side natives like GetGameName and PlayerPedId do work. Server-side i have no issues at all. Even the GetEntitiyCoords works on server side.

I setup my FxServer with the latest stable and installed the RedM template via the TxAdmin. I build the C# project via the Citizen template.

I then made a Lua script and it works perfectly fine. So it is a C# issue that is client side. I check the version I’m running which is .NET Framework 4.5.2. I reinstalled multiple times, I checked with other people that have some knowledge, however nobody seems to be able to find the issue.
So I’m pretty clueless on where to look. I hope someone can point me in the right direction!

Thanks in advance!

.NET, or .NET Framework? The distinction is important in this context.

.NET Framework 4.5.2

I’ve corrected it in the topic

Where are you getting the CitizenFX.Client from, nuget or your local machine?

The template handles it:

dotnet new -i CitizenFX.Templates
mkdir MyResource
cd MyResource
dotnet new cfx-resource

However when i check Nuget:

Righto. I’ve never used the templates or RedM, but in my experience BadImageFormatException generally means that something is not being loaded correctly, which is odd since you’re just using the standard client library.

Can you check that your build output put does not contain the Client library? If you’re not sure what I mean, send a screenshot of your build output folder.

Thank you for the help! I appreciate it!

I’m not really sure what you mean. As the template comes with a build.cmd that you run.


And in my fxmanifest.lua:

file 'Client/bin/Release/**/publish/*.dll'

client_script 'Client/bin/Release/**/publish/*.net.dll'
server_script 'Server/bin/Release/**/publish/*.net.dll'

Like I said, I’ve never used RedM before, but if it’s anything like FiveM (which I would expect it to be), you don’t want that CitizenFX.Fore.Client.dll in there. In visual studio, find the library like so:

Then make the following change (make it false):

Delete the .dll from your publish folder, rebuild the project, and try your resource in server again.

I think it should be similar to FiveM. The framework is exactly the same.
I don’t have that reference folder in my project.

This is from the Fivem docs(Scripting in C# - Cfx.re Docs). How would you recommend to build a solution? Just build it manual, make 2 projects and get the client and server citizen packages from Nuget?

Can you not right-click on CitizenFX.Core.Client.dll to get a Properties option?

No I can’t…

Mmm, that’s a pain.

Look I’m hesitant to suggest that, as it goes against what is written in the docs, but that’s how I’ve always done it.

Maybe @AvarianKnight can shed some light? They’re more competent than I am with this stuff.

Well, I can at least give it a go. I’m pretty new to C# so I went for the recommended way as I also saw a recent YouTube tutorial doing it like this.

Would be great if someone else with more knowledge could shed some light on this.

I’ll update the results later.

Alright, I completed the test.

So I created a new Class Library (.NET Framework) with .NET Framework 4.5.2. I then downloaded the Nuget package for CitizenFX.Core.Client. I went to the properties and set Copy Local to false.

I put in the same code:

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

namespace test
{
    public class Class1 : BaseScript
	{
        public Class1() 
        {
			API.RegisterCommand("clientcoords", new Action(GetClientCoords), false);
			string getName = API.GetGameName();
			Debug.WriteLine("Game is: " + getName);
		}

		public void GetClientCoords()
		{
			int pedId = API.PlayerPedId();
			Vector3 playerCoords = API.GetEntityCoords(pedId, true);
			Debug.WriteLine("" + playerCoords);
		}
	}
}

I then created the manifest and build the solution. I put both files from the build into a new resource folder:

And restarted both my RedM client and server for good measures.
However the same result:

[     81672] [b1491_GTAProce]             MainThrd/ Started resource rdrtest
[     81672] [b1491_GTAProce]             MainThrd/ 
[     81688] [b1491_GTAProce]             MainThrd/ Creating script environments for rdrtest
[     82016] [b1491_GTAProce]             MainThrd/ Loaded Client.net, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null into ScriptDomain_2029102516
[     82047] [b1491_GTAProce]             MainThrd/ Game is: redm
[     82047] [b1491_GTAProce]             MainThrd/ Instantiated instance of script test.Class1.
[     82063] [b1491_GTAProce]             MainThrd/ OnConnectionProgress: Mounted rdrtest (1 of 1)
[    108391] [b1491_GTAProce]             MainThrd/ ^1SCRIPT ERROR in reference call: System.BadImageFormatException: Error verifying test.Class1:GetClientCoords (): Cannot load method from token 0x0a000017 for call at 0x0008^7
[    108391] [b1491_GTAProce]             MainThrd/ ^3> ExecuteCommand^7 (^5ExecuteCommand.lua^7:4)
[    108391] [b1491_GTAProce]             MainThrd/ ^3> callback^7 (^5@chat/cl_chat.lua^7:139)
1 Like

Another update:

I even went to the extend of installing GTA5 and FiveM. I applied the same route I took initially when setting up everything. Code runs perfectly fine with any issue.

So it seems to be a RedM client issue(??).

I hope someone would be able have some insight. A difference what I saw in the process is that FiveM needs to line up its client version with the server you connect to. In RedM this is not the case.

If you want to use C# for RedM you have to use mono_rt2.

You’re currently trying to import code that is meant for FiveM on RedM, which wont work (for obvious reasons)

1 Like

Thanks for the help!

I managed after a lot of hurdles to get it working. Couple of questions, not sure if you know these:

  • The assemblies don’t export as “-xxx.net-” so I added the .net to the client and server assemblies.
  • There is an issue in the .csproj files in regard to: enable so I changed the following:
    <TargetFramework>net452</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <LangVersion>10.0</LangVersion>
    <AssemblyName>$(MSBuildProjectName).net</AssemblyName>
  </PropertyGroup>

  <!-- Only enable nullable feature for the supported frameworks -->
  <PropertyGroup Condition=" '$(TargetFramework)' != 'netstandard2.0' ">
    <Nullable>enable</Nullable>
  </PropertyGroup>
  • I have the following issues remaining, although the first one is probably because I initially build it without the .net. Regarding the server error. Does this mean i need to use the standard functionality and use mono_rt2 only for client?
[   1196375] [b1491_GTAProce]             MainThrd/ Could not load assembly Client/bin/release/net452/Client.net - loading exceptions: Exception loading assembly Client/bin/release/net452/Client.net.dll: System.NullReferenceException: Object reference not set to an instance of an object.

[   1196235] [b1491_GTAProce]             MainThrd/ ^1Error: mono_rt2 is no longer supported since (2024-03-31), skipped loading Server/bin/release/net452/Server.net.dll.^7

[   1196375] [b1491_GTAProce]             MainThrd/   at CitizenFX.Core.ScriptManager.LoadScripts (System.Reflection.Assembly assembly) [0x00024] in C:\gl\builds\cfx-fivem-2\code\client\clrcore-v2\ScriptManager.cs:67 

[   1196375] [b1491_GTAProce]             MainThrd/   at CitizenFX.Core.ScriptManager.LoadAssemblyInternal (System.String name, System.Text.StringBuilder exceptions) [0x00099] in C:\gl\builds\cfx-fivem-2\code\client\clrcore-v2\ScriptManager.cs:163 

Again thank you for the help!

You need to update the mono_rt2 string to Prerelease expiring 2024-12-31. See https://aka.cfx.re/mono-rt2-preview for info.

It should also be noted that you can use nullable in any version of the runtime, as its always been a thing, it was just never enabled for C# iirc.

Thanks, maybe I missed a thing, I remade it, and indeed no issues currently.

However updating the string doesn’t matter:

[   6062016] [b1491_GTAProce]             MainThrd/ ^1Error: mono_rt2 is no longer supported since (2024-03-31), skipped loading Server/bin/release/net452/Server.net.dll.^7
[   6062016] [b1491_GTAProce]             MainThrd/ Started resource framework
[   6062016] [b1491_GTAProce]             MainThrd/ 
[   6062032] [b1491_GTAProce]             MainThrd/ Creating script environments for framework
[   6062078] [b1491_GTAProce]             MainThrd/ Loaded Client.net, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null into ScriptDomain_8331
[   6062125] [b1491_GTAProce]             MainThrd/ Game is: redm
[   6062125] [b1491_GTAProce]             MainThrd/ Instantiated instance of script Server.Main.
[   6062125] [b1491_GTAProce]             MainThrd/ ^3Warning: Assembly Client/bin/release/net452/Client.net.dll has been loaded into the mono rt2 runtime. This runtime is still in beta and shouldn't be used in production, crashes and breaking changes are to be expected.^7
[   6062125] [b1491_GTAProce]             MainThrd/ OnConnectionProgress: Mounted framework (1 of 1)

I’ve breaking my head on this for the last couple of hours. However I cant get it to work server side.
Even with the following manifest.lua:

game 'common'

rdr3_warning 'I acknowledge that this is a prerelease build of RedM, and I am aware my resources *will* become incompatible once RedM ships.'

fx_version 'cerulean'

mono_rt2 'Prerelease expiring 2024-12-31. See https://aka.cfx.re/mono-rt2-preview for info.'

files { "Client/bin/CitizenFX.RedM.Native.dll" }

client_scripts { "Client/bin/Release/net452/*.dll" }
server_scripts { "Server/bin/Release/net452/*.dll" }

I’m still receiving the same error:

[  22570875] [b1491_GTAProce]             MainThrd/ ^1Error: mono_rt2 is no longer supported since (2024-03-31), skipped loading Server/bin/Release/net452/Server.net.dll.^7

Whenever I change the date to a random date then I get the message to change it to what you said. Any advise for me what to do?

Again thank you for your help!