问题
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