How to open TCP port on localhost on VSTS build agent?

假如想象 提交于 2021-01-28 09:57:24

问题


When using the online hosted version of Visual Studio Team Services, my unit tests are unable to connect to a service listening on a TCP port on the localhost of the build agent. The service is able to start and open the TCP port but it seems unreachable for the unit test.

Error message:

2017-06-20T12:05:00.8231306Z ##[error]------------ System.Net.Http.HttpRequestException : An error occurred while sending the request. 2017-06-20T12:05:00.8231306Z ##[error]---------------- System.Net.WebException : Unable to connect to the remote server 2017-06-20T12:05:00.8231306Z ##[error]-------------------- System.Net.Sockets.SocketException : No connection could be made because the target machine actively refused it 127.0.0.1:41670

The service that opens the TCP port is started with:

    public void Start()
    {
        HttpPort = ObtainFreePort();
        TcpPort = ObtainFreePort();
        ClusterVNode node = EmbeddedVNodeBuilder.AsSingleNode()
            .WithInternalTcpOn(new IPEndPoint(IPAddress.Loopback, TcpPort))
            .WithExternalTcpOn(new IPEndPoint(IPAddress.Loopback, TcpPort))
            .WithInternalHttpOn(new IPEndPoint(IPAddress.Loopback, HttpPort))
            .WithExternalHttpOn(new IPEndPoint(IPAddress.Loopback, HttpPort))
            .AddExternalHttpPrefix($"http://+:{HttpPort}/")
            .RunProjections(ProjectionsMode.All)
            .StartStandardProjections()
            .RunInMemory()
            .Build();
        node.StartAndWaitUntilReady().Wait();
    }

    static int ObtainFreePort()
    {
        using (var sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
        {
            sock.Bind(new IPEndPoint(IPAddress.Loopback, 0));
            var port = ((IPEndPoint)sock.LocalEndPoint).Port;
            sock.Close();
            return port;
        }
    }

This does work on my local machine :) Is this not supported in Visual Studio Team Services online?


回答1:


If you're using the hosted agent, you can't open up ports or change anything about the machine's configuration. You'll need to set up your own agent for builds.

Also, if a test requires TCP communication, it's no longer a unit test. Unit tests have no external dependencies.



来源:https://stackoverflow.com/questions/44652906/how-to-open-tcp-port-on-localhost-on-vsts-build-agent

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!