如何在C#中使用WCF REST服务?

sen*_*mar 6 c# rest wcf wcf-rest

我的合同细节如下.我正在使用Json响应和请求格式,也使用POST方法.如何编写客户端以在c#中使用此服务.

[OperationContract()]
[WebInvoke(UriTemplate = "/RESTJson_Sample1_Sample1Add", Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
int RESTJson_Sample1_Sample1Add(Int32 a, Int32 b, Int32 c);
Run Code Online (Sandbox Code Playgroud)

ran*_*uwe 0

如果您想使用 C# 的 REST 服务,您可以查看RestSharp。请注意,使用 WCF,您还可以在不同端点上使用 basicHttp 绑定来公开相同的方法,并使用 SOAP 使用它。

您还可以查看 WebChannelFactory,查看本文末尾的MSDN 教程。