Jump to content

callRemote always returns nil


arkus

Recommended Posts

Hi all,

I've got a pretty unique situation but hear me out! I'm using .NET's WCF to host a web service that accepts and responds to calls wrapped in JSON. For security reasons I can't give you the address of the server (it doesn't respond to requests from off-box) but suffice to say I can send it a string of JSON (using callRemote), it does some work and it returns a JSON object (not an array of objects - this is where I think the problem may lie).

I'm sending in a string which looks like this:

[ { "someField": "ab", "someOtherField": "ab", "anotherField": "{5B4904C9-4D95-465B-A23C-A79349323DA2}" } ] 

The service does some work server side and populates an instance of a simple class with a few string, Guid and string array values, which are then serialized as JSON and passed back to MTA. An example response looks like this:

{"fieldA":0,"arrayField":["someValue", "someOtherValue"],"uniqueKey":"yetAnotherValue"} 

This is, according to various sources, a valid JSON string - indeed, it passes validation at JSONlint.com - but it doesn't seem to be passed back to my callback method correctly. I'm wondering if this is because it's not an array, like MTA sends it's requests in, but rather a single object. The only response I seem to get is that result==nil.

The method I'm using to hit the WCF service is as follows:

function doCallRemote(entry) 
    callRemote("http://someaddress/", myCallbackFunction, entry ) 
end 
  
function myCallbackFunction(result) 
    if result and result ~="ERROR" then 
        --all good 
    elseif result and result == "ERROR" then 
        outputChatBox(result.." , "..errorcode) 
    elseif result == nil then 
        outputChatBox("result: nil") 
    end 
end 
  

Can anyone spot any obvious omissions, errors or lack of knowledge as to why I can't get my JSON object result passed back to my callback function? Any help would be gratefully received.

Daniel

Link to comment

We've had some reports of this a while back, and I did some tests and couldn't reproduce it.

I think your problem may be that you aren't returning an array. From looking quickly at the PHP SDK and the MTA code (here) it seems that this is the only way it will work.

At it's simplest, you can just wrap your JSON string in [ and ] which should fix it.

Let me know how you get on. If you can get this to work, we'd be grateful if you could share an example on our wiki for other users.

Edit: I've updated the wiki page for callRemote to make it a bit clearer.

Link to comment

Hi eAi,

Thanks for your response. I can certainly look into adding [] around my response (but this is a WCF service which is serializing the response automatically, I think I'll have a job on).

Is this, however, a bug in MTA? I am returning a valid JSON object tree which MTA cannot parse because it is expecting an array back. Is there scope here for a bug fix?

Daniel

EDIT:

I've made my WCF service return a List<> containing my response object which, as you would expect, is serialized to JSON as an array. MTA can parse this correctly, which suggests a bug in that MTA cannot accept a single object as a response from a remote server but can accept an array. I expect the same is also true as MTA always sends the arguments to a remote call as an array. Whilst it's not "wrong", it's not ideal - I'd be interested to find out if this can be resolved. I'm prepared to provide access to a WCF service to assist, if required.

Link to comment

Well... It could be 'fixed', yes, but the reason it works as it does it that the callback functions can take multiple arguments. Each element of the JSON array you return over HTTP becomes an argument of the callback function.

I guess we could have a special-case for where the JSON element provided isn't an array and treat that as the first argument to the callback function. I'd avoid doing this - it's provides some convenience for your case, but I think confuses matters and makes the outcome potentially ambiguous - what if you have something that sometimes returns a List<> and sometimes a Dictionary<>? In the first case, that would cause each list item to get passed to the callback function as separate argument, whereas the second would result in a table being passed as the only argument. This is inconsistent and potentially very confusing.

One thing I think would be good to have would be better reporting of incorrect data so that this is easy to fix. I'm not sure anyone else will do this, but you're more than welcome to submit a patch - it sounds like you'd be capable! We're always very keen to get more people actively involved in fixing bugs!

Link to comment

Hi eAi,

Alas, I'd like to contribute a patch but my C/C++ is severely lacking (as in I wouldn't know where to start!). Sit me down in front of C#, on the other hand, and I'm quite at home!

I understand your argument that multiple arguments can be passed by using an array, but additionally could these also form an anonymous class (i.e. one with no signature) like is being returned in my second signature above? This resulting object could be passed back as the parameter into the callback and then allow the use of property accessors to get at each of the properties returned by the remote service.

Link to comment

It doesn't seem that much of a burden to expect to the web service developer to wrap returns in an array - in your case this can be a List<>. I don't really know what your server-side code is though, feel free to post a snippet or PM it to me.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...