/// <summary> /// Gets the request parameters from attribute. /// </summary> /// <param name="interfaceMethod">The class verb.</param> /// <returns> /// The target uri to execute the get or invoke against. /// </returns> private static HttpRequestParameters CreateRequestParametersUsingReflection(MethodInfo interfaceMethod) { var restInvokeAttr = ExtractHttpRestInvokeAttribute(interfaceMethod); //Look for the custom attributes that inhert from IHttpMessageprocessingHandler var customHttpMessageHanders = (IHttpMessageProcessingHandler[])interfaceMethod.GetCustomAttributes(typeof(IHttpMessageProcessingHandler), false); var requestParameters = new HttpRequestParameters( restInvokeAttr.HttpMethod, new UriTemplate(restInvokeAttr.UriTemplate), GetImplemention <IRequestFormatter>(interfaceMethod), GetImplemention <IResponseFormatter>(interfaceMethod), customHttpMessageHanders); return(requestParameters); }
private HttpRequestMessage CreateHttpRequestMessage(HttpRequestParameters parameters, MethodInfo method, out CancellationToken cancellationToken, object[] methodArgumentsInOrder) { List <string> parameterNamesBoundToUri; var requestUri = parameters.BindUri(this._baseServiceUri, method, methodArgumentsInOrder, out parameterNamesBoundToUri, out cancellationToken); HttpRequestMessage requestMessage = new HttpRequestMessage(new HttpMethod(parameters.HttpMethod), requestUri); //The arguments when not used for uri binding either the request body or the cancellation token // the request body is serialized into the stream as the request body if (methodArgumentsInOrder.Length > parameterNamesBoundToUri.Count) { //This ordered as they appear in the method signature var parameterInfos = method.GetParameters(); for (int i = 0; i < parameterInfos.Length; i++) { var parameterName = parameterInfos[i].Name; if ( !parameterNamesBoundToUri.Any( paramName => paramName.Equals(parameterName, StringComparison.OrdinalIgnoreCase))) { if (parameterInfos[i].ParameterType != typeof(CancellationToken)) { //Pick the unbound argument for request stream data object dataToSerialize = methodArgumentsInOrder[i]; //serialize the data using the specified serializer into the request message requestMessage.Content = new ObjectContent(dataToSerialize.GetType(), dataToSerialize, parameters.RequestFormatter.RequestFormatter); } } } } foreach (var formatter in parameters.ResponseFormatter.ResponseFormatters) { foreach (var mediaType in formatter.SupportedMediaTypes) { requestMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(mediaType.MediaType)); } } return(requestMessage); }
/// <summary> /// Creates the HTTP client by chaining all the handlers found in the interface method attributes and the http /// client configuration. /// </summary> /// <param name="parameters">The parameters.</param> /// <returns>An instance of HttpClient.</returns> private HttpClient CreateHttpClient(HttpRequestParameters parameters) { HttpMessageHandler rootHandler = this.Configuration.RootHandler; if (parameters.CutomMessageProcessingHandlers.Any()) { IList <DelegatingHandler> handlers = parameters.CutomMessageProcessingHandlers.Select(c => c.CreateHandler()).ToList(); for (int i = 0; i < handlers.Count - 1; i++) { handlers[i].InnerHandler = handlers[i + 1]; } handlers.Last().InnerHandler = this.Configuration.RootHandler; rootHandler = handlers.First(); } else { rootHandler = this.Configuration.RootHandler; } return(new HttpClient(rootHandler, false)); }