Page 1 of 1
Dima C# Sample
Posted: Thu May 14, 2020 8:47 am
by HarryM
Hi,
I'm new to xServer and seek for a working C# sample code which shows who to deal with xDima.
The sample on
https://xserver2-europe-eu-test.cloud.p ... ntents.htm doesn't work.
I.e. DistanceMatrixContentsOptions, GetDistanceMatrixByLocationsRequest are not found,
createDistanceMatrixResponse distanceMatrixResponse = xdima.createDistanceMatrix(createDistanceMatrixRequest); drops a NullReference error.
Pleae help!
Harry
Re: Dima C# Sample
Posted: Fri May 15, 2020 6:10 am
by HarryM
Hello,
after struggeling around, I found an other sample using xServer, but the DistanceMatrixRespone.Contents are always NULL. How I understand there should be the response matrix. What's wrong?
Please help!
Harry
Code: Select all
using System;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net;
using XServerClient;
using System.Globalization;
using XServerClient.Models;
using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
namespace XServerJson
{
class Program
{
static void Main()
{
ExecAsync().Wait();
Console.ReadKey();
}
static async Task ExecAsync()
{
// initialize client for xserver-internet
var xServer = new XServer(
new Uri("https://xserver2-europe-eu-test.cloud.ptvgroup.com"), new HttpClientHandler
{
Credentials = new NetworkCredential
{
Password = "<TOKEN>",
UserName = "xtok"
}
});
try
{
CreateDistanceMatrixRequest createDistanceMatrixRequest = constructCreateDistanceMatrixRequest();
var job = await xServer.StartCreateDistanceMatrixAsync(createDistanceMatrixRequest);
var id = job.Id;
while (job.Status == JobStatus.QUEUING || job.Status == JobStatus.RUNNING)
{
await Task.Delay(1000);
job = await xServer.WatchJobAsync(new WatchRequest { Id = id });
}
if (job.Status == JobStatus.SUCCEEDED)
{
var result = await xServer.FetchDistanceMatrixResponseAsync(new JobRequest { Id = id });
DistanceMatrixContents con = ((DistanceMatrixResponse)result).Contents;
if (con == null)
{
Console.WriteLine("NULL");
}
else
{
Console.WriteLine($@"{con}");
}
}
else
{
Console.WriteLine($@"Failed with status: {job.Status}");
}
await xServer.DeleteJobAsync(new JobRequest { Id = job.Id });
}
catch (XServerErrorException ex)
{
Console.WriteLine($"Request failed with error: {ex.Response.Content}");
}
finally
{
xServer.Dispose();
}
}
private static CreateDistanceMatrixRequest constructCreateDistanceMatrixRequest()
{
List<RouteLocation> startLocations = new List<RouteLocation>();
List<RouteLocation> destLocations = new List<RouteLocation>();
startLocations.Add(createOffRoadCoordinate(10.7134, 49.7519));
startLocations.Add(createOffRoadCoordinate(11.0167, 49.8));
startLocations.Add(createOffRoadCoordinate(10.8886, 50.015));
startLocations.Add(createOffRoadCoordinate(11.9305, 51.0801));
destLocations.Add(createOffRoadCoordinate(11.58198, 48.135125));
CreateDistanceMatrixRequest createDistanceMatrixRequest = new CreateDistanceMatrixRequest
{
StartLocations = startLocations.ToArray(),
DestinationLocations = destLocations.ToArray(),
StoredProfile = "truck40t"
};
return createDistanceMatrixRequest;
}
public static Coordinate createCoordinate(double x, double y)
{
Coordinate coordinate = new Coordinate
{
X = x,
Y = y
};
return coordinate;
}
public static RouteLocation createOffRoadCoordinate(double x, double y)
{
OffRoadRouteLocation offRoadRouteLocation = new OffRoadRouteLocation
{
OffRoadCoordinate = createCoordinate(x, y),
};
return offRoadRouteLocation;
}
public static RouteLocation createOnRoadCoordinate(double x, double y)
{
OnRoadRouteLocation onRoadRouteLocation = new OnRoadRouteLocation
{
Coordinate = createCoordinate(x, y)
};
return onRoadRouteLocation;
}
}
}
Re: Dima C# Sample
Posted: Fri May 15, 2020 6:55 am
by Joost
You are using CreateDistanceMatrixRequest . This is designed for calculating a dima and leave it on the server so other xServer (like xTour and xCluster) can use it. If you want to get the result you have to use the derived type CreateAndGetDistanceMatrixRequest as request.
Keep in mind that CreateAndGetDistanceMatrixRequest does not leave the dima on the server. If you need the dima to persist but still want the content you can extend your current code with a getDistanceMatrix folow-up.
Re: Dima C# Sample
Posted: Fri May 15, 2020 7:59 am
by HarryM
Thank you!
Now it works ...