unable to connect to restful api hosted in docker container












0















I am facing an issue with my project i've built an api project and put it into a docker container.



I can reach the container via the web just fine using local host inside a browser, but when trying to consume it through code via restsharp, httpclient i get connection refused.



I tried local host, the direct 172 address assigned and even exposed a port pointed to 127.0.0.1 per other directions i've read.



I am at a lost of next steps because i can not seem to get this to work in any way



here is my simple code i set up:



api call



[Route("Internal/Email")]
public class EmailController : ControllerBase
{
[Route("Get")]
[HttpGet]
public ActionResult<string> Get()
{
return "email got";
}
}


consumer call:



public class TestController : Controller
{
private ServiceUrlConstants _serviceUrlConstants = new ServiceUrlConstants();
private string _serviceUrl;
public IActionResult GetEmail()
{
try
{
_serviceUrl = _serviceUrlConstants.InternalServiceUrl;
var httpClient = new HttpClient();
var response = httpClient.GetAsync(_serviceUrl + "Internal/Email/Get");
var content = response.Result.Content.ReadAsStringAsync();
httpClient.Dispose();
//client = new RestClient(_serviceUrl + "Internal/Email/Get");
//var response = client.Execute(request);
//var content = response.Content;
return RedirectToAction("Index");
}
catch (Exception ex)
{
return RedirectToAction("Index");
}
}
}


Docker configuration (auto built by visual studio except third port exposure)



internalapiservice:
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_URLS=https://+:443;http://+:80
- ASPNETCORE_HTTPS_PORT=44310
ports:
- "17399:80"
- "44310:443"
- "127.0.0.1:45310:45310"
volumes:
- ${APPDATA}/ASP.NET/Https:/root/.aspnet/https:ro
- ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro









share|improve this question























  • Docker Toolbox, Docker Machine, or Docker for Windows? What's _serviceUrl in your code? (Better to make it configurable, say via an environment variable, then putting it in a "constants" class.) Where are you running the client?

    – David Maze
    Aug 19 '18 at 13:04











  • @DavidMaze i am running docker on windows using linux containers, the url is just a class right now with a conditional for debug, as the project progresses it will move into a config file. that url is pointed to 127.0.0.1:45310 i am running the client on the same machine as the debug session.

    – micah hawman
    Aug 19 '18 at 13:22
















0















I am facing an issue with my project i've built an api project and put it into a docker container.



I can reach the container via the web just fine using local host inside a browser, but when trying to consume it through code via restsharp, httpclient i get connection refused.



I tried local host, the direct 172 address assigned and even exposed a port pointed to 127.0.0.1 per other directions i've read.



I am at a lost of next steps because i can not seem to get this to work in any way



here is my simple code i set up:



api call



[Route("Internal/Email")]
public class EmailController : ControllerBase
{
[Route("Get")]
[HttpGet]
public ActionResult<string> Get()
{
return "email got";
}
}


consumer call:



public class TestController : Controller
{
private ServiceUrlConstants _serviceUrlConstants = new ServiceUrlConstants();
private string _serviceUrl;
public IActionResult GetEmail()
{
try
{
_serviceUrl = _serviceUrlConstants.InternalServiceUrl;
var httpClient = new HttpClient();
var response = httpClient.GetAsync(_serviceUrl + "Internal/Email/Get");
var content = response.Result.Content.ReadAsStringAsync();
httpClient.Dispose();
//client = new RestClient(_serviceUrl + "Internal/Email/Get");
//var response = client.Execute(request);
//var content = response.Content;
return RedirectToAction("Index");
}
catch (Exception ex)
{
return RedirectToAction("Index");
}
}
}


Docker configuration (auto built by visual studio except third port exposure)



internalapiservice:
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_URLS=https://+:443;http://+:80
- ASPNETCORE_HTTPS_PORT=44310
ports:
- "17399:80"
- "44310:443"
- "127.0.0.1:45310:45310"
volumes:
- ${APPDATA}/ASP.NET/Https:/root/.aspnet/https:ro
- ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro









share|improve this question























  • Docker Toolbox, Docker Machine, or Docker for Windows? What's _serviceUrl in your code? (Better to make it configurable, say via an environment variable, then putting it in a "constants" class.) Where are you running the client?

    – David Maze
    Aug 19 '18 at 13:04











  • @DavidMaze i am running docker on windows using linux containers, the url is just a class right now with a conditional for debug, as the project progresses it will move into a config file. that url is pointed to 127.0.0.1:45310 i am running the client on the same machine as the debug session.

    – micah hawman
    Aug 19 '18 at 13:22














0












0








0








I am facing an issue with my project i've built an api project and put it into a docker container.



I can reach the container via the web just fine using local host inside a browser, but when trying to consume it through code via restsharp, httpclient i get connection refused.



I tried local host, the direct 172 address assigned and even exposed a port pointed to 127.0.0.1 per other directions i've read.



I am at a lost of next steps because i can not seem to get this to work in any way



here is my simple code i set up:



api call



[Route("Internal/Email")]
public class EmailController : ControllerBase
{
[Route("Get")]
[HttpGet]
public ActionResult<string> Get()
{
return "email got";
}
}


consumer call:



public class TestController : Controller
{
private ServiceUrlConstants _serviceUrlConstants = new ServiceUrlConstants();
private string _serviceUrl;
public IActionResult GetEmail()
{
try
{
_serviceUrl = _serviceUrlConstants.InternalServiceUrl;
var httpClient = new HttpClient();
var response = httpClient.GetAsync(_serviceUrl + "Internal/Email/Get");
var content = response.Result.Content.ReadAsStringAsync();
httpClient.Dispose();
//client = new RestClient(_serviceUrl + "Internal/Email/Get");
//var response = client.Execute(request);
//var content = response.Content;
return RedirectToAction("Index");
}
catch (Exception ex)
{
return RedirectToAction("Index");
}
}
}


Docker configuration (auto built by visual studio except third port exposure)



internalapiservice:
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_URLS=https://+:443;http://+:80
- ASPNETCORE_HTTPS_PORT=44310
ports:
- "17399:80"
- "44310:443"
- "127.0.0.1:45310:45310"
volumes:
- ${APPDATA}/ASP.NET/Https:/root/.aspnet/https:ro
- ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro









share|improve this question














I am facing an issue with my project i've built an api project and put it into a docker container.



I can reach the container via the web just fine using local host inside a browser, but when trying to consume it through code via restsharp, httpclient i get connection refused.



I tried local host, the direct 172 address assigned and even exposed a port pointed to 127.0.0.1 per other directions i've read.



I am at a lost of next steps because i can not seem to get this to work in any way



here is my simple code i set up:



api call



[Route("Internal/Email")]
public class EmailController : ControllerBase
{
[Route("Get")]
[HttpGet]
public ActionResult<string> Get()
{
return "email got";
}
}


consumer call:



public class TestController : Controller
{
private ServiceUrlConstants _serviceUrlConstants = new ServiceUrlConstants();
private string _serviceUrl;
public IActionResult GetEmail()
{
try
{
_serviceUrl = _serviceUrlConstants.InternalServiceUrl;
var httpClient = new HttpClient();
var response = httpClient.GetAsync(_serviceUrl + "Internal/Email/Get");
var content = response.Result.Content.ReadAsStringAsync();
httpClient.Dispose();
//client = new RestClient(_serviceUrl + "Internal/Email/Get");
//var response = client.Execute(request);
//var content = response.Content;
return RedirectToAction("Index");
}
catch (Exception ex)
{
return RedirectToAction("Index");
}
}
}


Docker configuration (auto built by visual studio except third port exposure)



internalapiservice:
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_URLS=https://+:443;http://+:80
- ASPNETCORE_HTTPS_PORT=44310
ports:
- "17399:80"
- "44310:443"
- "127.0.0.1:45310:45310"
volumes:
- ${APPDATA}/ASP.NET/Https:/root/.aspnet/https:ro
- ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro






c# rest docker docker-compose restsharp






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Aug 19 '18 at 12:42









micah hawmanmicah hawman

962




962













  • Docker Toolbox, Docker Machine, or Docker for Windows? What's _serviceUrl in your code? (Better to make it configurable, say via an environment variable, then putting it in a "constants" class.) Where are you running the client?

    – David Maze
    Aug 19 '18 at 13:04











  • @DavidMaze i am running docker on windows using linux containers, the url is just a class right now with a conditional for debug, as the project progresses it will move into a config file. that url is pointed to 127.0.0.1:45310 i am running the client on the same machine as the debug session.

    – micah hawman
    Aug 19 '18 at 13:22



















  • Docker Toolbox, Docker Machine, or Docker for Windows? What's _serviceUrl in your code? (Better to make it configurable, say via an environment variable, then putting it in a "constants" class.) Where are you running the client?

    – David Maze
    Aug 19 '18 at 13:04











  • @DavidMaze i am running docker on windows using linux containers, the url is just a class right now with a conditional for debug, as the project progresses it will move into a config file. that url is pointed to 127.0.0.1:45310 i am running the client on the same machine as the debug session.

    – micah hawman
    Aug 19 '18 at 13:22

















Docker Toolbox, Docker Machine, or Docker for Windows? What's _serviceUrl in your code? (Better to make it configurable, say via an environment variable, then putting it in a "constants" class.) Where are you running the client?

– David Maze
Aug 19 '18 at 13:04





Docker Toolbox, Docker Machine, or Docker for Windows? What's _serviceUrl in your code? (Better to make it configurable, say via an environment variable, then putting it in a "constants" class.) Where are you running the client?

– David Maze
Aug 19 '18 at 13:04













@DavidMaze i am running docker on windows using linux containers, the url is just a class right now with a conditional for debug, as the project progresses it will move into a config file. that url is pointed to 127.0.0.1:45310 i am running the client on the same machine as the debug session.

– micah hawman
Aug 19 '18 at 13:22





@DavidMaze i am running docker on windows using linux containers, the url is just a class right now with a conditional for debug, as the project progresses it will move into a config file. that url is pointed to 127.0.0.1:45310 i am running the client on the same machine as the debug session.

– micah hawman
Aug 19 '18 at 13:22












2 Answers
2






active

oldest

votes


















0














Sounds like your ports and connections are fine. I do not think that this is a network problem, but a problem with the environment variables.



Try:
- ASPNETCORE_URLS=https://localhost;http://localhost






share|improve this answer































    0














    I don't think external connections are allowed by default. You could try adding a URL reservation as detailed in this question. Maybe something like:



    netsh http add urlacl url=https://+:44310/ user=everyone


    (It looks like you are mapping container port 443 to 44310 on your machine..?)






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51917773%2funable-to-connect-to-restful-api-hosted-in-docker-container%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Sounds like your ports and connections are fine. I do not think that this is a network problem, but a problem with the environment variables.



      Try:
      - ASPNETCORE_URLS=https://localhost;http://localhost






      share|improve this answer




























        0














        Sounds like your ports and connections are fine. I do not think that this is a network problem, but a problem with the environment variables.



        Try:
        - ASPNETCORE_URLS=https://localhost;http://localhost






        share|improve this answer


























          0












          0








          0







          Sounds like your ports and connections are fine. I do not think that this is a network problem, but a problem with the environment variables.



          Try:
          - ASPNETCORE_URLS=https://localhost;http://localhost






          share|improve this answer













          Sounds like your ports and connections are fine. I do not think that this is a network problem, but a problem with the environment variables.



          Try:
          - ASPNETCORE_URLS=https://localhost;http://localhost







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 20 '18 at 11:52









          GenKaGenKa

          316




          316

























              0














              I don't think external connections are allowed by default. You could try adding a URL reservation as detailed in this question. Maybe something like:



              netsh http add urlacl url=https://+:44310/ user=everyone


              (It looks like you are mapping container port 443 to 44310 on your machine..?)






              share|improve this answer




























                0














                I don't think external connections are allowed by default. You could try adding a URL reservation as detailed in this question. Maybe something like:



                netsh http add urlacl url=https://+:44310/ user=everyone


                (It looks like you are mapping container port 443 to 44310 on your machine..?)






                share|improve this answer


























                  0












                  0








                  0







                  I don't think external connections are allowed by default. You could try adding a URL reservation as detailed in this question. Maybe something like:



                  netsh http add urlacl url=https://+:44310/ user=everyone


                  (It looks like you are mapping container port 443 to 44310 on your machine..?)






                  share|improve this answer













                  I don't think external connections are allowed by default. You could try adding a URL reservation as detailed in this question. Maybe something like:



                  netsh http add urlacl url=https://+:44310/ user=everyone


                  (It looks like you are mapping container port 443 to 44310 on your machine..?)







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Aug 20 '18 at 16:29









                  Morgan PeatMorgan Peat

                  84




                  84






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51917773%2funable-to-connect-to-restful-api-hosted-in-docker-container%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Costa Masnaga

                      Fotorealismo

                      Sidney Franklin