Select to view content in your preferred language

FeatureLayer.FromJson thows exception

909
2
08-05-2011 11:02 AM
KirkKuykendall
Deactivated User
Not sure if this is a bug or maybe I'm just doing something wrong.  When I run this code with 2.2 ...
private void Test()
{
    WebClient wc = new WebClient();
    wc.OpenReadCompleted += (sender, args) =>
        {
            if (args.Error == null)
            {
                StreamReader reader = new StreamReader(args.Result);
                string json = reader.ReadToEnd();
                var fLayer = FeatureLayer.FromJson(json);
            }
            else
                Debug.WriteLine(args.Error.Message);
        };            

    string url = @"http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/0?f=pjson";
    wc.OpenReadAsync(new Uri(url));
}


I get this exception:
System.ArgumentException occurred
  Message=layerDefinition
Parameter name: Property was not found during JSON deserialization.
  StackTrace:
       at ESRI.ArcGIS.Client.FeatureLayer.FromDictionary(IDictionary`2 definition)
       at ESRI.ArcGIS.Client.FeatureLayer.FromJson(String json)
       at Viewer.Addins.Query.QueryControl.<Test>b__1(Object sender, OpenReadCompletedEventArgs args)
  InnerException: 


Any suggestions are greatly appreciated.

Thanks!
0 Kudos
2 Replies
KirkKuykendall
Deactivated User
I think I've found a workaround

private void Test()
{
    var fLayer = new FeatureLayer();
    fLayer.ID = "my flayer";
    fLayer.Url = @"http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/0";
    //fLayer.Url = @"http://www.badurl123.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/0;";
    fLayer.Initialized += (sender, args) =>
        {
            foreach (Field f in fLayer.LayerInfo.Fields)
                Debug.WriteLine(f.Name);
                    
        };
    fLayer.InitializationFailed += (sender, args) =>
        {
            Debug.WriteLine("init failed");
        };
    fLayer.Initialize();
}


However if I use badurl123.com, InitializationFailed is not called as I would expect.  Instead Initialized is called, which is not expected.
0 Kudos
DominiqueBroux
Esri Frequent Contributor
However if I use badurl123.com, InitializationFailed is not called as I would expect. Instead Initialized is called, which is not expected.


In case of failure, both events are fired with InitializationFailure set.
You should see both events after changing your code this way:
 
    fLayer.Initialized += (sender, args) =>
        {
            if (fLayer.InitializationFailure == null)
              foreach (Field f in fLayer.LayerInfo.Fields)
                  Debug.WriteLine(f.Name);                
        };
0 Kudos