DataSnap與JSON序列化(5)

來源:互聯網
上載者:User

一、用ASP.Net做用戶端調用REST Server

 

在Delphi prism xe中使用ASP.Net做用戶端,

(1)通過http Get調用可以:

method MainForm.button3_Click(sender: System.Object; e: System.EventArgs);<br />var<br /> J:TJSONObject;<br /> S:string;<br /> vURL:string;<br /> vHC:RemObjects.InternetPack.Http.HttpClient;<br />begin<br /> vURL:='http://'+textBox_Server.Text+':'+textBox_Port.Text+'/datasnap/rest/TServerMethods1/ReverseString/';<br /> vURL:=vURL+system.Uri.EscapeDataString(textBox_RawString.Text);<br /> vHC:=new RemObjects.InternetPack.Http.HttpClient();<br /> S:=vHC.Get(vURL);<br /> //textBox_Result.Text:=S;<br /> J:=new Borland.Data.TJSONObject();<br /> J.Parse(System.Text.Encoding.ASCII.GetBytes(S),0);<br /> textBox_Result.Text:=TJSONArray(J.Get(0).JsonValue).Get(0).Value;<br />end;

(2)通過DataSnap Client調用

調用REST Server會出現一個錯誤:

method MainForm.button1_Click(sender: System.Object; e: System.EventArgs);<br />var<br /> C:TAdoDbxDatasnapConnection;<br /> vC:TADODBXCommand;<br />begin<br /> C:=new TAdoDbxDatasnapConnection();<br /> try<br /> C.ConnectionString:=<br /> 'CommunicationProtocol=http;'+<br /> 'HostName='+textBox_Server.Text+';'+<br /> 'Port='+textBox_Port.Text;<br /> C.Open;</p><p> vC:=C.CreateCommand as TADODBXCommand;<br /> try<br /> vC.CommandType:=System.Data.CommandType.StoredProcedure;// TDBXCommandTypes.DSServerMethod;<br /> vC.CommandText:='TServerMethods1.ReverseString';<br /> vC.Prepare;<br /> vC.Parameters[0].Value:=textBox_RawString.Text;<br /> vC.ExecuteNonQuery;<br /> textBox_Result.Text:=vC.Parameters[1].Value.ToString;<br /> finally<br /> vC.Dispose;<br /> end;<br /> finally<br /> C.Dispose;<br /> end;<br />end;

未能負載檔案或程式集“Borland.Data.DbxClientDriver/,Version/=$ASSEMBLY_VERSION$/,Culture/=neutral/,PublicKeyToken/=91d62ebb5b0d1b1b”或它的某一個依賴項。給定程式集名稱或基本代碼無效。 (異常來自 HRESULT:0x80131047)

具體應該是:

'Borland.Data.DbxCommonDriver, Version=15.0.0.0, Culture=neutral, PublicKeyToken=a91a7c5705831a4f' or one of its dependencies

 

已經確認這是個bug,https://forums.embarcadero.com/message.jspa?messageID=294148#294148

 

二、Delphi prism能寫REST Server嗎?

 

Delphi prism xe沒有直接寫REST Server的project wizard,但是可以參考visual c#/Web/WCF REST Service Application的寫法,然後在Prism中,建立一個Web/ASP.Net Web Application項目,然後,在Project右鍵,“添加”-“import c#”,選到那個Service1.cs,會自動轉成Service.pas,在其中寫個函數:

 

Service1 = public class<br /> public<br /> [WebGet(UriTemplate := 'Sum/{a}/{b}')]<br /> method Sum(a: System.String; b: System.String): System.Int32;</p><p>implementation</p><p>method Service1.Sum(a: System.String; b: System.String): System.Int32;<br />begin<br /> Result:= Convert.ToInt32(a) * 10 + Convert.ToInt32(b);<br />end;

方法前因為要加[WebGet(UriTemplate := 'Sum/{a}/{b}')],按參數列舉出URI的樣式,所以函數參數必須是字串,定義成a:integer就編譯不過。(我不確定是不是這樣定死了的)

 

然後,開啟global.asax.pas,在其中寫上:

method Global_asax.Application_Start(sender: Object; e: EventArgs);<br />begin<br /> // Edit the base address of Service1 by replacing the "Service1" string below<br /> RouteTable.Routes.Add(new ServiceRoute('Service1', new WebServiceHostFactory(), typeof(Service1)))<br />end;

記得useing 這幾個:

  System.ServiceModel,
  System.ServiceModel.Activation,
  System.Web.Routing,

 

然後編譯,ok了。

 

在IE中執行:http://localhost:54795/Service1/Sum/3/4

出現錯誤:

無法顯示 XML 頁。<br />無法查看使用 樣式表的 XML 輸入。請更正錯誤然後單擊 重新整理 按鈕,或稍後重試。 </p><p>--------------------------------------------------------------------------------</p><p>XML 文檔必須有一個頂層元素。處理資源 'http://localhost:54795/Service1/Sum/3/4' 時出錯。

查看原檔案,看到的是:

<int xmlns="http://schemas.microsoft.com/2003/10/Serialization/">34</int>

如果在函數的Attribute中加上ResponseFormat := WebMessageFormat.Json,就可以看到結果了:

[WebGet(UriTemplate := 'Sum/{a}/{b}', ResponseFormat := WebMessageFormat.Json)]<br /> method Sum(a: System.String; b: System.String): System.Int32;</p><p>http://localhost:54795/Service1/Sum/3/4</p><p>結果:<br /><int xmlns="http://schemas.microsoft.com/2003/10/Serialization/">34</int>

 

後續希望prism能直接提供這樣的REST Server project wizard。或者希望Visual Studo Shell的project wizard都有一個語言選項,讓不同的廠家以不同的語言來實現,如delphi,c#,vb.Net等。

 

三、REST Server的部署

 

以上是在IDE調試執行的。

在用dll部署到windows7的IIS7中,http://localhost:8084/WebApplication3.dll執行時出現了如下錯誤:

HTTP 錯誤 500.0 - Internal Server Error<br />無法顯示頁面,因為發生內部伺服器錯誤。

將繼續研究......

 

四、Delphi prism xe能用datasnap來寫REST Server嗎?

 

確認了在delphi prism xe中無法使用DataSnap來寫REST Server,即無法使用TDSServer。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.