Answered by:
REST /SOAP interface - Client

Question
-
Hello,I need to develop a REST / SOAP interface
Get https://xxxxxOneObject:8443 Get https://xxxxxList:8443 Order { "OrderNumber" : "73620190930", "ProductNumber" : “RECEIVER_TVK1280/01", "ProductRevision" : "R1", “NrPerIndividual" : "8" } List<Order> ListOrder
I get an object order back and in the second case a list of possible orders.My questions are. What's the best way to do that? How can I test it myself? Can I simply write a REST server that returns data to me?Thank you for your tips.Greetings Markus
- Edited by Markus Freitag Monday, September 30, 2019 9:32 AM
- Moved by Jack J JunMicrosoft contingent staff Friday, October 4, 2019 5:51 AM
Monday, September 30, 2019 9:29 AM
Answers
-
I use HTTPclient the de facto, and I use the DTO pattern known by the WebAPI client and service
https://docs.microsoft.com/en-us/aspnet/web-api/overview/data/using-web-api-with-entity-framework/part-5
https://www.codeproject.com/Articles/1050468/Data-Transfer-Object-Design-Pattern-in-Csharp
{ var dtopayrolls = new List<DtoPayroll>(); using (var client = new HttpClient()) { var uri = new Uri("http://localhost/WebAPI/api/payroll/GetAll"); var response = client.GetAsync(uri).Result; if (!response.IsSuccessStatusCode) throw new Exception(response.ToString()); var responseContent = response.Content; var responseString = responseContent.ReadAsStringAsync().Result; dynamic payrolls = JArray.Parse(responseString) as JArray; foreach (var obj in payrolls) { DtoPayroll dto = obj.ToObject<DtoPayroll>(); dtopayrolls.Add(dto); } } return dtopayrolls; } public DtoPayroll Find(int id) { DtoPayroll dto; using (var client = new HttpClient()) { var uri = new Uri("http://localhost/WebAPI/api/payroll/Find?id=" + id); HttpResponseMessage getResponseMessage = client.GetAsync(uri).Result; if (!getResponseMessage.IsSuccessStatusCode) throw new Exception(getResponseMessage.ToString()); var responsemessage = getResponseMessage.Content.ReadAsStringAsync().Result; dynamic payroll = JsonConvert.DeserializeObject(responsemessage); dto = payroll.ToObject<DtoPayroll>(); } return dto; } public DtoPayroll FindPayRollByAuthorId(int id) { DtoPayroll dto; using (var client = new HttpClient()) { var uri = new Uri("http://localhost/WebAPI/api/payroll/FindPayRollByAuthorId?id=" + id); HttpResponseMessage getResponseMessage = client.GetAsync(uri).Result; if (!getResponseMessage.IsSuccessStatusCode) throw new Exception(getResponseMessage.ToString()); var responsemessage = getResponseMessage.Content.ReadAsStringAsync().Result; dynamic payroll = JsonConvert.DeserializeObject(responsemessage); dto = payroll.ToObject<DtoPayroll>(); } return dto; } public void Add(DtoPayroll dto) { using (var client = new HttpClient { BaseAddress = new Uri("http://localhost") }) { string serailizeddto = JsonConvert.SerializeObject(dto); var inputMessage = new HttpRequestMessage { Content = new StringContent(serailizeddto, Encoding.UTF8, "application/json") }; inputMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage message = client.PostAsync("WebAPI/api/payroll/Add", inputMessage.Content).Result; if (!message.IsSuccessStatusCode) throw new Exception(message.ToString()); } } public void Update(DtoPayroll dto) { using (var client = new HttpClient { BaseAddress = new Uri("http://localhost") }) { string serailizeddto = JsonConvert.SerializeObject(dto); var inputMessage = new HttpRequestMessage { Content = new StringContent(serailizeddto, Encoding.UTF8, "application/json") }; inputMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage message = client.PostAsync("WebAPI/api/payroll/Update", inputMessage.Content).Result; if (!message.IsSuccessStatusCode) throw new Exception(message.ToString()); } } public void Delete(DtoId dto) { using (var client = new HttpClient { BaseAddress = new Uri("http://localhost") }) { string serailizeddto = JsonConvert.SerializeObject(dto); var inputMessage = new HttpRequestMessage { Content = new StringContent(serailizeddto, Encoding.UTF8, "application/json") }; inputMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage message = client.PostAsync("WebAPI/api/payroll/Delete", inputMessage.Content).Result; if (!message.IsSuccessStatusCode) throw new Exception(message.ToString()); } } } }
using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using DAL; using Entities; namespace WebAPI.Controllers { [Route("api/[controller]")] [ApiController] public class PayRollController : ControllerBase { private IDaoPayroll dao; public PayRollController(IDaoPayroll daoPayroll) { dao = daoPayroll; } [HttpGet] [Route("GetAll")] public async Task<List<DtoPayroll>> GetAll() { return await dao.GetAll(); } [HttpGet] [Route("Find")] public async Task<DtoPayroll> Find(int id) { return await dao.Find(id); } [HttpGet] [Route("FindPayRollByAuthorId")] public async Task<DtoPayroll> FindPayRollByAuthorId(int id) { return await dao.FindPayRollByAuthorId(id); } [HttpPost] [Route("Add")] public async Task Add(DtoPayroll dto) { await dao.Add(dto); } [HttpPost] [Route("Update")] public async Task Update(DtoPayroll dto) { await dao.Update(dto); } [HttpPost] [Route("Delete")] public async Task Delete(DtoId dto) { await dao.Delete(dto.Id); } } }
- Marked as answer by Markus Freitag Sunday, October 6, 2019 10:20 AM
Saturday, October 5, 2019 4:43 PM
All replies
-
Microsofts framework for rest services is the WebApi. Typically with the WebApi you have a url like this
https://xxxxx:8443/controller/method
Monday, September 30, 2019 9:35 AM -
Hello Ken,
Thanks for the quick response.
What is different between REST / SOAP interface?
Do you have a good sample? How I can test it, I need a server, who sends me the answer. How can I achieve this?
Thanks in advance.
Best regards Markus
Monday, September 30, 2019 10:25 AM -
https://dotnet.microsoft.com/apps/aspnet/apis
Hello Ken,
Thanks for the quick response.
What is different between REST / SOAP interface?
Do you have a good sample? How I can test it, I need a server, who sends me the answer. How can I achieve this?
Thanks in advance.
Best regards Markus
Rest service is over HTTP only. SOAP is a XML based Web Service over HTTP, and SOAP can be used with SMTP. SOAP is legacy technology such as ASP.NET Web service usage while Rest is aligned with more recent technology such as ASP.NET WebAPI.
You want to test either a SOAP Web service or a WebAPI service you can simply use localhost aka IP 127.0.0.1 thee loopback IP and create a client program for the SOAP Web service and create the Web service and consume the Web service from the client program.
If you want to go with the WebAPI than you need a client program that is using one of the HTTP libraries such as HTTPclient that will communicate with the WebAPI using the HTTP verbs such as GET, POST PUT, etc and ect.
Of course, all of this can be done wit a development machine using Visual Stuido. The simpler of the two service types to use is Rest and WebAPI using Json with Json being the default that WebAPI uses.
Also there is Postmon, a free 3rd party tool, that will help in testing the ASP.NET WebAPI. but I found that you don't even need Postmon if one knows how to use the VS debugger and Attach to the WebAPI process.
- Edited by DA924x Monday, September 30, 2019 11:07 AM
Monday, September 30, 2019 10:52 AM -
Hello,
>you can simply use localhost aka IP 127.0.0.1
Do you know a good example?
With best regards Markus
Monday, September 30, 2019 3:48 PM -
Hello,
>you can simply use localhost aka IP 127.0.0.1
Do you know a good example?
With best regards Markus
https://www.tutorialspoint.com/asp.net_mvc/asp.net_mvc_web_api.htm
https://www.c-sharpcorner.com/article/calling-web-api-using-httpclient/
https://docs.microsoft.com/en-us/visualstudio/debugger/how-to-enable-debugging-for-aspnet-applications?view=vs-2017
https://www.getpostman.com/
WebAPI can be discussed at the WebAPI forum in ASP.NET forums.
http://forums.asp.net/
- Edited by DA924x Monday, September 30, 2019 8:23 PM
- Proposed as answer by Wendy ZangMicrosoft contingent staff Tuesday, October 1, 2019 1:37 AM
Monday, September 30, 2019 7:41 PM -
https://docs.microsoft.com/en-us/visualstudio/debugger/how-to-enable-debugging-for-aspnet-applications?view=vs-2017
Dear DA924x!Last question.
I can create a test server with localhost as described here.
I'll write the client. Create the requests.
Now I have to implement a switch statement in the server.
I don't know how to do that. Can you help me?
Get http://localhost:63457/api/employees/Request_01 Get http://localhost:63457/api/employees/Request_02 Get http://localhost:63457/api/employees/Request_03 ... Get http://localhost:63457/api/employees/Request_0N
I only need to query data via C# WPF application.
When the application is ready, I enter the customer URL. So that I can test it I look for ways to simulate it.
Maybe you can give me some more tips here.
Thank you in advance for your help.
With best regards MarkusTuesday, October 1, 2019 4:28 PM -
You should test against Local IIS pushing the WebAPI solution out to IIS on your local machine and debug and test against local IIS using Postman. Testing against local IIS and not IIS Express that development only, which is the litmus test.
Becuase after all, the WebAPI must work on IIS when deployed to a production environment.
The debug link shows how to make the VS ASP.NET Web project use local IIS and not IIS Express.
Wednesday, October 2, 2019 8:39 AM -
Dear DA924x!I want to create the answer myself. That is the question. Similar to that.
Do you have a concrete example project?
https://www.jsontest.com/http://validate.jsontest.com/?json={"key":"value"};http://validate.jsontest.com/?json=[JSON-code-to-validate]
With best regards Markus
Thursday, October 3, 2019 2:12 PM -
I want to create the answer myself. That is the question. Similar to that.
I don't understand the statement. What answer are you talking about, or are you talking about how to architect the solution?
Do you have a concrete example project?Yeah, I have some concerning all Web based solutions, nothing concerning a Windows desktop solution, but the key here is learning how to use the HTTPClient library to send or receive data with the WebAPI service that is applicable to any type of program as the WebAPI client with the client program being Windows desktop or Web.
Also, developing and testing against IIS Express is one thing, but testing against local IIS will expose any problems that are not uncovered by IIS Express, since IIS Express is not a real Web server, and local IIS on the development machine is the real Web server.
You have the ability to connect the project to local IIS and use VS to test and debug the project on local IIS and circumvent getting slapped in the face, becuase you only discovered you had a problem when you pushed the solution to IIS the real Web server.
Thursday, October 3, 2019 6:34 PM -
Dear DA924x!
I received 4 requests from the customer like this.Get http://localhost:63457/api/employees/Request_01 Get http://localhost:63457/api/employees/Request_02 Get http://localhost:63457/api/employees/Request_03 Get http://localhost:63457/api/employees/Request_04
For each query, a different object returns a different structure.Sometimes an object, sometimes a list of objects.
Since I don't have access to the customer server during development, I want to simulate it. I would like to use localhost.
The question is simple. How can I write a server that gives me the right answer in JSON format?How would you write the client, like the server that gives me the answers.That's all.Maybe this way.... for ClientI hope you understood me and thank you in advance for your answers.
Thanks in advance.
Greetings Markus
Saturday, October 5, 2019 10:32 AM -
I use HTTPclient the de facto, and I use the DTO pattern known by the WebAPI client and service
https://docs.microsoft.com/en-us/aspnet/web-api/overview/data/using-web-api-with-entity-framework/part-5
https://www.codeproject.com/Articles/1050468/Data-Transfer-Object-Design-Pattern-in-Csharp
{ var dtopayrolls = new List<DtoPayroll>(); using (var client = new HttpClient()) { var uri = new Uri("http://localhost/WebAPI/api/payroll/GetAll"); var response = client.GetAsync(uri).Result; if (!response.IsSuccessStatusCode) throw new Exception(response.ToString()); var responseContent = response.Content; var responseString = responseContent.ReadAsStringAsync().Result; dynamic payrolls = JArray.Parse(responseString) as JArray; foreach (var obj in payrolls) { DtoPayroll dto = obj.ToObject<DtoPayroll>(); dtopayrolls.Add(dto); } } return dtopayrolls; } public DtoPayroll Find(int id) { DtoPayroll dto; using (var client = new HttpClient()) { var uri = new Uri("http://localhost/WebAPI/api/payroll/Find?id=" + id); HttpResponseMessage getResponseMessage = client.GetAsync(uri).Result; if (!getResponseMessage.IsSuccessStatusCode) throw new Exception(getResponseMessage.ToString()); var responsemessage = getResponseMessage.Content.ReadAsStringAsync().Result; dynamic payroll = JsonConvert.DeserializeObject(responsemessage); dto = payroll.ToObject<DtoPayroll>(); } return dto; } public DtoPayroll FindPayRollByAuthorId(int id) { DtoPayroll dto; using (var client = new HttpClient()) { var uri = new Uri("http://localhost/WebAPI/api/payroll/FindPayRollByAuthorId?id=" + id); HttpResponseMessage getResponseMessage = client.GetAsync(uri).Result; if (!getResponseMessage.IsSuccessStatusCode) throw new Exception(getResponseMessage.ToString()); var responsemessage = getResponseMessage.Content.ReadAsStringAsync().Result; dynamic payroll = JsonConvert.DeserializeObject(responsemessage); dto = payroll.ToObject<DtoPayroll>(); } return dto; } public void Add(DtoPayroll dto) { using (var client = new HttpClient { BaseAddress = new Uri("http://localhost") }) { string serailizeddto = JsonConvert.SerializeObject(dto); var inputMessage = new HttpRequestMessage { Content = new StringContent(serailizeddto, Encoding.UTF8, "application/json") }; inputMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage message = client.PostAsync("WebAPI/api/payroll/Add", inputMessage.Content).Result; if (!message.IsSuccessStatusCode) throw new Exception(message.ToString()); } } public void Update(DtoPayroll dto) { using (var client = new HttpClient { BaseAddress = new Uri("http://localhost") }) { string serailizeddto = JsonConvert.SerializeObject(dto); var inputMessage = new HttpRequestMessage { Content = new StringContent(serailizeddto, Encoding.UTF8, "application/json") }; inputMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage message = client.PostAsync("WebAPI/api/payroll/Update", inputMessage.Content).Result; if (!message.IsSuccessStatusCode) throw new Exception(message.ToString()); } } public void Delete(DtoId dto) { using (var client = new HttpClient { BaseAddress = new Uri("http://localhost") }) { string serailizeddto = JsonConvert.SerializeObject(dto); var inputMessage = new HttpRequestMessage { Content = new StringContent(serailizeddto, Encoding.UTF8, "application/json") }; inputMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage message = client.PostAsync("WebAPI/api/payroll/Delete", inputMessage.Content).Result; if (!message.IsSuccessStatusCode) throw new Exception(message.ToString()); } } } }
using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using DAL; using Entities; namespace WebAPI.Controllers { [Route("api/[controller]")] [ApiController] public class PayRollController : ControllerBase { private IDaoPayroll dao; public PayRollController(IDaoPayroll daoPayroll) { dao = daoPayroll; } [HttpGet] [Route("GetAll")] public async Task<List<DtoPayroll>> GetAll() { return await dao.GetAll(); } [HttpGet] [Route("Find")] public async Task<DtoPayroll> Find(int id) { return await dao.Find(id); } [HttpGet] [Route("FindPayRollByAuthorId")] public async Task<DtoPayroll> FindPayRollByAuthorId(int id) { return await dao.FindPayRollByAuthorId(id); } [HttpPost] [Route("Add")] public async Task Add(DtoPayroll dto) { await dao.Add(dto); } [HttpPost] [Route("Update")] public async Task Update(DtoPayroll dto) { await dao.Update(dto); } [HttpPost] [Route("Delete")] public async Task Delete(DtoId dto) { await dao.Delete(dto.Id); } } }
- Marked as answer by Markus Freitag Sunday, October 6, 2019 10:20 AM
Saturday, October 5, 2019 4:43 PM -
Thanks!
I will check it. For now, it's enough. Many greetings Markus
Sunday, October 6, 2019 10:20 AM