/// <summary> /// Dispatch the requests and get the responses. /// </summary> /// <param name="serviceName">The service name to dispatch.</param> /// <param name="requests">The requests to handle.</param> /// <param name="cancellationToken">The cancellation token which can cancel this method.</param> /// <returns>The handled response.</returns> public async Task <JsonRpcResponse[]> DispatchRequestsAsync(string serviceName, JsonRpcRequest[] requests, CancellationToken cancellationToken = default) { var service = JsonRpcCallManager.GetService(serviceName); if (service == null) { throw new InvalidOperationException($"Service {serviceName} does not exist."); } if (requests.Length == 1) { var request = requests[0]; var response = await GetResponseAsync(service, request, cancellationToken).ConfigureAwait(false); return(new[] { response }); } //batch call. var responseList = new List <JsonRpcResponse>(); foreach (var request in requests) { var response = await GetResponseAsync(service, request, cancellationToken).ConfigureAwait(false); if (response != null) { responseList.Add(response); } } return(responseList.ToArray()); }
/// <summary> /// Get service's SMD data by service name. /// </summary> /// <param name="serviceName">The service name to handle.</param> /// <returns>The SMD data for the service.</returns> public Task <byte[]> GetServiceSmdData(string serviceName) { var service = JsonRpcCallManager.GetService(serviceName); if (service == null) { throw new InvalidOperationException($"Service {serviceName} does not exist."); } return(Task.FromResult(service.SmdData)); }
/// <summary> /// Register exist services which marked with RpcService attribute. /// </summary> private void RegisterRpcServices() { var types = Assembly.GetEntryAssembly()?.GetTypes(); if (types != null) { foreach (var type in types) { var serviceAttributes = type.GetCustomAttributes(typeof(RpcServiceAttribute), false); if (serviceAttributes.Length > 0) { var service = type.New(); JsonRpcCallManager.RegisterService(service); } } } }
/// <summary> /// Check whether the service exists. /// </summary> /// <param name="serviceName">The service name.</param> /// <returns>True exists otherwise false.</returns> public bool ServiceExists(string serviceName) { return(JsonRpcCallManager.GetService(serviceName) != null); }
/// <summary> /// Register a service with its interface. /// </summary> /// <typeparam name="T">The interface of the service.</typeparam> /// <param name="service">The service to register.</param> public void RegisterService <T>(T service) { JsonRpcCallManager.RegisterService(service); }