最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

c# - .NET Core 3.1: No connection could be made because the target machine actively refused it - Stack Overflow

matteradmin7PV0评论

I am working on a core web app code challenge and I am getting the following error:

An unhandled exception occurred while processing the request. SocketException: No connection could be made because the target machine actively refused it. System.Net.Http.ConnectHelper.ConnectAsync(string host, int port, CancellationToken cancellationToken)

The code is in this repo

The error is occurring in this method:

public async Task<TeamDTO> AddTeam(string teamName)
{
    var client = _httpClientFactory.CreateClient();
    client.BaseAddress = new Uri(_baseUri);
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    var team = new TeamDTO { Name = teamName };
    var response = await client.PostAsync("teams", new StringContent(JsonSerializer.Serialize(team), Encoding.UTF8, "application/json"));
    response.EnsureSuccessStatusCode();
    var responseBody = await response.Content.ReadAsStringAsync();
    return JsonSerializer.Deserialize<TeamDTO>(responseBody, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
}

On this line of code:

var response = await client.PostAsync("teams", new StringContent(JsonSerializer.Serialize(team), Encoding.UTF8, "application/json"));

I am trying to connect to this API method:

    [HttpPost("Post")]
    public virtual ActionResult<GameDTO> Post(GameDTO gameDTO)
    {
        var game = new Game
        {
            HomeTeamId = gameDTO.HomeTeamId,
            AwayTeamId = gameDTO.AwayTeamId,
            Date = gameDTO.Date,
            WinningTeamId = gameDTO.WinningTeamId
        };
        game.Id = Repository.Insert(game);

        return Ok(gameDTO);
    }

I am running VS 2022 on Windows 11 pro. I am using IIS Express and launching both applications from the same VS 2022 solution. I have thoroughly tested (there are unit tests) the API code and it works. The API and the web app both launch. Everything works fine until I click Add Team in the games view of the web app, then the code shown above is hit etc. I believe this is a configuration issue.

I am working on a core web app code challenge and I am getting the following error:

An unhandled exception occurred while processing the request. SocketException: No connection could be made because the target machine actively refused it. System.Net.Http.ConnectHelper.ConnectAsync(string host, int port, CancellationToken cancellationToken)

The code is in this repo

The error is occurring in this method:

public async Task<TeamDTO> AddTeam(string teamName)
{
    var client = _httpClientFactory.CreateClient();
    client.BaseAddress = new Uri(_baseUri);
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    var team = new TeamDTO { Name = teamName };
    var response = await client.PostAsync("teams", new StringContent(JsonSerializer.Serialize(team), Encoding.UTF8, "application/json"));
    response.EnsureSuccessStatusCode();
    var responseBody = await response.Content.ReadAsStringAsync();
    return JsonSerializer.Deserialize<TeamDTO>(responseBody, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
}

On this line of code:

var response = await client.PostAsync("teams", new StringContent(JsonSerializer.Serialize(team), Encoding.UTF8, "application/json"));

I am trying to connect to this API method:

    [HttpPost("Post")]
    public virtual ActionResult<GameDTO> Post(GameDTO gameDTO)
    {
        var game = new Game
        {
            HomeTeamId = gameDTO.HomeTeamId,
            AwayTeamId = gameDTO.AwayTeamId,
            Date = gameDTO.Date,
            WinningTeamId = gameDTO.WinningTeamId
        };
        game.Id = Repository.Insert(game);

        return Ok(gameDTO);
    }

I am running VS 2022 on Windows 11 pro. I am using IIS Express and launching both applications from the same VS 2022 solution. I have thoroughly tested (there are unit tests) the API code and it works. The API and the web app both launch. Everything works fine until I click Add Team in the games view of the web app, then the code shown above is hit etc. I believe this is a configuration issue.

Share Improve this question edited Nov 18, 2024 at 17:13 James Z 12.3k10 gold badges27 silver badges47 bronze badges asked Nov 18, 2024 at 14:07 wayne.blackmonwayne.blackmon 76114 silver badges24 bronze badges 10
  • What's "teams?" – GH DevOps Commented Nov 18, 2024 at 14:18
  • The TeamsController. – wayne.blackmon Commented Nov 18, 2024 at 14:22
  • check your base url – Kabilu Commented Nov 18, 2024 at 14:23
  • "AllowedHosts": "*", "ApiSettings": { "BaseUri": "localhost:5000" }, _baseUri = configuration["ApiSettings:BaseUri"]; – wayne.blackmon Commented Nov 18, 2024 at 14:26
  • is service is running in 5000 port ? – Kabilu Commented Nov 18, 2024 at 14:28
 |  Show 5 more comments

1 Answer 1

Reset to default 1

I am using IIS Express and launching both applications from the same VS 2022 solution. I have thoroughly tested (there are unit tests) the API code and it works. The API and the web app both launch

You said you are using IIS Express ,but port of the base uri doesn't match;

"ApiSettings": { "BaseUri": "localhost:5000" },

 _baseUri = configuration["ApiSettings:BaseUri"];

modify 5000 to the port of your api project(20269) when you run it in IIS Express

Post a comment

comment list (0)

  1. No comments so far