2012-08-28 18 views
1

我想要使用暴露GET請求方法的第三方平靜Web服務。 GET請求將在Windows服務+定時器內執行,並且它們都返回JSON。使用平靜的Web服務

我想,如果可能的話,使用WCF庫去反序列化JSON - > POCO,同時實現向前兼容性。據我瞭解,後者是通過實現IExtensibleDataObject的DataContracts實現的。我可以使用這種方法來消費第三方平安(JSON)Web服務嗎?

任何反饋意見。謝謝。

回答

1

聽起來就像你試圖使用WCF客戶端庫消耗第三方服務。 WCF客戶端庫旨在使用已定義的服務合同,可以從WSDL文檔或具有ServiceContract類的程序集以及相關的OperationContractDataContract標記的方法&類中。您可以使用客戶端庫來使用JSON,但幾乎必須將第三方服務重新創建爲基於soap的服務來生成WSDL或爲服務合約編寫ServiceContract程序集。

看這個short CodeProject article,看看如何創建一個WCF JSON outputing 服務以及如何配置WCF中WebHttpBinding

至於使用IExtensibleDatObject與JSON,這other CodeProject article有一些很好的信息,但我不認爲它適用於您的情況,因爲你將不得不控制第三方服務合同。

+0

是的,我是這麼認爲的(即我將不得不重新創建ServiceContract以僞造第三方API。不知道這是好的做法/值得嗎?! – cs0815

+0

我想最好是使用像這樣的東西:https://github.com/restsharp/RestSharp/wiki/Getting-Started – cs0815

+1

這是一個更好的選擇,或者如果你只需要專注於JSON,看看[JSON。 NET庫](http://james.newtonking.com/projects/json-net.aspx)。它被Microsoft用來代替ASP.NET Web API框架的.NET JSON庫。 –

0

在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> 

在'behav web.config文件中的IOR部分: -

<behaviors> 
    <endpointBehaviors> 
      <behavior name="web"> 
       <webHttp/> 
      </behavior> 
      </endpointBehaviors> 

注: - 這名 「網絡」 應該是一樣的 - > behaviorConfiguration = 「網絡」