我遇到了来自 Azure 应用服务 Web 应用程序 (.NET Framework 4.7) 和在容器实例中运行的服务的连接问题。尝试建立连接时,会引发异常。启用 .NET Socket 日志记录时,我看到以下内容:
System.Net.Sockets Verbose: 0 : [4404] Entering Socket#48359280::Connect(10.0.1.4)
ProcessId=7176
DateTime=2021-02-09T08:08:14.0357422Z
System.Net.Sockets Verbose: 0 : [4404] Entering DNS::GetHostAddresses(10.0.1.4)
ProcessId=7176
DateTime=2021-02-09T08:08:14.0357422Z
System.Net.Sockets Verbose: 0 : [4404] Exiting DNS::GetHostAddresses() -> IPAddress[]#44849943
ProcessId=7176
DateTime=2021-02-09T08:08:14.0490561Z
System.Net.Sockets Verbose: 0 : [4404] Entering Socket#48359280::Connect(IPAddress[]#44849943)
ProcessId=7176
DateTime=2021-02-09T08:08:14.0490561Z
System.Net.Sockets Verbose: 0 : [4404] Entering Socket#48359280::Connect([::ffff:10.0.1.4]:7600#501859741)
ProcessId=7176
DateTime=2021-02-09T08:08:14.0490561Z
System.Net.Sockets Error: 0 : [4404] Socket#48359280::UpdateStatusAfterSocketError() - AccessDenied
ProcessId=7176
DateTime=2021-02-09T08:08:14.0658428Z
System.Net.Sockets Error: 0 : [4404] Exception in Socket#48359280::Connect - An attempt was made to access a socket in a way forbidden by its access permissions [::ffff:10.0.1.4]:7600.
ProcessId=7176
建立连接的代码是:
try
{
socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
if (socket != null)
{
socket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false);
socket.NoDelay = noDelay;
socket.Connect(fileservername, port);
// More statements omitted ...
}
}
catch (Exception ex)
{
string message = String.Format("Cannot connect to fileserver {0} on port {1}", fileservername, port);
Debug.WriteLine(message);
Debug.WriteLine("{0}", ex.ToString());
throw new FileserverException(message, -1);
}
容器在 vnet 内运行并具有 IP 10.0.1.4。使用 Kudo PowerShell 时,我能够从应用服务服务器连接到容器。当使用公共 IP 时,它也可以工作。
看起来像 10.0.1.4 变成了 "::ffff:10.0.1.4" 并且建立到这个地址的连接不起作用。我们已将 SocketOptionName.IPv6Only 设置为 false。
是否不能在 Azure vnet 中使用 AddressFamily.InterNetworkV6?
代码也在本地环境中运行,没有问题。因此,我们不想包含一个检查 Azure 并仅使用 IP4 的开关。这个问题有没有更好的解决方案?
感谢您的帮助。