2

I am using the following code in a Utils script and calling this variable each time I want to create a HttpClient:

public static IHttpClientFactory httpClientFactory = new ServiceCollection().AddHttpClient().BuildServiceProvider().GetService<System.Net.Http.IHttpClientFactory>();

I have included all the dependencies under the plugins folder such the Microsoft.Extensions.DependencyInjection.dll and its respective dependencies dlls. When compiled with mono scripting backend the game server runs fine. the problem when compiling with IL2CPP I get the following error and the program seems to halt:

ArgumentNullException: Value cannot be null.
Parameter name: obj
  at System.Threading.Monitor.ReliableEnterTimeout (System.Object obj, System.Int32 timeout, System.Boolean& lockTaken) [0x00000] in <00000000000000000000000000000000>:0 
  at System.Threading.Monitor.ReliableEnter (System.Object obj, System.Boolean& lockTaken) [0x00000] in <00000000000000000000000000000000>:0 
  at System.Threading.Monitor.Enter (System.Object obj, System.Boolean& lockTaken) [0x00000] in <00000000000000000000000000000000>:0 
  at Microsoft.Extensions.DependencyInjection.DependencyInjectionEventSource.ServiceProviderBuilt (Microsoft.Extensions.DependencyInjection.ServiceProvider provider) [0x00000] in <00000000000000000000000000000000>:0 
  at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor (System.Collections.Generic.ICollection`1[T] serviceDescriptors, Microsoft.Extensions.DependencyInjection.ServiceProviderOptions options) [0x00000] in <00000000000000000000000000000000>:0 
  at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider (Microsoft.Extensions.DependencyInjection.IServiceCollection services, Microsoft.Extensions.DependencyInjection.ServiceProviderOptions options) [0x00000] in <00000000000000000000000000000000>:0 
  at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider (Microsoft.Extensions.DependencyInjection.IServiceCollection services) [0x00000] in <00000000000000000000000000000000>:0 
  at Utils..cctor () [0x00000] in <00000000000000000000000000000000>:0 
  at Myscript.Awake () [0x00000] in <00000000000000000000000000000000>:0 
Rethrow as TypeInitializationException: The type initializer for 'Utils' threw an exception.
  at Myscript.Awake () [0x00000] in <00000000000000000000000000000000>:0

I tried different methods I included the Link.xml file in my assets folder reading the following but the same error keep popping out:

    <linker>
    <assembly fullname="System.Core">
        <type fullname="System.Linq.Expressions.Interpreter.LightLambda" preserve="all" />
    </assembly>
        <assembly fullname="Plugins/Microsoft.Extensions.Http" preserve="all" />
        <assembly fullname="Plugins/Microsoft.Extensions.Logging.Abstractions" preserve="all" />

     //The rest of the dlls included in the plugins folder declared in the same manner...
    </linker>
  • Unity doesnt normally use httpclient - which might be why. It will be available in windows but not necessarily on linux. Use unitywebrequest instead. – BugFinder Sep 23 '22 at 21:56
  • Nope, in fact it does work fine on linux. The problem arises just when building with IL2CPP – Mohammed Sitni Sep 24 '22 at 00:22

1 Answers1

0

Since the variable is static the when of initialization is complicated and could vary between platforms. My suspicion is that initialization is occurring too soon before all of the IHttpClientFactory dependencies are ready. Could you try delaying initialization to test if this is the culprit? Something like.

// don't initialize here ...
public static IHttpClientFactory httpClientFactory;

// ... put this immediately before the first use of httpClientFactory
if (httpClientFactory == null) {
    httpClientFactory = new ServiceCollection().AddHttpClient().BuildServiceProvider().GetService<System.Net.Http.IHttpClientFactory>();
}
// first use of httpClientFactory
httpClientFactory.DoSomething();
dynamicbutter
  • 311
  • 1
  • 5
  • I tried this method, luckily I ported the whole server into a console app,so technically this doesn't matter to me anymore. anyway thank you for your kind response. – Mohammed Sitni Dec 14 '22 at 10:24