簡體   English   中英

C#在生產中使用Json WebService

[英]C# Using a Json WebService in production

一個人應該如何編寫一個負責連接靜態api的庫? lib應該提供直接訪問api函數的方法。 使用WebClient以字符串形式獲取響應,並使用Json.NET將其反序列化為對象是一種可靠的,可接受的方法嗎?

ServicePointManager.ServerCertificateValidationCallback = (a, b, c, d) => true;//the server uses a self signed SSL cert

using (WebClient client = new WebClient())
{
    client.Headers.Add("..", "....");
    string str = client.DownloadString("https://..../users/"+UserId+"/super-secret-data");     
}

您可以為每組通話(用戶管理,上傳/下載數據等)創建一個類。 此類實現一些功能(映射Web調用)。 因此,當您要列出所有新成員時,您可以編寫

var accounting = new Accounting();
var memberList = accounting.GetNewMember();

這個函數(GetNewMember())只是一個包裝器。 在該函數中,您調用REST-full API,將JSON返回,然后將其轉換為所需的結果。

對於創建URI,可以使用string.Format(string,object [])重載。

您可以這樣做,也可以將代碼生成與Visual Studio和swagger結合使用,以在解決方案中導入其余的Web api。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM