最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Running a Web API as a Process with a real tcp socket .NET - Stack Overflow

programmeradmin1浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论