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
相关产品推荐
相关产品推荐

