如何在VSTS构建代理的localhost上打开TCP端口? [英] How to open TCP port on localhost on VSTS build agent?

查看:51
本文介绍了如何在VSTS构建代理的localhost上打开TCP端口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Visual Studio Team Services的联机托管版本时,我的单元测试无法连接到在构建代理的localhost上的TCP端口上侦听的服务.该服务能够启动和打开TCP端口,但是对于单元测试来说似乎无法访问.

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.

错误消息:

2017-06-20T12:05:00.8231306Z ## [错误] ------------System.Net.Http.HttpRequestException:发送时发生错误请求.2017-06-20T12:05:00.8231306Z ## [错误] ----------------System.Net.WebException:无法连接到远程服务器2017-06-20T12:05:00.8231306Z ## [错误] --------------------System.Net.Sockets.SocketException:无法建立连接因为目标计算机主动拒绝了127.0.0.1:41670

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

打开TCP端口的服务始于:

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;
        }
    }

这在我的本地计算机上有效:) Visual Studio Team Services在线不支持此功能吗?

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

推荐答案

如果使用的是托管代理,则无法打开端口或更改有关计算机配置的任何内容.您需要为构建建立自己的代理.

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.

此外,如果测试需要TCP通信,则不再是单元测试.单元测试没有外部依赖性.

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

这篇关于如何在VSTS构建代理的localhost上打开TCP端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆