本文整理汇总了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();
}
}