2013-12-19 4 views
8

가 나는 ApiController의 요청 처리 방법에서이 코드를 가지고 :Request.CreateResponse()에서 HttpResponseException을 처리해야합니까?

if (uri != null) 
{ 
    HttpResponseMessage r = Request.CreateResponse(HttpStatusCode.Redirect); 
    r.Headers.Location = uri; 
    throw new HttpResponseException(r); 
} 

잠재적 인 문제는 "R"이 배치되지 않습니다 점이다 (내 코드에서 적어도).
내가 이것을 사용하여 포장 할 수는 있지만 응답이 클라이언트에 스트리밍되기 전에 "r"이 처리되지 않겠는가?

이 문제를 해결하는 올바른 방법은 무엇입니까?

+2

그것은 그들의 클래스 이런 식으로 구조 것이 흥미 롭다. 나는 문서에서 도움이되는 것을 찾을 수는 없지만 [this constructor] (http://msdn.microsoft.com/en-us/library/hh835324(v=11118).aspx)을 사용할 수는있다. 대신 상태 코드? –

+0

"너무 빨리 처리되지 않습니까?" 너무 일찍, 정확히? – spender

+2

@spender, 나는 응답이 클라이언트에 스트리밍되기 전에 폐기 될 것이라고 말하고있다. –

답변

5

모든 examples 내가 본 것은 응답을 처리 할 필요가 없다는 것을 보여줍니다.

public Product GetProduct(int id) 
{ 
    Product item = repository.Get(id); 
    if (item == null) 
    { 
    var resp = new HttpResponseMessage(HttpStatusCode.NotFound) 
    { 
     Content = new StringContent(string.Format("No product with ID = {0}", id)), 
     ReasonPhrase = "Product ID Not Found" 
    } 
    throw new HttpResponseException(resp); 
    } 
    return item; 
} 

HttpResponseException의 소스 코드를 보면, 그것이 그 값을 가진 속성 (HttpResponseMessage Response)를 채우는이 표시되고, 폐기하는 것은 아마이 발생할 것 HttpResponseMessage 경우 ObjectDisposedException가 발생하거나 클라이언트에게 전달하는 데 실패 중 하나.

또한 SupressMessage가 소스 코드에 것을 알 수 있습니다 :

[SuppressMessage("Microsoft.Reliability", 
    "CA2000:Dispose objects before losing scope", 
    Justification = "Instance is disposed elsewhere")] 

인스턴스가 (이 HttpResponseMesssage에 언급되지 않은, 그것은으로 IDisposable을 구현하지 않는) 다른 곳으로 배치된다.

이 문제를 해결하는 올바른 방법은 무엇입니까?

코드 변경이 필요하지 않습니다.

1

나를 위해, "인스턴스는 다른 곳에 배치됩니다"라고해서 처리 할 필요가 없다는 의미는 아닙니다.

내 솔루션은 RegisterForDispose 그래서이된다 :

HttpResponseMessage r = Request.CreateResponse(HttpStatusCode.Redirect); 
r.Headers.Location = uri; 
this.request.RegisterForDispose(r); 
throw new HttpResponseException(r); 
관련 문제