使用 IL2CPP 和 Npgsql 将 Unity3D 应用程序连接到 PostgreSQL 数据库时出错,SocketException

问题描述

利用 Npgsql 数据库驱动程序,我尝试在 Unity3D 和 Postgresql 服务器之间建立连接。通常,我将 Npgsql 3.2.7 dll 的 .Net Standard 2.0 版本和 System.Threading.Tasks.Extensions 4.4.0 中的一个(它是一个依赖项)复制到 Unity 的 插件 em> 文件夹。我想为 Andoird 和 iOS 开发跨平台应用。

编辑器中运行并使用脚本后端选项Mono编译应用程序在Android上运行良好。切换到 IL2CPP,这是 iOS 的唯一选项,我得到一个 SocketException:

Android Mono (Logcat) -- 工作正常

2021/05/01 13:15:04.257 27975 28015 Info Unity ConnectionString created.
2021/05/01 13:15:04.337 27975 28015 Info Unity NpgsqlConnection object created.
2021/05/01 13:15:07.781 27975 28015 Info Unity NpgsqlConnection opened.

Android IL2CPP (Logcat) -- SocketException

2021/05/01 13:11:57.425 27562 27607 Info IL2CPP Locale de-DE
2021/05/01 13:11:57.432 27562 27607 Info Unity ConnectionString created.
2021/05/01 13:11:57.432 27562 27607 Info Unity UnityEngine.Logger:Log(LogType,Object)
2021/05/01 13:11:57.432 27562 27607 Info Unity DatabaseStarter:Start()
2021/05/01 13:11:57.432 27562 27607 Info Unity 
2021/05/01 13:11:57.437 27562 27607 Info Unity NpgsqlConnection object created.
2021/05/01 13:11:57.437 27562 27607 Info Unity UnityEngine.Logger:Log(LogType,Object)
2021/05/01 13:11:57.437 27562 27607 Info Unity DatabaseStarter:Start()
2021/05/01 13:11:57.437 27562 27607 Info Unity 
2021/05/01 13:11:57.623 27562 27607 Error Unity SocketException: Operation in progress
2021/05/01 13:11:57.623 27562 27607 Error Unity   at System.Net.sockets.socket.Connect (System.Net.EndPoint remoteEP) [0x00000] in <00000000000000000000000000000000>:0 
2021/05/01 13:11:57.623 27562 27607 Error Unity   at Npgsql.NpgsqlConnector.Connect (Npgsql.NpgsqlTimeout timeout) [0x00000] in <00000000000000000000000000000000>:0 
2021/05/01 13:11:57.623 27562 27607 Error Unity   at Npgsql.NpgsqlConnector+<RawOpen>d__148.MoveNext () [0x00000] in <00000000000000000000000000000000>:0 
2021/05/01 13:11:57.623 27562 27607 Error Unity   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine] (TStateMachine& stateMachine) [0x00000] in <00000000000000000000000000000000>:0 
2021/05/01 13:11:57.623 27562 27607 Error Unity   at Npgsql.NpgsqlConnector.RawOpen (Npgsql.NpgsqlTimeout timeout,System.Boolean async,System.Threading.CancellationToken cancellationToken) [0x00000] in <00000000000000000000000000000000>:0 
2021/05/01 13:11:57.623 27562 27607 Error Unity   at Npgsql.NpgsqlConnector+<Open>d__145.MoveNext () [0x00000] in <00000000000000000000000000000000>:0 
2021/05/01 13:11:57.623 27562 27607 Error Unity   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine] (TStateMachine& stateMachine) [0x00000] in <00000000000000000000000000000000>:0 
2021/05/01 13:11:57.623 27562 27607 Error Unity   at Npgsql.Npgs

和 iOS (xCode) -- SocketException

2021-05-01 13:39:01.831189+0200 NpgsqlTestProject[29609:1518602] Built from '2020.3/staging' branch,Version '2020.3.6f1 (338bb68529b2)',Build type 'Release',Scripting Backend 'il2cpp'
2021-05-01 13:39:01.833137+0200 NpgsqlTestProject[29609:1518602] MemoryManager: Using 'Default' Allocator.
-> applicationDidFinishLaunching()
-> applicationDidBecomeActive()
GfxDevice: creating device client; threaded=1
Initializing Metal device caps: Apple A10 GPU
Initialize engine version: 2020.3.6f1 (338bb68529b2)
2021-05-01 13:39:02.742516+0200 NpgsqlTestProject[29609:1518602] Unbalanced calls to begin/end appearance transitions for <UnityViewControllerStoryboard: 0x104a08ce0>.
UnloadTime: 1.094875 ms
ConnectionString created.
DatabaseStarter:Start()

NpgsqlConnection object created.
DatabaseStarter:Start()

SocketException: Operation in progress
  at System.Net.sockets.socket.Connect (System.Net.EndPoint remoteEP) [0x00000] in <00000000000000000000000000000000>:0 
  at Npgsql.NpgsqlConnector.Connect (Npgsql.NpgsqlTimeout timeout) [0x00000] in <00000000000000000000000000000000>:0 
  at Npgsql.NpgsqlConnector+<RawOpen>d__148.MoveNext () [0x00000] in <00000000000000000000000000000000>:0 
  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine] (TStateMachine& stateMachine) [0x00000] in <00000000000000000000000000000000>:0 
  at Npgsql.NpgsqlConnector.RawOpen (Npgsql.NpgsqlTimeout timeout,System.Threading.CancellationToken cancellationToken) [0x00000] in <00000000000000000000000000000000>:0 
  at Npgsql.NpgsqlConnector+<Open>d__145.MoveNext () [0x00000] in <00000000000000000000000000000000>:0 
  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine] (TStateMachine& stateMachine) [0x00000] in <00000000000000000000000000000000>:0 
  at Npgsql.NpgsqlConnector.Open (Npgsql.NpgsqlTimeout timeout,System.Threading.CancellationToken cancellationToken) [0x00000] in <00000000000000000000000000000000>:0 
  at Npgsql.ConnectorPool+<AllocateLong>d__24.MoveNext () [0x00000] in <00000000000000000000000000000000>:0 
  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1[TResult].Start[TStateMachine] (TStateMachine& stateMachine) [0x00000] in <00000000000000000000000000000000>:0 
  at Npgsql.ConnectorPool.AllocateLong (Npgsql.NpgsqlConnection conn,Npgsql.NpgsqlTimeout timeout,System.Threading.CancellationToken cancellationToken) [0x00000] in <00000000000000000000000000000000>:0 
  at Npgsql.NpgsqlConnection+<Open>d__28.MoveNext () [0x00000] in <00000000000000000000000000000000>:0 
  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine] (TStateMachine& stateMachine) [0x00000] in <00000000000000000000000000000000>:0 
  at Npgsql.NpgsqlConnection.Open (System.Boolean async,System.Threading.CancellationToken cancellationToken) [0x00000] in <00000000000000000000000000000000>:0 
  at Npgsql.NpgsqlConnection.Open () [0x00000] in <00000000000000000000000000000000>:0 
  at DatabaseStarter.Start () [0x00000] in <00000000000000000000000000000000>:0 
--- End of stack trace from prevIoUs location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptiondispatchInfo.Throw () [0x00000] in <00000000000000000000000000000000>:0 
  at Npgsql.NpgsqlConnector+<Open>d__145.MoveNext () [0x00000] in <00000000000000000000000000000000>:0 
  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine] (TStateMachine& stateMachine) [0x00000] in <00000000000000000000000000000000>:0 
  at Npgsql.NpgsqlConnector.Open (Npgsql.NpgsqlTimeout timeout,System.Threading.CancellationToken cancellationToken) [0x00000] in <00000000000000000000000000000000>:0 
  at Npgsql.NpgsqlConnection.Open () [0x00000] in <00000000000000000000000000000000>:0 
  at DatabaseStarter.Start () [0x00000] in <00000000000000000000000000000000>:0 

--- End of stack trace from prevIoUs location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptiondispatchInfo.Throw () [0x00000] in <00000000000000000000000000000000>:0 
  at Npgsql.ConnectorPool+<AllocateLong>d__24.MoveNext () [0x00000] in <00000000000000000000000000000000>:0 
  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1[TResult].Start[TStateMachine] (TStateMachine& stateMachine) [0x00000] in <00000000000000000000000000000000>:0 
  at Npgsql.ConnectorPool.AllocateLong (Npgsql.NpgsqlConnection conn,System.Threading.CancellationToken cancellationToken) [0x00000] in <00000000000000000000000000000000>:0 
  at Npgsql.NpgsqlConnection.Open () [0x00000] in <00000000000000000000000000000000>:0 
  at DatabaseStarter.Start () [0x00000] in <00000000000000000000000000000000>:0 
--- End of stack trace from prevIoUs location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptiondispatchInfo.Throw () [0x00000] in <00000000000000000000000000000000>:0 
  at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in <00000000000000000000000000000000>:0 
  at System.Runtime.CompilerServices.ValueTaskAwaiter`1[TResult].GetResult () [0x00000] in <00000000000000000000000000000000>:0 
  at Npgsql.NpgsqlConnection+<Open>d__28.MoveNext () [0x00000] in <00000000000000000000000000000000>:0 
  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine] (TStateMachine& stateMachine) [0x00000] in <00000000000000000000000000000000>:0 
  at Npgsql.NpgsqlConnection.Open (System.Boolean async,System.Threading.CancellationToken cancellationToken) [0x00000] in <00000000000000000000000000000000>:0 
  at Npgsql.NpgsqlConnection.Open () [0x00000] in <00000000000000000000000000000000>:0 
  at DatabaseStarter.Start () [0x00000] in <00000000000000000000000000000000>:0 
--- End of stack trace from prevIoUs location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptiondispatchInfo.Throw () [0x00000] in <00000000000000000000000000000000>:0 
  at Npgsql.NpgsqlConnection.Open () [0x00000] in <00000000000000000000000000000000>:0 
  at DatabaseStarter.Start () [0x00000] in <00000000000000000000000000000000>:0 

到目前为止,解决问题的不成功方法有:

  • 下载更新的 Npgsql dll 文件包括在 nugget 网站上列出的声明为依赖项的 dll 文件
  • 在 Unity 的Api 兼容级别.Net Standard 2.0.Net 4.x
  • 之间切换
  • 更改 Unity 版本:我尝试了 2021.2.0a15 (alpha)、2021.1.5f1、2020.03.6f1 和 2018.4.31f1

为了调试,我使用这个脚本附加到场景中唯一的GameObject,以消除所有干扰风险:

using UnityEngine;
using Npgsql;

public class DatabaseStarter : MonoBehavIoUr
{
    // Start is called before the first frame update
    void Start()
    {
        NpgsqlConnectionStringBuilder connectionBuilder = new NpgsqlConnectionStringBuilder();
        connectionBuilder.Host = "... .rds.amazonaws.com";
        connectionBuilder.Port = 5432;
        connectionBuilder.Username = "...";
        connectionBuilder.Password = "...";
        connectionBuilder.Database = "...";
        connectionBuilder.TrustServerCertificate = true;
        connectionBuilder.SslMode = SslMode.Require;

        Debug.Log("ConnectionString created.");

        NpgsqlConnection connection = new NpgsqlConnection(connectionBuilder.ConnectionString);


        Debug.Log("NpgsqlConnection object created.");

        connection.open();

        Debug.Log("NpgsqlConnection opened.");
    }
}

由于 Unity 的 Reflect 产品(面向所有平台)似乎也依赖于 Npgsql.EntityFrameworkCore.Postgresql 插件,正如其 License 中提到的那样,我认为可能有解决方法

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...