Console app built on the .NET Generic Host that resolves an application service from the container and uses it to compute travel distance and duration between two places via the Distance Matrix API.
This is the dedicated showcase for the
GoogleMapsApi.Extensions.DependencyInjection
package: IGoogleMapsClient is registered once with AddGoogleMaps(...) and the ambient API key
is bound from configuration, so TravelTimeService depends only on IGoogleMapsClient and
never sees the key or an HttpClient.
GOOGLE_API_KEY=your_api_key dotnet run --project samples/GenericHost.DistanceMatrix -- "New York, NY" "Boston, MA"Omit the origin/destination args and it defaults to New York → Boston.
The API key resolves from (in order):
- The
GOOGLE_API_KEYenvironment variable (mapped intoGoogleMaps:ApiKeyinProgram.cs). - The
GoogleMaps:ApiKeyconfiguration value —appsettings.json, user secrets, or aGoogleMaps__ApiKeyenvironment variable.
AddGoogleMaps(IConfiguration)— bindsGoogleMapsClientOptions(the ambient API key) from a config section- Constructor injection of
IGoogleMapsClientinto a regular application service Host.CreateApplicationBuildergeneric-host wiring withIServiceCollectionandILogger<T>DistanceMatrixRequest/DistanceMatrixResponsewith requests that carry no API key — it comes from DI
The Minimal API sample uses the
AddGoogleMaps(Action<GoogleMapsClientOptions>)overload instead; this sample uses the configuration-binding overload.