AnsweredAssumed Answered

OnlineRouteTask.SolveAsync throws an exception

Question asked by olejuul on Jun 26, 2018
Latest reply on Aug 20, 2018 by olejuul

I have problem when I use the Online Rote tak to find the shortest driving distance.

 

The OnlineRouteTask class throws an exception 'System.ArgumentNullException: A value can not be null' in the function 'SolveAsync'. I have tried to set/change all the parameters in the call with no luck.

It used to work, so I am a little puzzled.

 

Here is my code:

async Task<double> FindDistance(double fromLat, double fromLon, double toLat, double toLon)
{
double distance = -1;
try
{
const string RouteUrl = "http://route.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World";

var fromPoint = new MapPoint(fromLon, fromLat, SpatialReferences.Wgs84);
var fromSym = new SimpleMarkerSymbol { Style = SimpleMarkerStyle.X, Size = 10, Color = Colors.Green };
var fromMapGraphic = new Graphic { Geometry = fromPoint, Symbol = fromSym };

var toPoint = new MapPoint(toLon, toLat, SpatialReferences.Wgs84);
var toSym = new SimpleMarkerSymbol { Style = SimpleMarkerStyle.X, Size = 10, Color = Colors.GreenYellow };
var toMapGraphic = new Graphic { Geometry = toPoint, Symbol = toSym };

var routeTask = new OnlineRouteTask(new Uri(RouteUrl));
var routeParams = await routeTask.GetDefaultParametersAsync();
//routeParams.OutSpatialReference = ihmMapView.SpatialReference;
//routeParams.DirectionsLengthUnit = LinearUnits.Kilometers;
routeParams.ReturnDirections = false;
routeParams.OutputLines = OutputLine.None;
//routeParams.ReturnPointBarriers = true;
//routeParams.FindBestSequence = true;
var restrictions = new List<string>();
restrictions.Add("Avoid Ferries");
restrictions.Add("Driving an Emergency Vehicle");
routeParams.RestrictionAttributeNames = restrictions;

var stopGraphics = new List<Graphic>();
stopGraphics.Add(fromMapGraphic);
stopGraphics.Add(toMapGraphic);
routeParams.SetStops(stopGraphics);

var routeResult = await routeTask.SolveAsync(routeParams); <--- Exception is thrown here
foreach (var de in routeResult.Routes[0].TotalCosts)
{
if (de.Key == "Kilometers")
{
distance = de.Value;
break;
}
}
}
catch (Exception e)
{
log.Error(e);
}
return distance;
}

 

The Exception:

ERROR - System.ArgumentNullException: A value can not be null.
Parameternavn: source
ved System.Linq.Enumerable.Where[TSource](IEnumerable`1 source, Func`2 predicate)
ved Esri.ArcGISRuntime.Tasks.NetworkAnalyst.NetworkDescription.GetTimeAndLengthAttributes(NAParameters parameters, String& timeAttribute, String& lengthAttribute)
ved Esri.ArcGISRuntime.Tasks.NetworkAnalyst.NAParameters.GetParameters(NetworkDescription networkDescription, Boolean autoNormalize)
ved Esri.ArcGISRuntime.Tasks.NetworkAnalyst.RouteParameters.GetParameters(NetworkDescription networkDescription, Boolean autoNormalize)
ved Esri.ArcGISRuntime.Tasks.NetworkAnalyst.OnlineRouteTask.<SolveRequest>d__17.MoveNext()
--- Afslutningen på staksporingen fra den tidligere placering, hvor undtagelsen blev udløst ---
ved System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
ved System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
ved Esri.ArcGISRuntime.Tasks.NetworkAnalyst.OnlineRouteTask.<Solve>d__12.MoveNext()
--- Afslutningen på staksporingen fra den tidligere placering, hvor undtagelsen blev udløst ---
ved System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
ved System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
ved System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
ved InformiGis.IhmMapControl.<FindDistance>d__106.MoveNext() <--- This is my function

 

Here is my callback for User/Password (it is a valid account, but it never gets called)

 

async Task<Credential> CreateHardCodedCredentialAsync(CredentialRequestInfo requestInfo)
{
Credential cred = null;
try
{
var options = new GenerateTokenOptions();
options.TokenAuthenticationType = TokenAuthenticationType.ArcGISToken;
cred = await IdentityManager.Current.GenerateCredentialAsync(requestInfo.ServiceUri, ArcGisUser, ArcGisPassword, options);
}
catch (Exception ex)
{
log.Error(ex);
}
return cred;
}

Outcomes