Restsharp-由于XElement属性导致的异常
我需要发出一个REST请求并传递一个具有XElement类型属性的对象 对象:Restsharp-由于XElement属性导致的异常,restsharp,Restsharp,我需要发出一个REST请求并传递一个具有XElement类型属性的对象 对象: public class Test { public string Property1 {get;set;} public XElement PropertyXml {get;set;} } 守则: var testObj = new Test(); testObj.Property1 = "value"; testObj.PropertyXml = new XElement("test"); va
public class Test
{
public string Property1 {get;set;}
public XElement PropertyXml {get;set;}
}
守则:
var testObj = new Test();
testObj.Property1 = "value";
testObj.PropertyXml = new XElement("test");
var level1 = new XElement("level1", "value111");
testObj.PropertyXml.Add(level1);
var client = new RestClient();
client.BaseUrl = new Uri(string.Format(_url));
var rRequest = new RestRequest(_address, Method.POST);
rRequest.RequestFormat = DataFormat.Json;
rRequest.AddBody(testObj);
var response = client.Execute(rRequest);
我在AddBody调用的行中得到一个“System.StackOverflowException”。
PS我可以使用HttpClient(我使用PostAsJsonAsync方法)而不是Restsharp通过测试对象
如有任何意见,将不胜感激 RestSharp没有XElement的固有知识,而
AddBody
将尝试像任何其他POCO类型一样序列化它—通过遍历其属性。你可以很容易地看到这个过程陷入无限循环:
testObj.FirstNode.Parent.FirstNode.Parent....
最好将PropertyXml
属性的类型更改为简单的POCO类型,XML结构可以轻松映射到该类型。比如:
public class PropertyStructure
{
public string level1 {get;set;}
}
public class Test
{
public string Property1 {get; set;}
public PropertyStructure PropertyXml {get; set;}
}
您使用的是哪个版本的restsharp?如果使用103以上的版本,则需要使用
rRequest.JsonSerializer=new JsonSerializer()将JSON seralizer设置回JSON.net代码>从何处提交