public async Task <TResult> InvokeFunctionAsync <TResult>(string name, params object[] parameters) { name.CheckArgument(nameof(name)); var invokeParam = new InvokeParam() { MethodName = name, }; invokeParam.SetParameters(parameters); using var client = GetClient(BaseUri); var jsonData = JsonSerializer.Serialize(invokeParam); var contentData = new StringContent(jsonData, Encoding.UTF8, MediaType); var response = await client.PostAsync($"{ExtUri}/CallFunction", contentData).ConfigureAwait(false); if (response.IsSuccessStatusCode) { var resultData = await response.Content.ReadAsStreamAsync().ConfigureAwait(false); var invokeResult = await JsonSerializer.DeserializeAsync <Transfer.InvokeTypes.InvokeReturnValue>(resultData, DeserializerOptions).ConfigureAwait(false); return(JsonSerializer.Deserialize <TResult>(invokeResult.JsonData)); } else { string errorMessage = $"{response.ReasonPhrase}: {await response.Content.ReadAsStringAsync().ConfigureAwait(false)}"; System.Diagnostics.Debug.WriteLine("{0} ({1})", (int)response.StatusCode, errorMessage); throw new AdapterException((int)response.StatusCode, errorMessage); } }
public async Task InvokeActionAsync(string name, params object[] parameters) { name.CheckArgument(nameof(name)); var invokeParam = new InvokeParam() { MethodName = name, }; invokeParam.SetParameters(parameters); using (var client = GetClient(BaseUri)) { string jsonData = JsonSerializer.Serialize(invokeParam); StringContent contentData = new StringContent(jsonData, Encoding.UTF8, MediaType); HttpResponseMessage response = await client.PostAsync($"{ExtUri}/CallAction", contentData).ConfigureAwait(false); if (response.IsSuccessStatusCode == false) { string errorMessage = $"{response.ReasonPhrase}: {await response.Content.ReadAsStringAsync().ConfigureAwait(false)}"; System.Diagnostics.Debug.WriteLine("{0} ({1})", (int)response.StatusCode, errorMessage); throw new AdapterException((int)response.StatusCode, errorMessage); } } }