簡體   English   中英

C# Wcf Web API 反序列化

[英]C# Wcf Web API Deserializing Response

我正在嘗試對返回非原語的 web 服務進行單元測試。 根據請求,響應可以是 xml 或 json。 在我的測試中,如果我可以將內容主體反序列化為我的對象之一,那就太好了。 這是一些代碼:

    [WebGet(UriTemplate = "{arg1}/{arg2}")]
    public HttpResponseMessage<MyType> GetSomethingCool(string arg1, long arg2)
    {    
         return new HttpResonseMessage<MyTpe>(new MyType());
    }

    public class MyType
    {
         public string Property1 { get; set; }
         public long Property2 { get; set; }
         public string Property3 { get; set; }
    }

我的測試:

    [Test]
    public void MyTestForTheWebService_ReturnsText()
    {
          Service1 service = new  Service1 (_mockRepository.Object);
          HttpResponseMessage<MyType> result = service.GetSomethingCool("Something", **);

          Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);

          MyType resultContent = result.Content.ReadAs<MyType>(); // doesn't work
          Assert.AreEqual("expected value", resultContent .Property1);
          .....

所以基本上我需要能夠將 result.Content 轉換為 MyType 並且不知道如何。 謝謝

嘗試做:

MyType resultContent = (MyType)result.Content.ReadAs();

我相信你遇到了一個已知的錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM