You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity Linux服务器IL2CPP编译时依赖注入抛出异常

Unity IL2CPP编译时IHttpClientFactory初始化抛出空引用异常

我在Utils脚本中使用以下代码初始化静态变量,每次创建HttpClient时都会调用它:

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

我已将所有依赖项(如Microsoft.Extensions.DependencyInjection.dll及其相关依赖DLL)放入Plugins文件夹。使用Mono脚本后端编译时游戏服务器运行正常,但切换为IL2CPP编译时出现以下错误,程序直接终止:

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

我尝试过在Assets文件夹中添加Link.xml文件来保留相关代码,但错误依然存在,Link.xml内容如下:

<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" />

    //Plugins文件夹中包含的其他DLL也以同样方式声明...
</linker>

内容的提问来源于stack exchange,提问作者Mohammed Sitni

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 18:16:05