首页 > 解决方案 > Azure App Service Web 应用无法连接到 vnet 中的容器实例

问题描述

我遇到了来自我们的 Azure 应用服务 Web 应用程序 (.NET Framework 4.7) 和我们在容器实例中运行的服务的连接问题。尝试建立连接时,会引发异常。启用 .NET 套接字日志记录时,我看到以下内容:

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 的开关。这个问题有更好的解决方案吗?

谢谢你的帮助。

标签: .netazuresocketsvnet

解决方案


推荐阅读