admin管理员组文章数量:1025202
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
1 Answer
Reset to default 1I 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
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
1 Answer
Reset to default 1I 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
版权声明:本文标题:c# - .NET Core 3.1: No connection could be made because the target machine actively refused it - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745612032a2159059.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论