Restsharp - XElement Property Exception

I need to make a REST request and pass an object that has a property of type XElement.

An object:

public class Test
{
    public string Property1 {get;set;}
    public XElement PropertyXml {get;set;}
}

The code:

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);

I get a "System.StackOverflowException" on a line with an AddBody call. PS I can pass the Test object using HttpClient (I use the PostAsJsonAsync method) instead of Restsharp.

Any ideas would be appreciated.

+4
source share
1 answer

RestSharp does not have the inherent knowledge of XElement, but AddBodywill try to serialize it, like any other type of POCO, by moving its properties. You can see that this process is easily stuck in an infinite loop:

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;}
}
+2

Source: https://habr.com/ru/post/1570883/


All Articles