我想使用第三方 RESTful Web 服务,它公开 GET 请求的方法。GET 请求将在 Windows 服务 + 计时器中执行,它们都返回 JSON。
如果可能的话,我希望使用 WCF 库来反序列化 JSON -> POCO,同时实现前向兼容性。据我了解,后者是通过实现 IExtensibleDataObject 的 DataContracts 实现的。我可以使用这种方法来使用 3rd 方 restful (JSON) Web 服务吗?
任何反馈表示赞赏。谢谢。
我想使用第三方 RESTful Web 服务,它公开 GET 请求的方法。GET 请求将在 Windows 服务 + 计时器中执行,它们都返回 JSON。
如果可能的话,我希望使用 WCF 库来反序列化 JSON -> POCO,同时实现前向兼容性。据我了解,后者是通过实现 IExtensibleDataObject 的 DataContracts 实现的。我可以使用这种方法来使用 3rd 方 restful (JSON) Web 服务吗?
任何反馈表示赞赏。谢谢。
听起来您正在尝试使用 WCF 客户端库来使用第三方服务。WCF 客户端库旨在使用已定义的服务契约,无论是来自 WSDL 文档还是来自具有ServiceContract类以及相关OperationContract和DataContract标记的方法和类的程序集。您可以使用客户端库来使用 JSON,但您几乎必须将第三方服务“重新创建”为基于 soap 的服务来生成 WSDL 或ServiceContract为服务合同编写程序集。
查看这篇简短的 CodeProject 文章,了解如何创建 WCF JSON 输出服务以及如何在 WCF 中配置WebHttpBinding。
至于使用IExtensibleDatObjectJSON,这篇其他 CodeProject 文章有一些很好的信息,但我认为它不适用于您的场景,因为您必须控制第三方服务合同。
在 IService1.cs 页面中:-
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "Json/{id}")]
string myfun(string id);
在 Service1.cs 页面中:-
public string myfun(string id)
{
string ConnectString = "server=localhost;database=test_local;integrated security=SSPI";
string QueryString = "select * from tbl_af_register where id=" + id;
SqlConnection myConnection = new SqlConnection(ConnectString);
SqlDataAdapter da = new SqlDataAdapter(QueryString, myConnection);
DataSet ds = new DataSet();
da.Fill(ds, "TestName");
string i = ds.Tables[0].Rows[0]["name"].ToString();
return "your name is " + i;
}
在 Web.Config 文件中:-
在 -> 'system.serviceModel' 部分
<system.serviceModel>
<services>
<service name="WcfService2.Service1">
<!-- Service Endpoints -->
<!-- Unless fully qualified, address is relative to base address supplied above -->
<endpoint address ="" binding="webHttpBinding" contract="WcfService2.IService1" behaviorConfiguration="web">
<!--
Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
automatically.
-->
</endpoint>
</service>
</services>
在 web.config 的“行为”部分:-
<behaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
注意:- 此名称“web”应与 -> behaviorConfiguration="web" 相同