C# IoC issue on client side

Hello,

I’m trying to write a FiveM resource in C#. Whole script setup will be done in Main.cs using Castle Windsor IoC framework. Now I managed to get it working on server side without any problems. However if I use the same approach on client side, I get the following error:

Override failure for Castle.MicroKernel.DefaultKernel:InitializeLifetimeService () over System.MarshalByRefObject:InitializeLifetimeService (). Override must be [SecurityCritical]

I tried setting in AssemblyInfo.cs, but it doesn’t help.
[assembly: SecurityCritical]

Sample code of Main.cs (do note that I don’t even need to register anything in IoC for it to fail)

private void OnResourceStart(string resourceName)
{
	try
	{
		if (GetCurrentResourceName() != resourceName)
		{
			return;
		}

		container = new WindsorContainer();

	}
	catch (Exception e)
	{
		Debug.WriteLine(e.Message);
	}
}

In addition to that I tried to use Autofac and Simpleinjector instead of Castle Windsor, but both of them received an error as well.

Hopefully somebody can help me!

1 Like

You’ll likely have to build existing libraries from source against the framework reference package since default metadata might be doing some things which aren’t supported in SL5 .NET API level.

Supposedly Castle Windsor supports .net 4.5 and Silverlight 5 ever since version 3.2

Or do you mean that FiveM artifact should be built?

E: I tried to build resource with 3.4.0 Windsor from NuGet, but it gives the same error.
In case of Autofac IoC framework error is following:
System.MethodAccessException: Error verifying Autofac.Util.SequenceGenerator:GetNextUniqueSequence (): Method System.Threading.Interlocked:Read (long&) is not accessible at 0x0005

Unless the SL5 assembly is still there, and you’re using it, I’m not expecting a binary to work at all.

Since Silverlight is generally “dead”, it might very well be most these libraries have given up on supporting its sandboxed API set.

Note the second part of that sentence:

It is also the last version to support Silverlight and .NET 3.5.

You’d have to use 3.2’s SL5 assembly, or forward-port that build mode to a newer version.

Thanks for the input! I tried to downgrade version to 3.2 and it produced the same error. The reason why I tried 3.4.0, was that it had the same major version, but more up to date library. I also tried to use Unity IoC framework. An old version of 2.0.0 didn’t crash the application. For the time being it seems that TypeScript with tsyringe is the way to go if you plan to write enterprise style resources in FiveM / RedM.

SL5 assemblies won’t directly be used by NuGet, you’d have to manually reference the right one.

Sorry I’m a bit unfamiliar with .net ecosystem (have Java background). I modified Client.csproj from

<Reference Include="Castle.Core, Version=3.3.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
    <HintPath>..\packages\Castle.Core.3.3.0\lib\net45\Castle.Core.dll</HintPath>
</Reference>
<Reference Include="Castle.Windsor, Version=3.4.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
    <HintPath>..\packages\Castle.Windsor.3.4.0\lib\net45\Castle.Windsor.dll</HintPath>
</Reference>

To

<Reference Include="Castle.Core, Version=3.3.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
    <HintPath>..\packages\Castle.Core.3.3.0\lib\sl5\Castle.Core.dll</HintPath>
</Reference>
<Reference Include="Castle.Windsor, Version=3.4.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
    <HintPath>..\packages\Castle.Windsor.3.4.0\lib\sl5\Castle.Windsor.dll</HintPath>
</Reference>

and it works with the code provided above. However upon class registration in container e.g.

var assembly = Assembly.GetExecutingAssembly();
foreach (Type type in assembly.GetTypes())
{
    Debug.WriteLine(type.FullName); // this prints out all project files no system packages
}
// Register state machine
container.Register(
    Classes
        .FromAssembly(assembly)
        .BasedOn<IZoneState>()
        .WithService.FromInterface()
        .LifestyleSingleton()
);

Results in

Exception loading assembly Client/bin/Debug/System.Windows: System.IO.FileNotFoundException: Unable to find the specified file

Anyways, thanks for the help!

Ouch, the SL5 version somehow ended up depending on a WPF assembly? Egh.