public async Task HandleAsync <TCommand>(TCommand command) { Ensure.NotNull(command, "command"); Type commandType = command.GetType(); RouteDefinition route; if (routeTable.TryGet(commandType, out route)) { using (HttpClient httpClient = httpAdapter.PrepareHttpClient(route)) { // Prepare content and send request. ObjectContent objectContent = httpAdapter.PrepareObjectContent(command, route); HttpResponseMessage response = await httpAdapter.Execute(httpClient, objectContent, route); //TODO: Process HTTP status errors. return; } } throw Ensure.Exception.InvalidOperation("Unnable to preces command without registered URL route, command type is '{0}'.", commandType.FullName); }
public bool TryGet(Type inputType, out RouteDefinition route) { if (routeTable.TryGet(inputType, out route)) { // If URL in route is absolute, nothing is needed. Uri url; if (Uri.TryCreate(route.Url, UriKind.Absolute, out url)) { return(true); } if (route.Url.StartsWith("~/")) { string relativeUrl = route.Url.Substring(1); route = new RouteDefinition(baseUrl + relativeUrl, route.Method, route.Serialization); return(true); } } route = null; return(false); }
public async Task <TOutput> QueryAsync <TOutput>(IQuery <TOutput> query) { Ensure.NotNull(query, "query"); Type queryType = query.GetType(); RouteDefinition route; if (routeTable.TryGet(queryType, out route)) { using (HttpClient httpClient = httpAdapter.PrepareHttpClient(route)) { // Prepare content and send request. ObjectContent objectContent = httpAdapter.PrepareObjectContent(query, route); HttpResponseMessage response = await httpAdapter.Execute(httpClient, objectContent, route); // Parse output. TOutput output = await response.Content.ReadAsAsync <TOutput>(); return(output); } } throw Ensure.Exception.InvalidOperation("Unnable to preces query without registered URL route, query type is '{0}'.", queryType.FullName); }