[英]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.