Hello expert please i need help to pass list of object to data contract
first, i create a data contract
[DataContract('Colors')] class ColorDataContract { Name colorName; DataAreaId dataAreaId; [DataMember] public Name parmColorName(Name _colorName = colorName) { colorName = _colorName; return colorName; } }
second, create a service class
class ColorService { public void InsertColor(ColorDataContract colorObj) { ColorsTable _color; Name _name; _name = colorObj.parmColorName(); ttsbegin; _color.Name = _name; _color.insert(); ttscommit; } } public void insertColorList(List colorList) { ListIterator iterator; ListEnumerator enumerator; ListIterator literator; ColorDataContract colorDataContract; enumerator = colorList.getEnumerator(); while(enumerator.moveNext()) { colorDataContract = enumerator.current(); if(colorDataContract != null) { this.InsertColor(colorDataContract); } } } }
after that, I create a new service group and service and assign the method to the service, etc. everything until this point is working fine
In the code below i try to consume custom service in json using the Microsoft simple integration
github.com/.../d365fo.integrations
my problem is how to pass the list to my contract into JSON
i can pass data to insertColor method without any issue my problem with insertColorList method
static void Main(string[] args) { string sessionUrl = "/api/services/XXXServices/XXXColors/insertColorList"; string GetUserSessionOperationPath = string.Format("{0}{1}", ClientConfiguration.Default.UriString.TrimEnd('/'), sessionUrl); var request = HttpWebRequest.Create(GetUserSessionOperationPath); //how to pass the list to my service //Post one color //var con = new ArafaContractList(); //con.parmColorName = "Blue"; //var contract = new { colorObj = con }; string json = JsonConvert.SerializeObject(contract); Byte[] byteArray = Encoding.UTF8.GetBytes(json); request.Headers[OAuthHelper.OAuthHeader] = OAuthHelper.GetAuthenticationHeader(true); request.Method = "POST"; request.ContentLength = byteArray.Length; request.ContentType = "application/json"; using (var stream = request.GetRequestStream()) { stream.Write(byteArray, 0, byteArray.Length); } using (var response = (HttpWebResponse)request.GetResponse()) { using (Stream responseStream = response.GetResponseStream()) { using (StreamReader streamReader = new StreamReader(responseStream)) { string responseString = streamReader.ReadToEnd(); Console.WriteLine(responseString); } } } Console.ReadLine(); }
Thanks Martin Dráb
What problem do you have with insertColorList?
One thing I noticed it that the method isn't decorated with AifCollectionTypeAttribute. You should add it:
[AifCollectionTypeAttribute('colorList', Types::Class, classStr(ColorDataContract))] public void insertColorList(List colorList)
André Arnaud de Cal...
291,969
Super User 2025 Season 1
Martin Dráb
230,842
Most Valuable Professional
nmaenpaa
101,156