using System.Net.Http.Json;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
public class OsrmResponse
public string code { get; set; }
public Matching[] matchings { get; set; }
public Leg[] legs { get; set; }
public Annotation annotation { get; set; }
public long[] nodes { get; set; }
static async Task Main(string[] args)
const string HttpClientMapMatch = "HttpClientMapMatch";
const string OsrmUri = "10.757938,52.437444;10.764379,52.437314;10.770562,52.439067;10.773268,52.436633?overview=simplified&radiuses=50;50;50;50&generate_hints=false&skip_waypoints=true&gaps=ignore&annotations=nodes&geometries=geojson";
ServiceProvider serviceProvider = new ServiceCollection()
.AddHttpClient(HttpClientMapMatch, httpClient =>
httpClient.BaseAddress = new Uri("https://router.project-osrm.org/match/v1/driving/");
}).Services.BuildServiceProvider();
IHttpClientFactory? httpClientFactory = serviceProvider.GetService<IHttpClientFactory>();
HttpClient? httpClient = httpClientFactory?.CreateClient(HttpClientMapMatch);
Console.WriteLine("Error httpClient is null");
HttpResponseMessage response = await httpClient.GetAsync(OsrmUri);
if (response.IsSuccessStatusCode)
string stringData = await response.Content.ReadAsStringAsync();
JsonSerializerOptions options = new()
PropertyNameCaseInsensitive = true,
OsrmResponse? osrmResponse = JsonSerializer.Deserialize<OsrmResponse>(stringData, options);
PrintNodes(osrmResponse);
OsrmResponse? osrmResponse2 = await httpClient.GetFromJsonAsync<OsrmResponse>(OsrmUri);
PrintNodes(osrmResponse2);
private static void PrintNodes(OsrmResponse? osrmResponse)
Console.WriteLine($"osrmResponse: {JsonSerializer.Serialize(osrmResponse)}\n");
if (osrmResponse?.code == "Ok")
foreach (Matching matching in osrmResponse.matchings)
foreach (Leg leg in matching.legs)
Console.WriteLine($"nodes: {JsonSerializer.Serialize(leg.annotation.nodes)}\n");