private async Task <TResponse> SendAsync <TResponse>(RequestWrapper <TResponse> request, string?route = null) { try { string requestJson = request.Serialize(this.requestSerializer); Uri uri = new Uri(this.BaseUrl, route); List <RpcRequest> requests = request.GetRequests(); var requestContext = new RequestEventContext(route, requests, requestJson); ResponseEventContext?responseContext = null; if (this.Events.OnRequestStartAsync != null) { await this.Events.OnRequestStartAsync(requestContext); } List <RpcResponse>?responses = null; string responseJson; Exception? error = null; responseJson = await this.transportClient.SendRequestAsync(uri, requestJson); try { if (string.IsNullOrWhiteSpace(responseJson)) { throw new RpcClientParseException("Server did not return a rpc response, just an empty body."); } try { TResponse returnValue; (responses, returnValue) = request.Deserialize(responseJson, this.requestSerializer); responseContext = new ResponseEventContext(responseJson, responses); return(returnValue); } catch (Exception ex) { error = ex; throw new RpcClientParseException($"Unable to parse response from server: '{responseJson}'", ex); } } finally { if (this.Events.OnRequestCompleteAsync != null) { if (responseContext == null) { responseContext = new ResponseEventContext(responseJson, responses, error); } await this.Events.OnRequestCompleteAsync(responseContext, requestContext); } } } catch (Exception ex) when(!(ex is RpcClientException) && !(ex is RpcException)) { throw new RpcClientUnknownException("Error occurred when trying to send rpc request(s)", ex); } }