在 Xamarin 中与服务器通信的最佳方式?

Best way to communicate with the server in Xamarin?

我对 Xamarin 和移动开发完全陌生。我的第一个问题是架构明智的是使用现有的 Rest api 与移动客户端传输数据是一个好习惯。

我翻了 google Xamarin Azure 云文档,但没有找到满意的文章。此外,我们有一个 ember 前端应用程序,它使用来自 Azure 的 REST apis。

如果这些信息不够,请向我询问,如果它是重复的,请不要犹豫,用适当的 link 将其标记为重复。谢谢大家,请帮助并分享您的宝贵经验。

谢谢

我浏览的链接很少

https://azure.microsoft.com/en-us/documentation/learning-paths/appservice-mobileapps/

http://www.davevoyles.com/asp-net-web-api-vs-azure-mobile-services/

https://azure.microsoft.com/en-us/blog/azure-mobile-services-why-should-asp-net-developers-care/

http://weblogs.asp.net/scottgu/azure-virtual-machine-machine-learning-iot-event-ingestion-mobile-sql-redis-sdk-improvements

https://azure.microsoft.com/en-us/documentation/articles/app-service-mobile-value-prop/

https://azure.microsoft.com/en-us/documentation/articles/app-service-mobile-ios-get-started/

https://azure.microsoft.com/en-us/documentation/articles/partner-xamarin-mobile-services-ios-get-started/

如果您只想使用 现有 服务器 REST API,显而易见的步骤是在 Xamarin 中构建一个 REST 客户端。忘记 Xamarin 的 Azure 移动服务客户端或任何相关的东西,因为这将需要更改您的服务器代码。

关于如何构建 REST 客户端,有多种选择,但我推荐 Refit 作为更简单、更快速的方法。例如,您在一个简单的接口中声明您的 REST 方法:

public interface IGitHubApi
{
    [Get("/users/{user}")]
    Task<User> GetUser(string user);
}

神奇的是,所有必需的代码都将在编译时生成,因此您可以专注于您的 API 方法而不是实现本身,并在几分钟内构建您的客户端,而不是几小时或几天。然后你可以像这样使用那个界面:

var gitHubApi = RestService.For<IGitHubApi>("https://api.github.com");
var octocat = await gitHubApi.GetUser("octocat");

不使用 Azure 移动服务客户端的缺点是,如果您需要任何 offline/online 同步,则必须自己完成。但根据我的经验,客户端-服务器数据同步是一种边缘情况,在绝大多数应用程序中根本不需要。