WSL2 Azure SQL连接问题



我在WSL2 (Linux的Windows子系统)上运行。net 6 web应用程序,但与Azure SQL连接有连接问题。我一直得到以下异常:

System.InvalidOperationException: The exception handler configured on ExceptionHandlerOptions produced a 404 status response. This InvalidOperationException containing the original exception was thrown since this is often due to a misconfigured ExceptionHandlingPath. If the exception handler is expected to return 404 status responses then set AllowStatusCode404Response to true.
---> System.Data.Entity.Core.EntityException: The underlying provider failed on Open.
---> System.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 40 - Could not open a connection to SQL Server)
at System.Data.ProviderBase.DbConnectionPool.CheckPoolBlockingPeriod(Exception e)
at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)

我正在使用以下格式的连接字符串。

Server=tcp:[ServerName].database.windows.net,1433;Initial Catalog=[DatabaseName];Persist Security Info=False;User ID=[UserName];Password=[Password];MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;

这在Windows上工作,但在WSL2上不起作用。我用tcpping和nslookup检查了数据库服务器和端口是否可达,但由于某种原因,应用程序无法连接到它。

有谁知道为什么会发生这种情况,有解决方案吗?

  1. 尝试使用ExceptionHandlerOptions。AllowStatusCode404Response财产。
    修改UseExceptionHandler方法如下:

Startup.cs

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseExceptionHandler(
new ExceptionHandlerOptions()
{
AllowStatusCode404Response = true, // important!
ExceptionHandlingPath = "/error"                  
}
);      
}
  • 另外,如果Windows有任何VPN软件,关闭它,因为它会干扰WSL2 Windows网络连接。

相关内容

  • 没有找到相关文章

最新更新