我正在尝试从部署在 Azure 网站中的 ASP.NET MVC4 Web 应用程序(默认“互联网”)连接到未托管在 Azure 上的现有 MySQL 数据库。我尽可能简化以说明问题:
1)我通过 Nuget 添加到我的 MVC 应用程序 MySql.Data 和 MySql.Web
2)我在 About() 方法中的默认 HomeController 中编写了以下丑陋的代码:
public ActionResult About()
{
try
{
ViewBag.Message = "Your app description page.";
Trace.TraceInformation("Connection String");
var connStr =
"server=blah.blah.com;user=someone;database=somedatabase;port=3306;password=password;";
Trace.TraceInformation("Connection");
var connection = new MySqlConnection(connStr);
Trace.TraceInformation("Open Connection");
connection.Open();
Trace.TraceInformation("Close Connection");
connection.Close();
return View();
}
catch (Exception ex)
{
Trace.TraceError("{0} / {1} / {2}", ex.Message, ex.StackTrace, ex.TargetSite);
throw;
}
}
当我调试时,这在我的本地机器上运行没有故障(我检索数据也没有问题),但是当我发布到 Azure 网站时,如果失败得很惨。产生的 Trace 如下:
2013-07-31T03:05:02 PID[20636] Information Connection String
2013-07-31T03:05:02 PID[20636] Information Connection
2013-07-31T03:05:02 PID[20636] Information Open Connection
2013-07-31T03:05:09 PID[20636] Error Can't get hostname for your address
2013-07-31T03:05:09 PID[20636] Error Can't get hostname for your address / at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
at MySql.Data.MySqlClient.NativeDriver.Open()
at MySql.Data.MySqlClient.Driver.Open()
at MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings)
at MySql.Data.MySqlClient.MySqlPool.GetPooledConnection()
at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver()
at MySql.Data.MySqlClient.MySqlPool.GetConnection()
at MySql.Data.MySqlClient.MySqlConnection.Open()
at MvcTest.Controllers.HomeController.About() / MySql.Data.MySqlClient.MySqlPacket ReadPacket()
谁能帮我弄清楚我做错了什么?是否有特定原因导致 Azure 网站而不是我的本地计算机失败?注意:我的本地机器与 MySQL 数据库不在同一个网络上,或者类似的东西。