最好的方式来从.NET REST API使用JSON最好的、方式、NET、JSON

2023-09-03 06:50:54 作者:七彩云朵.

我的工作在C#.NET Web应用程序,我需要使用来自第三方REST服务(他们没有使用WCF)。我在使用Web服务调用的一个背景的,那里有一个WSDL提供和Visual Studio将建立所有的底层code,然后我准备好了。

I'm working on a .net web app in c# and I need to consume a REST service from a third party (they are not using WCF). I'm coming from a background of using web service calls where there was a WSDL available and Visual Studio would build all of the underlying code and then I am ready to go.

有没有工具或框架,可以在一定程度上模仿这种行为?据我所知,没有合同,没有办法的工具,知道会发生什么,但我想我可以继续通过向导,我提供参数做一个REST调用,然后帮助向导制定出响应的细节。在该过程结束时,我将有一组模拟类似于如何在Web服务的行为的REST API的对象。

Is there no tool or framework that can to some degree simulate this behavior? I understand that without a contract there is no way for a tool to know what to expect but I would think I could go through a wizard where I supply parameters to make a REST call and then help the wizard work out the details of the response. At the end of the process I would have a set of objects that model the REST API similar to how the web service behave.

我知道,REST和JSON有一些很大的优势,他们却缺乏一个标准的开箱合同,允许自动code一代好像倒退的真正一步。

I know that REST and JSON have some great advantages to them but the lack of a standard out of the box contract to allow automated code generation seems like a real step backwards.

我失去了一些东西明显的或者是在.NET中使用REST时的事务只是当前的状况如何?我真的需要写锅炉板code为每一个新的API?

Am I missing something obvious or is that just the current state of affairs when consuming REST in .NET? Do I really need to write boiler plate code for each new API?

推荐答案

您可以看一下使用 RestSharp 访问REST API 。我给了一个样本,其中包括一种方法,在使用AutoMapper从数据传输对象去域模型的this SO质疑。

You can look at using RestSharp to access the REST API. I gave a sample, including an approach at using AutoMapper to go from data transfer object to domain model on this SO question.