Class GatewayClient
Dynamic proxy for the API Gateway. Service names are accessed as properties, and backend RPC methods are called dynamically on the returned ServiceClient.
client.Gateway.workmanager triggers TryGetMember(GetMemberBinder, out object?),
which creates or returns a cached ServiceClient for "workmanager".
service.ListWorkers() triggers TryGetMember(GetMemberBinder, out object?),
which returns a callable that invokes via the Gateway's InvokeJson RPC.
Provides service discovery, method listing, invocation, publishing, and streaming capabilities.
public class GatewayClient : DynamicObject, IDynamicMetaObjectProvider
- Inheritance
-
GatewayClient
- Implements
- Inherited Members
Examples
using var client = new ApiClient();
var workers = await client.Gateway.workmanager.ListWorkers();
// Equivalent to:
var workers = await client.InvokeAsync("workmanager", "ListWorkers");
Constructors
GatewayClient(GrpcChannel)
Creates a new GatewayClient with the specified gRPC channel.
public GatewayClient(GrpcChannel channel)
Parameters
channelGrpcChannelThe gRPC channel to use for communication.
Methods
DeleteStateAsync(string, string, bool, CancellationToken)
Deletes a state entry and broadcasts a delete event to subscribers asynchronously.
public Task<DeleteStateResponse> DeleteStateAsync(string service, string key, bool includeValue = false, CancellationToken cancellationToken = default)
Parameters
servicestringThe service name.
keystringThe state key.
includeValueboolIf true, the last value is included in the state change event payload.
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<DeleteStateResponse>
DeleteStateResponse with success status.
GetAllStateAsync(string, CancellationToken)
Gets all state key-value pairs asynchronously for a service.
public Task<Dictionary<string, string>> GetAllStateAsync(string service, CancellationToken cancellationToken = default)
Parameters
servicestringThe service name.
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<Dictionary<string, string>>
Dictionary of key-value pairs.
GetService(string)
Gets a client for a specific service.
public ServiceClient GetService(string name)
Parameters
namestringThe service name.
Returns
- ServiceClient
A ServiceClient for the service.
Remarks
ServiceClients are cached internally. Calling this method multiple times with the same service name returns the same cached instance.
GetStateAsync(string, string, CancellationToken)
Gets a single state value asynchronously by service and key.
public Task<(string Value, string Etag)> GetStateAsync(string service, string key, CancellationToken cancellationToken = default)
Parameters
servicestringThe service name.
keystringThe state key.
cancellationTokenCancellationTokenCancellation token.
Returns
InvokeAsync(string, string, Dictionary<string, object?>?)
Invokes a method on a service asynchronously.
public Task<Dictionary<string, object?>> InvokeAsync(string service, string method, Dictionary<string, object?>? requestData = null)
Parameters
servicestringThe service name.
methodstringThe method name.
requestDataDictionary<string, object>Optional request data as dictionary.
Returns
- Task<Dictionary<string, object>>
The response as a dictionary.
Remarks
The polymorphic Dictionary<string, object?>
payload is (de)serialized via Virtufin.Api.Client.GatewayClient.JsonOptions,
which sets an explicit DefaultJsonTypeInfoResolver
so reflection-based dispatch works regardless of the
process-wide IsReflectionEnabledByDefault flag.
See Virtufin.Api.Client.GatewayClient.JsonOptions for the AOT caveat.
Exceptions
- InvalidOperationException
Thrown when the invocation fails.
ListMethodsAsync(string)
Lists all methods for a given service asynchronously.
public Task<List<Dictionary<string, object?>>> ListMethodsAsync(string service)
Parameters
servicestringThe service name.
Returns
- Task<List<Dictionary<string, object>>>
List of method information dictionaries.
ListServicesAsync()
Lists all available services asynchronously.
public Task<List<string>> ListServicesAsync()
Returns
PublishEventAsync(string, byte[], Dictionary<string, string>?, CancellationToken)
Publishes an event to a topic asynchronously.
public Task<PublishResponse> PublishEventAsync(string topic, byte[] data, Dictionary<string, string>? metadata = null, CancellationToken cancellationToken = default)
Parameters
topicstringThe topic name.
databyte[]The event data as bytes.
metadataDictionary<string, string>Optional metadata dictionary.
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<PublishResponse>
PublishResponse with success status.
PublishWithResultAsync(string, byte[], string, TimeSpan?, Dictionary<string, string>?, string?, CancellationToken)
Publishes to a topic and waits for a correlated response on a reply topic. Implements the request-reply pattern over pub/sub using correlation IDs.
public Task<PubsubSubscribeResponse> PublishWithResultAsync(string topic, byte[] data, string replyTopic, TimeSpan? timeout = null, Dictionary<string, string>? metadata = null, string? correlationId = null, CancellationToken cancellationToken = default)
Parameters
topicstringThe topic to publish the request to.
databyte[]The request data as bytes.
replyTopicstringThe topic to listen for responses on.
timeoutTimeSpan?Maximum time to wait for a response (default: 30s).
metadataDictionary<string, string>Optional additional metadata to include.
correlationIdstringOptional correlation ID (auto-generated if null).
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<PubsubSubscribeResponse>
The correlated PubsubSubscribeResponse.
Exceptions
- TimeoutException
Thrown if no response arrives within the timeout.
SaveStateAsync(string, string, string, string?, bool, CancellationToken)
Saves state and broadcasts a change event to subscribers asynchronously.
public Task<SaveStateResponse> SaveStateAsync(string service, string key, string value, string? etag = null, bool includeValue = false, CancellationToken cancellationToken = default)
Parameters
servicestringThe service name.
keystringThe state key.
valuestringThe state value.
etagstringOptional etag for concurrency.
includeValueboolIf true, the value is included in the state change event payload.
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<SaveStateResponse>
SaveStateResponse with success status.
Subscribe(IEnumerable<string>?, IEnumerable<string>?, IEnumerable<string>?, CancellationToken)
Subscribes to events from specified services and topics.
public AsyncServerStreamingCall<TopicEventRequest> Subscribe(IEnumerable<string>? services = null, IEnumerable<string>? topics = null, IEnumerable<string>? eventTypes = null, CancellationToken cancellationToken = default)
Parameters
servicesIEnumerable<string>List of service names to subscribe to (empty for all).
topicsIEnumerable<string>List of topics to subscribe to (empty for all).
eventTypesIEnumerable<string>List of event types to subscribe to (empty for all).
cancellationTokenCancellationTokenCancellation token.
Returns
- AsyncServerStreamingCall<TopicEventRequest>
The async call handle. Dispose to cancel subscription.
SubscribeAsync(IStreamEventHandler, IEnumerable<string>?, IEnumerable<string>?, IEnumerable<string>?, CancellationToken)
Subscribes to events with a handler callback.
public Task SubscribeAsync(IStreamEventHandler handler, IEnumerable<string>? services = null, IEnumerable<string>? topics = null, IEnumerable<string>? eventTypes = null, CancellationToken cancellationToken = default)
Parameters
handlerIStreamEventHandlerHandler to receive events.
servicesIEnumerable<string>Optional service filter.
topicsIEnumerable<string>Optional topic filter.
eventTypesIEnumerable<string>Optional event type filter.
cancellationTokenCancellationTokenCancellation token.
Returns
- Task
Task that completes when subscription is cancelled.
SubscribeToTopic(string, CancellationToken)
Subscribes to a pub/sub topic and streams events.
public AsyncServerStreamingCall<PubsubSubscribeResponse> SubscribeToTopic(string topic, CancellationToken cancellationToken = default)
Parameters
topicstringThe topic name.
cancellationTokenCancellationTokenCancellation token.
Returns
- AsyncServerStreamingCall<PubsubSubscribeResponse>
Async streaming call for PubsubSubscribeResponse. Dispose to cancel.
TryGetMember(GetMemberBinder, out object?)
Dynamic property access returns a ServiceClient for the named service.
public override bool TryGetMember(GetMemberBinder binder, out object? result)
Parameters
binderGetMemberBinderresultobject
Returns
UnsubscribeFromTopicAsync(string, CancellationToken)
Unsubscribes from a pub/sub topic.
public Task<UnsubscribeResponse> UnsubscribeFromTopicAsync(string subscriptionId, CancellationToken cancellationToken = default)
Parameters
subscriptionIdstringThe subscription ID to cancel.
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<UnsubscribeResponse>
UnsubscribeResponse with success status.