JSON-RPC Async Request How-to?

Hi there,

what would be your recommendation to implement the async calls on the RO Services via json-rpc? Is there some built-in feature for that?

Thank you!

Best regards

You can use _Async/_AsyncEx services - they are designed for async calls

If I execute this request

{
	"version": "1.1",
	"method": "HelloWorldService_Async.Invoke_GetHelloWorld",
	"params": {}
}

I am getting an error

{
    "version": "1.1",
    "error": {
        "name": "JsonRPCError",
        "code": "1",
        "message": "Class factory for interface HelloWorldService_Async not found"
    }
}

What class factory should I take?

How can I check if an answer is received? Colud you write the JSON-RPC request for that?

Thanks!

pls review sample: json_async.zip (108.1 KB)

thanks for the sample. There is no problem to call the async service in a delphi application. How do I call the service async in a REST tool, e.g. in Postman?

you can’t call service method in async mode directly.

if you like to launch something async from 3rd party client like Postman, you have to add new special methods:

  • one method will launch something in background thread
  • [optionally] second method will check state of execution, i.e. completed or still running
  • [optionally] third method will return result of execution, if it is required

okay, that is what I wanted to hear. Thank you for your time and help!

I am trying to implement an async approach

function TService.Invoke_ServiceMethod(const AValue: string): string;
var
  TokenID: string;
begin
  TokenID := GUIDToString(Session.SessionID);

  Session.Values[AValue] := 'Some Session Value';

  FLongTask := TTask.Create(
   procedure()
  var
    anonSession: TROSession;
  begin
      anonSession := Session;
      Sleep(10000);
       anonSession.Values['hello'] := 'world';
        fClassFactory.TimeoutSession(ClientID);
    end);
  FLongTask.Start;

  Result := TokenID;
end;


initialization
  fClassFactory := TROPerClientClassFactory.Create(__ServiceName, Create_Service, TRORTTIInvoker, 691200);

finalization
  UnRegisterClassFactory((fClassFactory as IROClassFactory));
  fClassFactory := nil;
  1. What do you think about this approach?
  2. Do you have a recommendation for a better solution?
  3. Why does Session.Values[AValue] := ‘Some Session Value’ work? And anonSession.Values[‘hello’] := ‘world’ does’nt work! :frowning:

Thank you!!!

this one may not work correctly because, your session can be expired already.

You can try another code. this one will allow also call methods from another service, like

  sid := Session.SessionID;
  FLongTask := TTask.Create(
    procedure()
    var
      service: TSessionTypesService;
      instance: IUnknown;
      objactivation:IROObjectActivation;
    begin
      Sleep(10000);
      Service := TService.Create(nil);
      instance := Service as IUnknown;
      if Supports(Service, IROObjectActivation, objactivation) then begin
        objactivation.OnActivate(sid, nil);
        objactivation := nil;
      end;
      Service.Session.Values['hello'] := 'world';
      if Supports(instance, IROObjectActivation, objactivation) then begin
        objactivation.OnDeactivate(sid);
        objactivation := nil;
      end;
      instance := nil;
    end
    );
    FLongTask.Start;