TWebXDataConnection not connected on first call

I have a WebCore app which calls an XData Server. All is working quite well at the moment except I have this issue.

I have a service defined on the XData Server

 [ServiceContract]
  ICredentialsService = Interface(IInvokable)
    ['{3FE40512-BF91-47FB-B6CC-263AFC0752CC}']
    Function SetUserCredentials(Const AUserId: Integer; Const AUserName: String; Const APassword: String;
      Const AAccessLevel: String; Const AStatus: string): String;
    Function RemoveCredentials(Const AUserId: Integer): boolean;
    Function CurrentUserAccess(Const AUserId: Integer): TJSONObject;
  End;

This is handled by a server

CredModule  := TXDataServerModule.Create(SystemSettings.BaseURI + CREDENTIAL_PATH, DataPool);
  CredModule.AddMiddleware(TCorsMiddleware.Create);
  CredModule.AddMiddleware(TJwtMiddleware.Create(JWT_SECRET, True));
  LoggingMiddleware := TLoggingMiddleware.Create(Logger);
  CredModule.AddMiddleware(LoggingMiddleware);
  SparkleServer.AddModule(CredModule);

Access to this is handled in the WebCore app with an object

 TCredentialService = Class
  Private
    FClient: TXDataWebClient;
  Protected

  Public
    Constructor Create; Reintroduce;
    Destructor Destroy; Override;
    procedure SetUserCredentials(Const AUserId: Integer; Const AUserName: String;
        Const APassword: String; Const AAccessLevel: String; Const AStatus: String;
        AOnSuccess: TOnSuccessProcEx; AOnError: TOnErrorProc);
    Procedure RemoveCredentials(Const AUserId: Integer; ASuccess: TOnSuccessProc; AError: TOnErrorProc);
    Procedure CurrentUserAccess(Const AUserId: Integer; AOnSuccess: TOnSuccessProcEx; AOnError: TOnErrorProc);
  End;

The problem I have is that when I first make a call - it is the CurrentUserAccess method it causes an exception "TXDataWebConnection is not connected". Recalling the method works without any problems. By the looks of it is in getting TXDataWebConnection .Model that cause the issue.

I have tried pre-opening the TXDataWebConnection, but that makes no difference.

I've probably missed something, so any ideas gratefully received

Connecting to the server is an asynchronous process. You didn't provide the code you used to connect and use your object, but if you do something like this:

XDataWebConnection1.Connected := True;
Service.RemoveCredentials();

The connection will be complete when RemoveCredentials is called. You must be sure XDataWebConnection1.Connected is True before using your service object.

thanks. I thought I was opening the connection well before the call. I have changed it now to test Connection.Connected and if it isn't pass across an OnSuccess procedure.

1 Like

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.