TXDataWebConnection first call get Exception "TXDataWebConnection is not connected"

Hi,

I'm testing a XData Service. I have the following code on a button click. Ths first time I click I get a Exception "TXDataWebConnection is not connected", later calls works ok. If I Set .Connected := false on method OnResult, I get intermitent exceptions->Exception, Ok, Exception, Ok....
try
XDataWebConnection1.URL := 'http://localhost/INTEC/ICSAT/xdata';
XDataWebConnection1.Connected := true;
XDataWebClient1.RawInvoke('IICSATService.EchoString', [sTemp], @OnResult);
except on E:Exception do
WebMemo2.Text := E.Message;
end;

Any hint?

Thanks in advace,

Omar Zelaya

I had fixed with the following code, but getting a Uncaught (in promise) TypeError.
try
try
Conn := TXDataWebConnection.Create(nil);
Conn.URL := 'http://localhost/INTEC/ICSAT/xdata';
Conn.OnRequest := XDataWebConnection1Request;
Client := TXDataWebClient.Create(nil);
Client.Connection := Conn;
await(Conn.OpenAsync);
Response := await(Client.RawInvokeAsync('IICSATService.EchoString', [sTemp]));
sTemp := string(TJSObject(Response.Result)['value']);
WebMemo2.Text := sTemp;
javascriptobject := TJSJSON.parse(sTemp);
Duca := TDUCA(javascriptobject);
Duca.Free;
except on E:Exception do
WebMemo2.Text := E.Message;
end;
finally
Client.Free;
Conn.Free;<--- Error
end;

Uncaught (in promise) TypeError: obj.$destroy is not a function
freeLoc rtl.js:448
TestXDataService Forms.Main.pas:244
WebButton1Click Forms.Main.pas:266
cb rtl.js:240
Click WEBLib.Controls.pas:1747
HandleDoClick WEBLib.Controls.pas:3744
cb rtl.js:236

Thanks in advance,

Omar Zelaya

What is XDataWebConnection1Request doing?
Is this still relying on Conn to be accessible as you might be destroying it here before this event handler is called?

Also, this looks suspicious:

javascriptobject := TJSJSON.parse(sTemp);
Duca := TDUCA(javascriptobject);
Duca.Free;

Usually you can't cast a raw Javascript object to a Pascal object and just call Free.
Can you try to remove such code and see if it helps?