admin 管理员组文章数量: 1086019
I am trying to run a test for my project that will need the whole API app to run by using process to start and run the application on a real TCP socket for eventual pact testing.
The API seems to start but my test fails to connect to the local host.
My test sets up the Process in a StartProcess
class and uses dotnet run
within the API app directory to run the app:
public class GetWeatherForecastTest
{
private Process StartProcess()
{
var processStartInfo = new ProcessStartInfo
{
FileName = "dotnet",
Arguments = "run",
WorkingDirectory = "../../../../ProcessAPI",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
var process = new Process { StartInfo = processStartInfo };
process.OutputDataReceived += (sender, args) => Debug.WriteLine(args.Data);
process.ErrorDataReceived += (sender, args) => Debug.WriteLine(args.Data);
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
return process;
}
[Fact]
public async Task GivenValidParameters_WhenGetWeatherForecast_ThenReturnForecast()
{
using (var process = StartProcess())
{
// Wait for the process to start and the server to be ready
await Task.Delay(5000);
using (var client = new HttpClient())
{
try
{
var response = await client.GetAsync("http://localhost:5083/WeatherForecast");
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
Assert.False(string.IsNullOrEmpty(responseString));
}
catch (Exception ex)
{
Debug.WriteLine($"Request error: {ex.Message}");
throw;
}
process.Kill();
}
}
}
}
The error response I get when running this test shows that the connection is refused.
[xUnit 00:00:09.25] System.Net.Http.HttpRequestException : No connection could be made because the target machine actively refused it. (localhost:5000)
[xUnit 00:00:09.25] ---- System.Net.Sockets.SocketException : No connection could be made because the target machine actively refused it.
[xUnit 00:00:09.25] Stack Trace:
[xUnit 00:00:09.25] at System.Net.Http.HttpConnectionPool.ConnectToTcpHostAsync(String host, Int32 port, HttpRequestMessage initialRequest, Boolean async, CancellationToken cancellationToken)
[xUnit 00:00:09.25] at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
[xUnit 00:00:09.25] at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
[xUnit 00:00:09.25] at System.Net.Http.HttpConnectionPool.InjectNewHttp11ConnectionAsync(QueueItem queueItem)
[xUnit 00:00:09.25] at System.Threading.Tasks.TaskCompletionSourceWithCancellation`1.WaitWithCancellationAsync(CancellationToken cancellationToken)
[xUnit 00:00:09.25] at System.Net.Http.HttpConnectionPool.SendWithVersionDetectionAndRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken)
[xUnit 00:00:09.25] at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
[xUnit 00:00:09.25] at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
[xUnit 00:00:09.25] C:\Programming\ProcessAPI\ProcessAPI.Test\GetWeatherForecastTest.cs(33,0): at ProcessAPI.Test.GetWeatherForecastTest.GivenValidParameters_WhenGetWeatherForecast_ThenReturnForecast()
So the app does appear to start but the test is unable to connect to the locally running server.
本文标签: cRunning a Web API as a Process with a real tcp socket NETStack Overflow
版权声明:本文标题:c# - Running a Web API as a Process with a real tcp socket .NET - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744047098a2524325.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论