GithubHelp home page GithubHelp logo

http-from-docker-to-local's Introduction

http-from-docker-to-local

This is a demo solution of a Worker process running on Docker, making HTTP requests to an API running on IIS Express on the local machine.

There are 2 APIs:

  • Api: older style of Web API with Global.asax and older csproj format
  • ApiV2: newer style of Web API with launchSettings.json, Startup and more recent csproj format

Both APIs:

  • target .NET Framework 4.8
  • create HTTP bindings on port 55173
  • binding to both localhost and 127.0.0.1

Typically IIS Express only binds to localhost, and if the same request is made to 127.0.0.1, the response will be a Bad Request - Invalid Hostname (HTTP 400).

To test this, simply run either Api or ApiV2, open a browser tab and go to http://localhost:55173/api/values or http://127.0.0.1:55173/api/values, both should work.

The place where the bindings are being defined is in the following file, under the element <sites>:

  • src\.vs\HttpFromDockerToLocal\config\applicationhost.config
            <site name="Api" id="2">
                <application path="/" applicationPool="Clr4IntegratedAppPool">
                    <virtualDirectory path="/" physicalPath="C:\Git\http-from-docker-to-local\src\Api" />
                </application>
                <bindings>
                    <binding protocol="http" bindingInformation="*:55173:localhost" />
                    <binding protocol="http" bindingInformation="*:55173:127.0.0.1" />
                    <binding protocol="https" bindingInformation="*:44364:localhost" />
                </bindings>
            </site>
            <site name="ApiV2" id="3">
                <application path="/" applicationPool="Clr4IntegratedAppPool">
                    <virtualDirectory path="/" physicalPath="C:\Git\http-from-docker-to-local\src\ApiV2" />
                </application>
                <bindings>
                    <binding protocol="http" bindingInformation="*:55173:localhost" />
                    <binding protocol="http" bindingInformation="*:55173:127.0.0.1" />
                </bindings>
            </site>

While the Api project will make use of this applicationhost.config file by default, the newer style ApiV2 is referencing this file in launchSettings.json, by executing the iisexpress.exe [/config:path-to-applicationhost.config] [/site:corresponding-site-name-in-applicationhost.config] [/apppool:corresponding-app-pool-in-applicationhost.config]

When running the Worker application, depending on whether if it's on the local machine or in docker, it will set the API_BASE_URL environment variable to http://localhost:55173 or http://host.docker.internal:55173 respectively, as per defined in launchSettings.json file. The host.docker.internal in a docker container will resolve to the local machine, where the API has the binding to 127.0.0.1, solving the Bad Request error.

http-from-docker-to-local's People

Contributors

luiscnsousa avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.