public ServiceModule(IServiceMethodLocator serviceMethodLocator) { foreach (ServiceMethod serviceMethod in serviceMethodLocator.GetServiceMethods()) { ParameterInfo[] parameterInfos = serviceMethod.MethodInfo.GetParameters(); ServerPath serverPath = ServerPaths.GetServerPath(serviceMethod.AsyncServiceMethodInfo); Post[serverPath.SubPath, serverPath.FullPath, true] = async(x, ct) => { Task responseTask = (Task)serviceMethod.MethodInfo.Invoke( serviceMethod.Instance, parameterInfos.Select(CreateParameter).ToArray()); object result = await GetTaskResult(responseTask); Response response = JsonConvert.SerializeObject(result); response.ContentType = "application/json"; response.StatusCode = HttpStatusCode.OK; return(response); }; } }
public async Task <object> Call(AsyncServiceMethodInfo asyncServiceMethodInfo, CallParam[] callParams) { try { // Get the URI for the HTTP server call. ServerPath path = ServerPaths.GetServerPath(asyncServiceMethodInfo); using (var httpClient = new HttpClient()) { using (HttpContent content = HttpContentSupport.Create(callParams)) { HttpResponseMessage response = await httpClient.PostAsync(_rootUri + path, content).ConfigureAwait(false); if (!response.IsSuccessStatusCode) { throw new ServerCallException(asyncServiceMethodInfo, response.StatusCode); } using (Stream responseStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false)) { if (responseStream == null) { throw new ServerCallException(asyncServiceMethodInfo, HttpStatusCode.NoContent); } using (var reader = new StreamReader(responseStream, Encoding.UTF8)) { string responseJson = await reader.ReadToEndAsync().ConfigureAwait(false); return(JsonConvert.DeserializeObject(responseJson, asyncServiceMethodInfo.ReturnType)); } } } } } catch (Exception exception) { throw new ServerCallException(asyncServiceMethodInfo, "Exception during server method call", exception); } }