Setting AttributeParameterValue.ParameterValue results in "Invalid type" exception

350
2
04-04-2018 08:43 AM
MichaelWidmer
New Contributor II

I'm trying to query a route for pedestrians using the ArcGIS Runtime SDK for .NET 100.2.0 and the ESRI World Routing service.

First I'm retreiving the supported travel modes using the travel modes service using a GeoprocessingTask. I'm about to parse the returned modes to find the "walking" mode, what works fine so far.

When I'm getting to parse and write the AttributeParameterValues for the travel mode object, I'm running into an exception when trying to set the ParameterValue-property of it:

foreach (var attributeValue in modes["attributeParameterValues"])
{
    string aName = Convert.ToString(attributeValue["attributeName"]);
    string pName = Convert.ToString(attributeValue["parameterName"]);
    string pValue = Convert.ToString(attributeValue["value"]);

    AttributeParameterValue attr = new AttributeParameterValue();
    attr.AttributeName = aName;
    attr.ParameterName = pName;
    attr.ParameterValue = pValue; // <-- here the exception occurs

    travelMode.AttributeParameterValues.Add(attr);
}

The value of "pValue" is a simple, unmodified string directly received from the above mentioned travel modes service (that reads e.g. "AVOID_MEDIUM"). Neither setting the value directly works.

I get an exception, stating that the value does not seem to conform to an expected type:

Invalid argument: Invalid element type.

The stacktrace reads as follows:

   bei Esri.ArcGISRuntime.ArcGISException.HandleCoreError(CoreError error, Boolean throwException)
   bei RuntimeCoreNet.GeneratedWrappers.Interop.CheckError(IntPtr errorHandle, Boolean throwOnFailure, GCHandle wrapperHandle)
   bei RuntimeCoreNet.GeneratedWrappers.CoreAttributeParameterValue.set_ParameterValue(CoreElement value)
   bei My.RoutingManager.<SetupTravelModeRouteParameters>d__20.MoveNext() in C:\source\RoutingManager.cs:Line 231.

The exception occurs for all of the various received values targeted to be set. Unfortunately the documentation does not provide any more information (the property itself being declared) about what type of value is expected for that part of the AttributeParameterValue. What kind of type do I need to set for "ParameterValue"?

0 Kudos
2 Replies
ThadTilton
Esri Contributor

Hey Michael - 

I suspect the problem is that you are reading the travel modes array for a different network than the one you are using for the `RouteTask`. The parameter values (such as "AVOID_MEDIUM") need to map to one of the values in the `RestrictionAttributes` for the `RouteTask` (you can check `RouteTaskInfo` to see a list of these).

Try code like the following to 1) create a route task and get its default parameters, 2) read the available travel modes for the network, 3) iterate all modes, and 4) find the mode for pedestrian travel ("WALK") and use it in the parameters.

// Create the RouteTask using the world routing service.
var routeTask = await RouteTask.CreateAsync(new Uri("http://route.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World"));
           
// Get the default parameters for the task (which uses driving as the travel mode).
var routeParams = await routeTask.CreateDefaultParametersAsync();

// Get the available travel modes from the RouteInfo. Iterate all modes.
var routeInfo = routeTask.RouteTaskInfo;
foreach (var mode in routeInfo.TravelModes)
{
    // Find the travel mode of type "WALK".
    if(mode.Type == "WALK")
    {
        // Assign this as the travel mode for the route parameters, then exit the loop.
        routeParams.TravelMode = mode;
        break;
    }
}

Hope that helps, happy routing! Thad

0 Kudos
dotMorten_esri
Esri Frequent Contributor

Was the value actually a string? Does it still happen if you change

string pValue = Convert.ToString(attributeValue["value"]);

to:

var pValue = attributeValue["value"];

0 Kudos