Select to view content in your preferred language

Rest SOE problem

1250
3
07-19-2012 10:07 AM
YanyiZhang
Deactivated User
Hi guys,
    I just start using SOE in ArcGIS Server 10.1 and find something strange here. When I create a RESTSOE project from Visual studio 2010 template, the handler is like this:
        private byte[] SampleOperHandler(NameValueCollection boundVariables,
                                                  JsonObject operationInput,
                                                      string outputFormat,
                                                      string requestProperties,
                                                  out string responseProperties)
        {
            responseProperties = null;

            string parm1Value;
            bool found = operationInput.TryGetString("parm1", out parm1Value);
            if (!found || string.IsNullOrEmpty(parm1Value))
                throw new ArgumentNullException("parm1");

            string parm2Value;
            found = operationInput.TryGetString("parm2", out parm2Value);
            if (!found || string.IsNullOrEmpty(parm2Value))
                throw new ArgumentNullException("parm2");

            JsonObject result = new JsonObject();
            result.AddString("parm1", parm1Value);
            result.AddString("parm2", parm2Value);

            return Encoding.UTF8.GetBytes(result.ToJson());
        }


After adding this soe file to ArcGIS Server and invoke the rest service at:  http://localhost:6080/arcgis/rest/services/demo/MapServer/exts/RestSOE/sampleOperation?parm1=1111&pa...
the soe will throw ArgumentNullException when it trying to retrieve parm1 value.
Then I change handler code to:

        private byte[] SampleOperHandler(NameValueCollection boundVariables,
                                                  JsonObject operationInput,
                                                      string outputFormat,
                                                      string requestProperties,
                                                  out string responseProperties)
        {
            responseProperties = null;

            object parm1Value;
            bool found = operationInput.TryGetObject("parm1", out parm1Value);
            if (!found || parm1Value == null || string.IsNullOrEmpty(parm1Value.ToString()))
                throw new ArgumentNullException("parm1");

            object parm2Value;
            found = operationInput.TryGetObject("parm2", out parm2Value);
            if (!found || parm2Value == null ||  string.IsNullOrEmpty(parm2Value.ToString()))
                throw new ArgumentNullException("parm2");

            JsonObject result = new JsonObject();
            result.AddString("parm1", parm1Value.ToString());
            result.AddString("parm2", parm2Value.ToString());

            string aaa;
            found = operationInput.TryGetString("parm1", out aaa);
            if (!found || string.IsNullOrEmpty(aaa))
            {
                result.AddString("parm1Exist", "False");
            }
            else
            {
                result.AddString("parm1Exist", "True");
            }

            return Encoding.UTF8.GetBytes(result.ToJson());
        }


then result json will be:
{
 "parm1": "1111",
 "parm2": "222",
 "parm1Exist": "False"
}


It looks like JsonObject.TryGetObject() function works fine while JsonObject.TryGetString() not working.
Am i right?
Tags (2)
0 Kudos
3 Replies
nicogis
MVP Alum
If you write 11111 the jsonobject (operationInput) passed to delegate this

{"parm2":1111,"parm1":222}


it's created number and not string so if you can have string (number integer) you test TryGetAsLong ....
0 Kudos
YanyiZhang
Deactivated User
Yes, you are right. If I changed parameter to 111abc then JsonObject.TryGetString() function works.
But how can I make JsonObject treat integer 1111 as a string "1111" if I'm directly invoke rest api via http? Do I have to use TryGetObject() function and then convert object to string?
Thanks!
0 Kudos
nicogis
MVP Alum
json isn't strongly typed so there isn't help of info it cannot know if it'string or number. (in soe you pass a list of parameters but not its type). you test before if it is a number.
0 Kudos