我有一个 azure 队列触发器函数,其中包含以下代码:
using (var client = new TcpClient(AddressFamily.InterNetworkV6))
{
client.Client.DualMode = true;
client.Connect(endpoint);
var data = Encoding.ASCII.GetBytes("test");
using (var outStream = client.GetStream())
{
outStream.Write(data, 0, data.Length);
}
}
我返回的错误:
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
端点地址看起来正确,并且当我在本地调试时此代码可以工作,因此我怀疑 azure 服务器可能不允许出站连接。
知道为什么此连接不起作用吗?
<小时/>更新:这仍然不起作用,我尝试通过以下方式生成客户端:
// DualMode IPV6
var client = new TcpClient(AddressFamily.InterNetworkV6);
client.Client.DualMode = true;
client.Connect(endpoint);
// SingleMode Internetwork
var client = new TcpClient(AddressFamily.InterNetwork);
client.Connect(endpoint);
// Just Endpoint
var client = new TcpClient(endpoint);
client.Connect(endpoint);
// Normal
var client = new TcpClient(hostAddress, port);
// Forced IPV6
var client = new TcpClient("::ffff:" + hostAddress, port);
在本地调试,除了“强制 IPV6”之外,所有这些方法都可以正常工作。在服务器上,我收到以下错误:
== DualMode IPV6
Failed PingBack: A connection attempt failed because the connected party did not properly
respond after a period of time, or established connection failed because connected host
has failed to respond [::ffff:204.16.184.62]:3164
== SingleMode Internetwork
Failed PingBack: A connection attempt failed because the connected party did not properly
respond after a period of time, or established connection failed because connected host
has failed to respond 204.16.184.62:3164
== Just Endpoint
Failed PingBack: The requested address is not valid in its context
== Normal
Failed PingBack: A connection attempt failed because the connected party did not properly
respond after a period of time, or established connection failed because connected host
has failed to respond 204.16.184.62:3164
== Forced IPV6
Failed PingBack: The requested address is not valid in its context [::ffff:204.16.184.62]:3164
最佳答案
查看您的 TcpClient
实例,
var client = new TcpClient(AddressFamily.InterNetworkV6)
Azure Functions 中尚无 IPv6。将您的 AddressFamily
切换到 v4:
var client = new TcpClient(AddressFamily.InterNetwork)
应用服务/功能中的出站目的地没有限制。
关于azure - 来自 Azure Function 的 TcpIp 通信?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49566498/