【问题标题】:consume restful web service使用 RESTful Web 服务
【发布时间】:2012-08-28 11:42:08
【问题描述】:

我想使用第三方 RESTful Web 服务,它公开 GET 请求的方法。 GET 请求将在 Windows 服务 + Timer 中执行,它们都返回 JSON。

如果可能,我希望使用 WCF 库来反序列化 JSON -> POCO,同时实现前向兼容性。据我了解,后者是通过实现 IExtensibleDataObject 的 DataContracts 实现的。我可以使用这种方法来使用 3rd 方 RESTful (JSON) Web 服务吗?

感谢任何反馈。谢谢。

【问题讨论】:

    标签: wcf


    【解决方案1】:

    听起来您正在尝试使用 WCF 客户端库来使用第三方服务。 WCF 客户端库被设计为使用定义的服务契约,无论是来自 WSDL 文档还是来自具有 ServiceContract 类和相关 OperationContract 和 DataContract 标记的方法和类的程序集。您可以使用客户端库来使用 JSON,但您几乎必须将第三方服务“重新创建”为基于 soap 的服务来生成 WSDL 或为服务合同编写 ServiceContract 程序集。

    查看这个short CodeProject article,了解如何创建WCF JSON 输出服务,以及如何在WCF 中配置WebHttpBinding。

    关于使用带有 JSON 的IExtensibleDatObject,这个other CodeProject article 有一些很好的信息,但我认为它不适用于您的场景,因为您必须控制第三方服务合同。

    【讨论】:

    • 是的,我是这么认为的(即我必须重新创建 ServiceContract 以“伪造”第三方 API。不确定这是否是好的做法/值得吗?!
    • 我想使用类似的东西会更好:github.com/restsharp/RestSharp/wiki/Getting-Started
    • 这是一个更好的选择,或者如果您只需要关注 JSON,请查看 JSON.NET library。 Microsoft 使用它来代替 ASP.NET Web API 框架的 .NET JSON 库。
    • 如果我使用 RestSharp 如何确保前向兼容性?如果该服务向 JSON DTO 添加一个附加字段是解决我的客户端在反序列化期间中断的唯一方法吗?真的会失败吗?谢谢...
    【解决方案2】:

    在 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" 一样

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-16
      相关资源
      最近更新 更多