0

I have a Visual Studio solution with two projects; API and MVC. MVC is my MVC-project containing my views etc., API is my API-project containing the API-endpoints and logic. Since I have two projects, I have configured my solution to have multiple startup-projects.

HttpClient

public const string AuctionAPI = "http://localhost:44337/";
public const string AuctionClient = "http://localhost:44398/";

public static HttpClient GetClient()
{
    HttpClient client = new HttpClient();
    client.BaseAddress = new Uri(AuctionAPI);
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
    return client;
}

MVC Controller (Notice: I receive the error on line 6, when I set HttpResponseMessage and try to call client.GetAsync()

[Route("item/{itemNumber}")]
public async Task<IActionResult> IndexForItem(int itemNumber)
{
    var client = GetClient();
    //ERROR: In next line, HttpResponseMessage it tries to call my API controller and fails
    HttpResponseMessage response = await client.GetAsync($@"api/Auction/SeeOneItem/{itemNumber}");

    if (response.IsSuccessStatusCode)
    {
        string content = await response.Content.ReadAsStringAsync();
        var model = await response.Content.ReadAsAsync<AuctionItemProxy>();

        return View(model);
    }
    else
    {
        return Content("An error occured.");
    }
}

API Controller (Notice: my breakpoint in this method never hits)

[HttpGet("SeeOneItem/{itemNumber:int}")]
public AuctionItem GetAuctionItem(int itemNumber)
{
    var item = _auctionItemDataFactory.AuctionItems.First(x => x.ItemNumber == itemNumber);
    if (item == null)
    {
        return null;
    }
    return _auctionItemDataFactory.AuctionItems.First(x => x.ItemNumber == itemNumber);
}

Actual error I receive

IOException: Unable to read data from the transport connection

6
  • Both projects in the same solution using IIS Express? Are you sure the first (API) is actually running when you are running the second (MVC)? Commented Dec 7, 2018 at 9:32
  • check this stackoverflow.com/questions/5420656/… Commented Dec 7, 2018 at 9:36
  • This problem also occurs if circular reference exists inside table definition. Try disabling lazy loading and proxy creation (assumed using EF Core). Commented Dec 7, 2018 at 9:40
  • @sellotape Yes, the problem has been correctly identified in the answer. Thanks for trying to help Commented Dec 8, 2018 at 10:26
  • @D-johnAnshani Thanks for the link, the problem was identified in the answer below Commented Dec 8, 2018 at 10:26

1 Answer 1

1

This error means that the target machine is accessible but the service you are trying to reach is not accessible.

By looking at your code you shared I noticed one thing. If the project is set up to use Https then IIS Express assigns ports starting with 443 resulting in a port like 443xx. Change your http scheme to https and try again.

public const string AuctionAPI = "http://localhost:44337/";

to

public const string AuctionAPI = "https://localhost:44337/";

OR

Change your project settings to not use SSL

  1. Right Click your project Go to properties
  2. In properties window go to Debug section
  3. Under Web Server Settings un-check "Enable SSL"

This issue can also be related to transport level security.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.