AXForum  
Вернуться   AXForum > Microsoft Dynamics AX > DAX Blogs
All
Забыли пароль?
Зарегистрироваться Правила Справка Пользователи Сообщения за день Поиск Все разделы прочитаны

 
 
Опции темы Поиск в этой теме Опции просмотра
Старый 23.02.2017, 14:13   #1  
Blog bot is offline
Blog bot
Участник
 
25,475 / 846 (79) +++++++
Регистрация: 28.10.2006
goshoom: JSON-based custom service with parameters (AX 7)
Источник: http://dev.goshoom.net/en/2017/02/js...th-parameters/
==============

Dynamics 365 for Operations deploys custom web services in two ways: as SOAP-based services and JSON-based services. AX developers are often familiar with SOAP services (which were used in AX 2012), but JSON-based ones are new to them.

One particular challenge is passing arguments to a service. Let’s say we have an Add operation, which can sum two numbers.

public int Add(int a, int b){ return a + b;}






To call the service, we have to provide values for both arguments, a and b. Because the expected format is JSON (Java Script Object Notation), we have to provide a JSON string describing an object with two properties (a and b) and their values. This is it:

{ a: 2, b: 5 }






Note that names of the properties are important – they must match parameter names in the X++ method.

Because building JSON strings by yourself can be cumbersome (especially with more complex parameters), a better approach is working with objects and leaving conversion to JSON to a serializer.

For example, you can build a simple class,

public class AddContract{ public int a { get; set; } public int b { get; set; }}






create an instance with required values and call JsonConvert (from Newtonsoft.Json) to convert it to string:

AddContract contract = new AddContract { a = 2, b = 5 };string json = JsonConvert.SerializeObject(contract)






If you need AddContract class just at this single place, maybe it’s not worth creating it at all. We can use an anonymous object instead and it will still work the same.

var contract = new { a = 2, b = 5 }; // Anonymous objectstring json = JsonConvert.SerializeObject(contract)






When we have the JSON string, we have to send it to the service. The implementation below assumes that you use AuthenticationUtility from the sample solution from Microsoft.

// Prepare HTTP client, including authenticationHttpClient client = new HttpClient();client.BaseAddress = new Uri(ClientConfiguration.Default.UriString);client.DefaultRequestHeaders.Add(OAuthHelper.OAuthHeader, OAuthHelper.GetAuthenticationHeader()); // Define parametersvar contract = new { a = 2, b = 5 }; // Create a requestHttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "api/services/MyServiceGroup/MyService/Add");request.Content = new StringContent(JsonConvert.SerializeObject(contract), Encoding.UTF8, "application/json"); // Run the servicevar result = client.SendAsync(request).Result; // Display result to consoleif (result.IsSuccessStatusCode){ Console.WriteLine(result.Content.ReadAsStringAsync().Result);}else{ Console.WriteLine(result.StatusCode);}






If you’re using WebRequest instead of HttpClient (as in Microsoft sample code), you can use add parameters to the request in this way:

var contract = new { a = 2, b = 5 };string json = JsonConvert.SerializeObject(contract); Byte[] byteArray = Encoding.UTF8.GetBytes(json); request.ContentLength = byteArray.Length;request.ContentType = "application/json"; using (Stream dataStream = request.GetRequestStream()){ dataStream.Write(byteArray, 0, byteArray.Length);}






It’s clearly nothing difficult, just make sure that your properties in JSON exactly match parameter names. For example, if parameters of my Add operation were prefixed with underscore (as usual in X++), the JSON string would have to be { _a: 2, _b: 5 }.



Источник: http://dev.goshoom.net/en/2017/02/js...th-parameters/
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору.
 

Похожие темы
Тема Автор Раздел Ответов Посл. сообщение
atinkerersnotebook: New Configuration Blueprint Available: Configuring Service Management To Track Service Orders within Dynamics AX 2012 Blog bot DAX Blogs 0 19.09.2016 00:21
emeadaxsupport: AX Performance - Analyzing key SQL Server configuration and database settings Blog bot DAX Blogs 0 28.09.2015 14:11
atinkerersnotebook: Using Service Management to Track Service Orders Blog bot DAX Blogs 1 25.08.2013 19:16
emeadaxsupport: AX for Retail 2012 R2: Adding a Custom User Control to the POS Blog bot DAX Blogs 0 27.02.2013 07:14
Platform updates overview - 3.70.B - NAV2009 R2 Blog bot Dynamics CRM: Blogs 0 07.02.2011 22:06
Опции темы Поиск в этой теме
Поиск в этой теме:

Расширенный поиск
Опции просмотра

Ваши права в разделе
Вы не можете создавать новые темы
Вы не можете отвечать в темах
Вы не можете прикреплять вложения
Вы не можете редактировать свои сообщения

BB коды Вкл.
Смайлы Вкл.
[IMG] код Вкл.
HTML код Выкл.
Быстрый переход

Рейтинг@Mail.ru
Часовой пояс GMT +3, время: 03:46.
Powered by vBulletin® v3.8.5. Перевод: zCarot
Контактная информация, Реклама.