/// <summary> /// Generates an HttpRequestMessage from the BatchRequest. /// </summary> private static HttpRequestMessage GenerateHttpRequest(BatchRequest batchRequest) { HttpRequestMessage requestMessage = null; Uri uri = null; // Since we aren't directly using the Protocol Layer to send the request, we have to extract the pieces to create the signed web request // that we can convert into a format compatible with the HTTP Recorder. MethodInfo getResourceMethod = batchRequest.GetType().GetMethod("GetResourceUri", BindingFlags.NonPublic | BindingFlags.Instance); if (getResourceMethod != null) { uri = getResourceMethod.Invoke(batchRequest, null) as Uri; } MethodInfo createWebRequestMethod = batchRequest.GetType().GetMethod("CreateWebRequest", BindingFlags.NonPublic | BindingFlags.Instance); if (createWebRequestMethod != null && uri != null) { HttpWebRequest webRequest = createWebRequestMethod.Invoke(batchRequest, new object[] { uri, null, null }) as HttpWebRequest; if (webRequest != null) { batchRequest.AuthenticationHandler.SignRequest(webRequest, null); // Convert the signed HttpWebRequest into an HttpRequestMessage for use with the HTTP Recorder requestMessage = new HttpRequestMessage(new HttpMethod(webRequest.Method), webRequest.RequestUri); foreach (var header in webRequest.Headers) { string key = header.ToString(); string value = webRequest.Headers[key]; requestMessage.Headers.Add(key, value); } } } return(requestMessage); }
/// <summary> /// Serializes an HttpWebRespone and response body into a BatchResponse /// </summary> private static object GenerateBatchResponse(BatchRequest batchRequest, HttpWebResponse webResponse, Stream responseBody) { object batchResponse = null; MethodInfo createCommandMethod = batchRequest.GetType().GetMethod("CreateRestCommand", BindingFlags.NonPublic | BindingFlags.Instance); if (createCommandMethod != null) { object command = createCommandMethod.Invoke(batchRequest, null); FieldInfo postProcessField = command.GetType().GetField("PostProcessResponse", BindingFlags.Public | BindingFlags.Instance); if (postProcessField != null) { Delegate postProcessDelegate = postProcessField.GetValue(command) as Delegate; if (postProcessDelegate != null) { FieldInfo responseStreamField = command.GetType().GetField("ResponseStream", BindingFlags.Public | BindingFlags.Instance); if (responseStreamField != null) { responseStreamField.SetValue(command, responseBody); } FieldInfo destinationStreamField = command.GetType().GetField("DestinationStream", BindingFlags.Public | BindingFlags.Instance); if (destinationStreamField != null) { Stream destinationStream = destinationStreamField.GetValue(command) as Stream; if (destinationStream != null) { responseBody.CopyTo(destinationStream); } } batchResponse = postProcessDelegate.DynamicInvoke(command, webResponse, null, null); } } } return(batchResponse); }
public static BaseClientService GetService(this BatchRequest thisBatchRequest) { var field = thisBatchRequest.GetType().GetField("service", BindingFlags.Instance | BindingFlags.NonPublic); var service = (BaseClientService)field?.GetValue(thisBatchRequest); return(service); }
/// <summary> /// Serializes an HttpWebRespone and response body into a BatchResponse /// </summary> private static object GenerateBatchResponse(BatchRequest batchRequest, HttpWebResponse webResponse, Stream responseBody) { object batchResponse = null; Type requestBaseType = batchRequest.GetType().BaseType; if (requestBaseType != null) { Type batchResponseType = requestBaseType.GetGenericArguments()[0]; MethodInfo processMethod = batchResponseType.GetMethod("ProcessResponse", BindingFlags.NonPublic | BindingFlags.Instance); ConstructorInfo constructor = batchResponseType.GetConstructor(new Type[] { }); if (constructor != null) { batchResponse = constructor.Invoke(null); if (processMethod != null) { processMethod.Invoke(batchResponse, new object[] { webResponse, responseBody, null }); } } } return(batchResponse); }
/// <summary> /// Generates an HttpRequestMessage from the BatchRequest. /// </summary> private static HttpRequestMessage GenerateHttpRequest(BatchRequest batchRequest) { HttpRequestMessage requestMessage = null; Uri uri = null; // Since we aren't directly using the Protocol Layer to send the request, we have to extract the pieces to create the signed web request // that we can convert into a format compatible with the HTTP Recorder. MethodInfo getResourceMethod = batchRequest.GetType().GetMethod("GetResourceUri", BindingFlags.NonPublic | BindingFlags.Instance); if (getResourceMethod != null) { uri = getResourceMethod.Invoke(batchRequest, null) as Uri; } // Get the generated HttpWebRequest from the BatchRequest MethodInfo createCommandMethod = batchRequest.GetType().GetMethod("CreateRestCommand", BindingFlags.NonPublic | BindingFlags.Instance); if (createCommandMethod != null) { object command = createCommandMethod.Invoke(batchRequest, null); FieldInfo buildRequestField = command.GetType().GetField("BuildRequestDelegate", BindingFlags.Public | BindingFlags.Instance); if (buildRequestField != null) { PropertyInfo currentResultProperty = command.GetType().GetProperty("CurrentResult", BindingFlags.NonPublic | BindingFlags.Instance); if (currentResultProperty != null) { currentResultProperty.SetValue(command, new RequestResult()); } Delegate buildRequest = buildRequestField.GetValue(command) as Delegate; if (buildRequest != null) { HttpWebRequest webRequest = buildRequest.DynamicInvoke(uri, null, false, null, null) as HttpWebRequest; if (webRequest != null) { // Delete requests set a Content-Length of 0 with the HttpRequestMessage class for some reason. // Add in this header before signing the request. if (webRequest.Method == "DELETE") { webRequest.ContentLength = 0; } // Sign the request to add the Authorization header batchRequest.AuthenticationHandler.SignRequest(webRequest, null); // Convert the signed HttpWebRequest into an HttpRequestMessage for use with the HTTP Recorder requestMessage = new HttpRequestMessage(new HttpMethod(webRequest.Method), webRequest.RequestUri); foreach (var header in webRequest.Headers) { string key = header.ToString(); string value = webRequest.Headers[key]; if (string.Equals(key, "Content-Type")) { // Copy the Content to the HttpRequestMessage FieldInfo streamField = command.GetType().GetField("SendStream", BindingFlags.Public | BindingFlags.Instance); if (streamField != null) { Stream contentStream = streamField.GetValue(command) as Stream; if (contentStream != null) { MemoryStream memStream = new MemoryStream(); contentStream.CopyTo(memStream); memStream.Seek(0, SeekOrigin.Begin); requestMessage.Content = new StreamContent(memStream); // Add Content-Type header to the HttpRequestMessage // Use a custom class to force the proper formatting of the Content-Type header. requestMessage.Content.Headers.ContentType = new JsonOdataMinimalHeader(); requestMessage.Content.Headers.ContentLength = webRequest.ContentLength; } } } else { requestMessage.Headers.Add(key, value); } } } } } } return(requestMessage); }
/// <summary> /// Serializes an HttpWebRespone and response body into a BatchResponse /// </summary> private static object GenerateBatchResponse(BatchRequest batchRequest, HttpWebResponse webResponse, Stream responseBody) { object batchResponse = null; MethodInfo createCommandMethod = batchRequest.GetType().GetMethod("CreateRestCommand", BindingFlags.NonPublic | BindingFlags.Instance); if (createCommandMethod != null) { object command = createCommandMethod.Invoke(batchRequest, null); FieldInfo preProcessField = command.GetType().GetField("PreProcessResponse", BindingFlags.Public | BindingFlags.Instance); if (preProcessField != null) { Delegate preProcessDelegate = preProcessField.GetValue(command) as Delegate; if (preProcessDelegate != null) { preProcessDelegate.DynamicInvoke(command, webResponse, null, null); } } FieldInfo postProcessField = command.GetType().GetField("PostProcessResponse", BindingFlags.Public | BindingFlags.Instance); if (postProcessField != null) { Delegate postProcessDelegate = postProcessField.GetValue(command) as Delegate; if (postProcessDelegate != null) { FieldInfo responseStreamField = command.GetType().GetField("ResponseStream", BindingFlags.Public | BindingFlags.Instance); if (responseStreamField != null) { responseStreamField.SetValue(command, responseBody); } FieldInfo destinationStreamField = command.GetType().GetField("DestinationStream", BindingFlags.Public | BindingFlags.Instance); if (destinationStreamField != null) { Stream destinationStream = destinationStreamField.GetValue(command) as Stream; if (destinationStream != null) { responseBody.CopyTo(destinationStream); } } batchResponse = postProcessDelegate.DynamicInvoke(command, webResponse, null, null); } } } return batchResponse; }
/// <summary> /// Generates an HttpRequestMessage from the BatchRequest. /// </summary> private static HttpRequestMessage GenerateHttpRequest(BatchRequest batchRequest) { HttpRequestMessage requestMessage = null; Uri uri = null; // Since we aren't directly using the Protocol Layer to send the request, we have to extract the pieces to create the signed web request // that we can convert into a format compatible with the HTTP Recorder. MethodInfo getResourceMethod = batchRequest.GetType().GetMethod("GetResourceUri", BindingFlags.NonPublic | BindingFlags.Instance); if (getResourceMethod != null) { uri = getResourceMethod.Invoke(batchRequest, null) as Uri; } // Get the generated HttpWebRequest from the BatchRequest MethodInfo createCommandMethod = batchRequest.GetType().GetMethod("CreateRestCommand", BindingFlags.NonPublic | BindingFlags.Instance); if (createCommandMethod != null) { object command = createCommandMethod.Invoke(batchRequest, null); FieldInfo buildRequestField = command.GetType().GetField("BuildRequestDelegate", BindingFlags.Public | BindingFlags.Instance); if (buildRequestField != null) { PropertyInfo currentResultProperty = command.GetType().GetProperty("CurrentResult", BindingFlags.NonPublic | BindingFlags.Instance); if (currentResultProperty != null) { currentResultProperty.SetValue(command, new RequestResult()); } Delegate buildRequest = buildRequestField.GetValue(command) as Delegate; if (buildRequest != null) { HttpWebRequest webRequest = buildRequest.DynamicInvoke(uri, null, false, null, null) as HttpWebRequest; if (webRequest != null) { // Delete requests set a Content-Length of 0 with the HttpRequestMessage class for some reason. // Add in this header before signing the request. if (webRequest.Method == "DELETE") { webRequest.ContentLength = 0; } // Sign the request to add the Authorization header batchRequest.AuthenticationHandler.SignRequest(webRequest, null); // Convert the signed HttpWebRequest into an HttpRequestMessage for use with the HTTP Recorder requestMessage = new HttpRequestMessage(new HttpMethod(webRequest.Method), webRequest.RequestUri); foreach (var header in webRequest.Headers) { string key = header.ToString(); string value = webRequest.Headers[key]; if (string.Equals(key, "Content-Type")) { // Copy the Content to the HttpRequestMessage FieldInfo streamField = command.GetType().GetField("SendStream", BindingFlags.Public | BindingFlags.Instance); if (streamField != null) { Stream contentStream = streamField.GetValue(command) as Stream; if (contentStream != null) { MemoryStream memStream = new MemoryStream(); contentStream.CopyTo(memStream); memStream.Seek(0, SeekOrigin.Begin); requestMessage.Content = new StreamContent(memStream); // Add Content-Type header to the HttpRequestMessage // Use a custom class to force the proper formatting of the Content-Type header. requestMessage.Content.Headers.ContentType = new JsonOdataMinimalHeader(); requestMessage.Content.Headers.ContentLength = webRequest.ContentLength; } } } else { requestMessage.Headers.Add(key, value); } } } } } } return requestMessage; }