c# - 尝试从 gitpod IDE(ubuntu 服务器)连接到 MySql 数据库时出现套接字异常

标签 c# mysql ubuntu database-connection gitpod

我正在使用 gitpod 在线 IDE(浏览器中的 vs 代码,后面有一个 ubuntu linux 服务器)。
我正在努力在其他 linux (CentOS) 服务器上创建与我的 mysql 数据库的连接。我已将数据库服务器上的远程 MySql 主机设置为 '%' => 'all allowed'。

我正在使用 MySql 连接器包:https://www.nuget.org/packages/MySqlConnector连接到它。
这就是我的代码的样子:

public async Task<List<User>> GetUsersAsync()
{
      List<User> readUsers = new List<User>();
      string connection = "server=myserverip;database=db;user=user;password=pwd";
      using (MySqlConnection con = new MySqlConnection(connection))
      {
           MySqlCommand cmd = new MySqlCommand("SELECT * FROM admin", con);
           cmd.CommandType = CommandType.Text;
           con.Open();

           MySqlDataReader rdr = cmd.ExecuteReader();
           while(rdr.Read())
           {
               User user = new User();
               user.ID = Convert.ToInt32(rdr["ID"]);
               user.Username = rdr["Username"].ToString();
               user.Password = rdr["Password"].ToString();
               readUsers.Add(user);
           }
      }
      return readUsers;
}  

当然,连接字符串有其他值而不是上面显示的(出于安全原因)。
我收到以下错误,我只是不知道如何解决它们或问题到底是什么:

image of errors in console

我不确定如何继续或现在尝试什么。

最佳答案

从调用堆栈来看,您似乎正在使用 Blazor 将代码编译为 wasm。此代码在浏览器中运行,并受浏览器沙箱的所有限制。更具体地说,浏览器无法打开 TCP 套接字进行网络通信。

更多背景信息是 here :

Blazor is running in the browser sandbox and cannot do more than javascript can network wise. So technically this is not possible.



here :

TCP networking APIs that can't ever work in the browser, since JavaScript doesn't expose raw TCP networking capabilities. This naturally leads to a runtime failure from somewhere low in the stack.



这就是为什么你会得到一个 PlatformNotSupportedException .

要解决此问题,您需要重新架构代码,以便 MySQL 连接由运行在 Web 服务器上的代码建立,并且 Blazor 代码发出 HTTP 请求(到 Web API)以调用执行数据库的服务器端代码手术。

关于c# - 尝试从 gitpod IDE(ubuntu 服务器)连接到 MySql 数据库时出现套接字异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60165920/

相关文章:

c# - 可编辑的 WPF ListView

c# - 处理长时间运行循环中的超时

php - 用它在 php 中的当前值更新 MySql 值

python - centos无法安装mysqlclient

php - 查询列中的多个值

Ubuntu 15.10 : port 8080 already used

C# - 在按下键时从字符串中删除最后一个字符

c# - 为什么app.config运行不正常?

linux - 在ubuntu中检查进程的启动时间后,有没有办法在一定时间后杀死进程?

ubuntu - 在 ubuntu 上本地运行 Neo4J