Socket Exceptions when trying to connect to MySql database from gitpod IDE (ubuntu server)

旧时模样 提交于 2021-02-05 09:35:17

问题


I'm working with the gitpod online IDE ( vs code in the browser with an ubuntu linux server behind it). I'm struggling with creating a Connection to my mysql database on an other linux (CentOS) server. I've set the Remote MySql hosts on the db server to '%' => 'all allowed'.

I'm using the MySql Connector package: https://www.nuget.org/packages/MySqlConnector to connect to it. That's what my code looks like:

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;
}  

Of course the connection string has other values than displayed above (because of security reasons). I'm getting following errors and I just don't know how to solve them or what exactly the problem is:

I'm not sure how to continue or what to try now.


回答1:


From the call stack, it looks like you're compiling your code to wasm using Blazor. This code is running in the browser and is subject to all the limitations of the browser sandbox. More specifically, there is no ability in a browser to open a TCP socket for network communication.

Some more background info is here:

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

And 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.

That is why you are getting a PlatformNotSupportedException.

To fix this, you would need to rearchitect your code so that the MySQL connection is made from code running on a web server, and the Blazor code makes a HTTP request (to a web API) to invoke server-side code that performs the database operation.



来源:https://stackoverflow.com/questions/60165920/socket-exceptions-when-trying-to-connect-to-mysql-database-from-gitpod-ide-ubun

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!