本文整理匯總了C#中System.Net.Http.HttpRequestMessage.DisposeRequestResources方法的典型用法代碼示例。如果您正苦於以下問題:C# HttpRequestMessage.DisposeRequestResources方法的具體用法?C# HttpRequestMessage.DisposeRequestResources怎麽用?C# HttpRequestMessage.DisposeRequestResources使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Net.Http.HttpRequestMessage
的用法示例。
在下文中一共展示了HttpRequestMessage.DisposeRequestResources方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: GetResponse
public static async Task<HttpResponseMessage> GetResponse(
string requestUri,
HttpMethod httpMethod,
HttpContent requestContent,
Action<HttpConfiguration, HttpServer> registerOData,
IEnumerable<KeyValuePair<string, string>> headers = null)
{
using (HttpConfiguration config = new HttpConfiguration())
{
using (HttpServer server = new HttpServer(config))
using (HttpMessageInvoker client = new HttpMessageInvoker(server))
{
registerOData(config, server);
HttpRequestMessage request = new HttpRequestMessage(httpMethod, requestUri);
try
{
request.Content = requestContent;
if (headers != null)
{
foreach (var header in headers)
{
request.Headers.Add(header.Key, header.Value);
}
}
return await client.SendAsync(request, CancellationToken.None);
}
finally
{
request.DisposeRequestResources();
request.Dispose();
}
}
}
}
示例2: GetResponseWithContentValidation
public static async Task<HttpResponseMessage> GetResponseWithContentValidation(
string requestUri,
HttpMethod httpMethod,
HttpContent requestContent,
Action<HttpConfiguration, HttpServer> registerOData,
HttpStatusCode expectedStatusCode,
string baselineFileName,
Func<string, string> postProcessContentHandler = null,
IEnumerable<KeyValuePair<string, string>> headers = null)
{
using (HttpConfiguration config = new HttpConfiguration())
{
using (HttpServer server = new HttpServer(config))
using (HttpMessageInvoker client = new HttpMessageInvoker(server))
{
registerOData(config, server);
HttpRequestMessage request = new HttpRequestMessage(httpMethod, requestUri);
try
{
request.Content = requestContent;
if (headers != null)
{
foreach (var header in headers)
{
request.Headers.Add(header.Key, header.Value);
}
}
var response = await client.SendAsync(request, CancellationToken.None);
await CheckResponse(response, expectedStatusCode, baselineFileName, postProcessContentHandler);
return response;
}
finally
{
request.DisposeRequestResources();
request.Dispose();
}
}
}
}
示例3: ConvertResponse
/// <summary>
/// Converts a <see cref="HttpResponseMessage"/> to an <see cref="HttpResponseBase"/> and disposes the
/// <see cref="HttpResponseMessage"/> and <see cref="HttpRequestMessage"/> upon completion.
/// </summary>
/// <param name="httpContextBase">The HTTP context base.</param>
/// <param name="response">The response to convert.</param>
/// <param name="request">The request (which will be disposed).</param>
/// <returns>A <see cref="Task"/> representing the conversion of an <see cref="HttpResponseMessage"/> to an <see cref="HttpResponseBase"/>
/// including writing out any entity body.</returns>
internal static Task ConvertResponse(HttpContextBase httpContextBase, HttpResponseMessage response, HttpRequestMessage request)
{
Contract.Assert(httpContextBase != null);
Contract.Assert(response != null);
Contract.Assert(request != null);
HttpResponseBase httpResponseBase = httpContextBase.Response;
httpResponseBase.StatusCode = (int)response.StatusCode;
httpResponseBase.StatusDescription = response.ReasonPhrase;
httpResponseBase.TrySkipIisCustomErrors = true;
EnsureSuppressFormsAuthenticationRedirect(httpContextBase);
CopyHeaders(response.Headers, httpContextBase);
CacheControlHeaderValue cacheControl = response.Headers.CacheControl;
// TODO 335085: Consider this when coming up with our caching story
if (cacheControl == null)
{
// DevDiv2 #332323. ASP.NET by default always emits a cache-control: private header.
// However, we don't want requests to be cached by default.
// If nobody set an explicit CacheControl then explicitly set to no-cache to override the
// default behavior. This will cause the following response headers to be emitted:
// Cache-Control: no-cache
// Pragma: no-cache
// Expires: -1
httpContextBase.Response.Cache.SetCacheability(HttpCacheability.NoCache);
}
Task responseTask = null;
bool isBuffered = false;
if (response.Content != null)
{
CopyHeaders(response.Content.Headers, httpContextBase);
// Select output buffering by the kind of content
isBuffered = IsOutputBufferingNecessary(response.Content);
httpResponseBase.BufferOutput = isBuffered;
responseTask = response.Content.CopyToAsync(httpResponseBase.OutputStream);
}
else
{
responseTask = TaskHelpers.Completed();
}
return responseTask
.Catch((info) =>
{
if (isBuffered)
{
// Failure during the CopyToAsync needs to stop any partial content from
// reaching the client. If it was during a buffered write, we will give
// them InternalServerError with zero-length content.
httpResponseBase.SuppressContent = true;
httpResponseBase.Clear();
httpResponseBase.ClearContent();
httpResponseBase.ClearHeaders();
httpResponseBase.StatusCode = (int)Net.HttpStatusCode.InternalServerError;
}
else
{
// Any failure in non-buffered mode has already written out StatusCode and possibly content.
// This means the client will receive an OK but the content is incomplete.
// The proper action here is to abort the connection, but HttpResponse.Abort is a 4.5 feature.
// TODO: DevDiv bug #381233 -- call HttpResponse.Abort when it becomes available
httpResponseBase.Close();
}
// We do not propagate any errors up, or we will get the
// standard ASP.NET html page. We want empty content or
// a closed connection.
return info.Handled();
})
.Finally(
() =>
{
request.DisposeRequestResources();
request.Dispose();
response.Dispose();
});
}
示例4: ApiControllerPutsSelfInRequestResourcesToBeDisposed
public void ApiControllerPutsSelfInRequestResourcesToBeDisposed()
{
// Arrange
var config = new HttpConfiguration();
config.Routes.MapHttpRoute("default", "", new { controller = "SpyDispose" });
var server = new HttpServer(config);
var invoker = new HttpMessageInvoker(server);
var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/");
invoker.SendAsync(request, CancellationToken.None).WaitUntilCompleted();
// Act
request.DisposeRequestResources();
// Assert
Assert.True(SpyDisposeController.DisposeWasCalled);
}
示例5: ConvertResponse
/// <summary>
/// Converts a <see cref="HttpResponseMessage"/> to an <see cref="HttpResponseBase"/> and disposes the
/// <see cref="HttpResponseMessage"/> and <see cref="HttpRequestMessage"/> upon completion.
/// </summary>
/// <param name="httpContextBase">The HTTP context base.</param>
/// <param name="response">The response to convert.</param>
/// <param name="request">The request (which will be disposed).</param>
/// <returns>A <see cref="Task"/> representing the conversion of an <see cref="HttpResponseMessage"/> to an <see cref="HttpResponseBase"/>
/// including writing out any entity body.</returns>
internal static async Task ConvertResponse(HttpContextBase httpContextBase, HttpResponseMessage response, HttpRequestMessage request)
{
Contract.Assert(httpContextBase != null);
Contract.Assert(request != null);
// A null response creates a 500 with no content
if (response == null)
{
CreateEmptyErrorResponse(httpContextBase.Response);
return;
}
CopyResponseStatusAndHeaders(httpContextBase, response);
// TODO 335085: Consider this when coming up with our caching story
if (response.Headers.CacheControl == null)
{
// DevDiv2 #332323. ASP.NET by default always emits a cache-control: private header.
// However, we don't want requests to be cached by default.
// If nobody set an explicit CacheControl then explicitly set to no-cache to override the
// default behavior. This will cause the following response headers to be emitted:
// Cache-Control: no-cache
// Pragma: no-cache
// Expires: -1
httpContextBase.Response.Cache.SetCacheability(HttpCacheability.NoCache);
}
// Asynchronously write the response body. If there is no body, we use
// a completed task to share the Finally() below.
// The response-writing task will not fault -- it handles errors internally.
try
{
if (response.Content != null)
{
await WriteResponseContentAsync(httpContextBase, response, request);
}
}
finally
{
request.DisposeRequestResources();
request.Dispose();
response.Dispose();
}
}