diff --git a/c-sharp/MessageTransports/PapiClient.cs b/c-sharp/MessageTransports/PapiClient.cs
index fd30453252..2a3d470fed 100644
--- a/c-sharp/MessageTransports/PapiClient.cs
+++ b/c-sharp/MessageTransports/PapiClient.cs
@@ -185,6 +185,35 @@ await _webSocket.CloseAsync(
}
}
+ ///
+ /// Send a request to the server. The response (if any) will be handled asynchronously by calling .
+ ///
+ /// Type of request intended for the server
+ /// If the object is a , it will be used as-is in the request.
+ /// Otherwise the object will be serialized into a then sent.
+ /// Callback for the response to this request being sent.
+ /// The first argument indicates whether the request was successful.
+ /// The second argument is the optional contents of the response message.
+ public virtual void SendRequest(
+ string requestType,
+ object requestContents,
+ Action responseCallback
+ )
+ {
+ ObjectDisposedException.ThrowIf(_isDisposed, this);
+
+ int requestId = Interlocked.Increment(ref _nextRequestId);
+ var requestMessage =
+ (requestContents is JsonElement jse)
+ ? new MessageRequest(requestType, requestId, jse)
+ : new MessageRequest(requestType, requestId, requestContents);
+ _messageHandlersForMyRequests[requestId] = new MessageHandlerResponse(
+ requestMessage,
+ responseCallback
+ );
+ QueueOutgoingMessage(requestMessage);
+ }
+
///
/// Register a request handler with the server.
///
@@ -205,14 +234,9 @@ public virtual async Task RegisterRequestHandler(
TaskCompletionSource registrationSource = new();
using Task registrationTask = registrationSource.Task;
- var requestMessage = new MessageRequest(
+ SendRequest(
"server:registerRequest",
- Interlocked.Increment(ref _nextRequestId),
- new object[] { requestType, _clientId }
- );
-
- _messageHandlersForMyRequests[requestMessage.RequestId] = new MessageHandlerResponse(
- requestMessage,
+ new object[] { requestType, _clientId },
(bool success, object? _) =>
{
if (!success)
@@ -237,8 +261,6 @@ public virtual async Task RegisterRequestHandler(
}
);
- QueueOutgoingMessage(requestMessage);
-
var timeout = TimeSpan.FromMilliseconds(responseTimeoutInMs);
if (!await IsTaskCompleted(registrationTask, timeout, _cancellationToken))
{
diff --git a/c-sharp/Messages/MessageRequest.cs b/c-sharp/Messages/MessageRequest.cs
index 0f990ea95d..674073c892 100644
--- a/c-sharp/Messages/MessageRequest.cs
+++ b/c-sharp/Messages/MessageRequest.cs
@@ -27,6 +27,14 @@ public MessageRequest(string requestType, int requestId, object contents)
Contents = JsonSerializer.SerializeToElement(contents);
}
+ public MessageRequest(string requestType, int requestId, JsonElement contents)
+ : base(MessageType.REQUEST)
+ {
+ RequestType = requestType;
+ RequestId = requestId;
+ Contents = contents;
+ }
+
public string RequestType { get; set; }
public int RequestId { get; set; }