내가 가진 문제가 작업을 사용하는 방식과 관련이 있는지 또는 ReadAsAsync를 올바르게 사용하지 않는지 확실하지 않습니다. 내가 여기에있는 패턴을 다음입니다 :HttpClient ReadAsAsync가 deserialize하지 않음
http://blogs.msdn.com/b/henrikn/archive/2012/02/11/httpclient-is-here.aspx
배경 : 나는 직렬화 복원하고 객체는 POCO이다. 속성에는 속성이 없습니다. 그것은 단지 몇 가지 값 유형 속성과 몇 가지 컬렉션 속성입니다. REST 서비스가 제대로 작동하는 것 같습니다. 서비스에서 반환 한 JSON을 보면 괜찮은 것으로 보입니다. 웹 API 2.1 사용하기 5.1.2
문제점 : .. HttpResponseMessage.Content.ReadAsAsync()를 호출 중입니다. 때로는 작동하고 (객체를 반환합니다.) 때때로 ("Thread is aborted"를 던지거나 null을 반환합니다) throw하지 않습니다. content 속성은 한 번만 읽을 수 있고 후속 읽기는 오류를 발생시키는 것으로 보입니다. 아래 코드의 주석을보십시오.
관련 질문 : HttpContent.ReadAsAsync Deserialization issue
질문 광산과 유사한 것으로 보인다. 답변은 버그를 나타내지 만 2 년이 넘었습니다.
코드 :
[TestMethod]
public void AddSiteTest()
{
// Use POST to create a resource i.e. insert. Use PUT to update.
Site site = new Site {SiteName = "Test", Active = true, URI="www.test.com" };
Site newSite = null;
client.PostAsJsonAsync<Site>(baseURI + "/Sites/AddSite?securityKey="+ SecurityKey, site).ContinueWith(x =>
{
HttpResponseMessage response = x.Result;
response.EnsureSuccessStatusCode();
if (response.IsSuccessStatusCode)
{
try
{
string str = Task.Run(() => response.Content.ReadAsStringAsync()).Result; // yep its json and it is a proprety serialized object
// Method 1 (preferred...):
//Site siteA = Task.Run(() => response.Content.ReadAsAsync<Site>()).Result; // usuallly throws if content has been read
// Method 2:
Site siteB = response.Content.ReadAsAsync<Site>().Result; // usully returns a valid result (when I dont put a breakpoint on it). Does not deadlock.
// Method 3:
response.Content.ReadAsAsync<Site>().ContinueWith(d =>
{
Site siteC = d.Result; // returns null
});
}
catch (Exception ex)
{
string y = ex.Message;
}
}
});
}
요청이 완료된 후에도 계속 작업이 진행 중일 수 있습니다. '.ContinueWith' 대신 async/await 사용을 고려하십시오. – spender