2011-07-26 27 views
0

我想讓我們的用戶登陸Web服務器POST XML到我們的計算網絡服務器(用於關注XML數據)。登陸和計算網絡服務器只能通過互聯網連接 - 所以在計算網絡服務器上最好有一個REST API,以便XML數據可以發佈用於處理。將REST API設計到現有的.NET C#網站的練習

說實話,我們都是軟件工程師,但我們的能力在於算法處理,而不是網絡管理或ASP.NET本身。我環顧四周,看到WCF(Windows Communication Foundation)被踢了很多次。我擔心WCF可能太複雜/過度設計以適應我們的情況。

那麼,爲了讓現有的基於web表單的處理網站能夠接受POST的XML,最簡單的方法是什麼?我想我需要註冊一個URI(在web.config中?)作爲API接口,在該URI上有一個處理程序並檢索XML字符串 - 我只是不知道實現步驟:(。因此,玩具示例或指針教程將是真棒!

BTW,一旦我有處理服務器上的XML字符串,我的黃金!

感謝 希德

回答

0

好吧,所以我讀了幾個文件,能夠回答我自己的問題。基本上WCF REST已經發生了很大的變化。這在早期版本的安裝中非常複雜,但在最近的版本(.NET 4和WCF REST入門工具包)中,配置的開銷要少得多。

爲了讓WCF REST快速運行,我基本上做了3件事。

  1. 安裝路徑中的global.asax.cs服務(我不得不手動添加/編輯以此爲Global.asax中和的global.asax.cs)
  2. 設置了「服務合同」(即「 doThisMethod」裏面http://localhost/doThisMethod/
  3. 設置了 「數據合同」(即,如果您是通過HTTP主體發表任何XML/JSON,這個 「地圖」 XML/JSON和本機C#對象之間/序列化。)

對於非常基本的REST API(即只有HTTP GET),前兩步就足夠了。請注意,我的複製粘貼過程中洗出一些東西,但你應該明白我的意思...

Step1a

Global.asax的

<%@ Application Codebehind="~/App_Code/Global.asax.cs" Inherits="MyBLL.Global" Language="C#" %> 

Step1b

Global.asax中。CS

using System; 
using System.Collections.Generic; 
using System.ServiceModel.Activation; 
using System.Web; 
using System.Web.Routing; 

public class Global : System.Web.HttpApplication 
{ 
    void Application_Start(object sender, EventArgs e) 
    { 
     // Code that runs on application startup 
     RouteTable.Routes.Add(new ServiceRoute("/", new WebServiceHostFactory(), typeof(TestService))); 
    } 
} 

Step2a

正如你所看到的,我分裂接口(抽象類)和實際執行成兩個單獨的文件中完全分離。

TestInterface.cs

using System; 
using System.ServiceModel; 
using System.ServiceModel.Activation; 
using System.ServiceModel.Web; 

[ServiceContract] 
interface TestInterface 
{ 
    [OperationContract] 
    string TestAsXML(string extra); 

    [OperationContract] 
    string TestAsJSON(string extra); 
} 

Step2b TestService.cs

using System; 
using System.ServiceModel; 
using System.ServiceModel.Activation; 
using System.ServiceModel.Web; 

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
public class TestService : TestInterface 

    [WebInvoke(UriTemplate = "Test/{username}", Method = "GET", ResponseFormat = WebMessageFormat.Xml)] 
    public string TestAsXML(string username) 
    { 
     WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml"; 
     return String.Format("Hello {0}!", String.IsNullOrWhiteSpace(username) ? "world" : username); 
    } 

    [WebInvoke(UriTemplate = "Test/{username}?format=json", Method = "GET", ResponseFormat = WebMessageFormat.Json)] 
    public string TestAsJSON(string username) 
    { 
     // NOTE, if we GET this from a browsers, it will most likely have 
     // "Accept: text/html, application/xhtml+xml, */*" in the HTTP header 
     // So the framework will return XML instead. Try from Fiddler2 instead or 
     // write your own WCF client or from AJAX 
     WebOperationContext.Current.OutgoingResponse.ContentType = "application/json"; 
     return String.Format("Hello {0}!", String.IsNullOrWhiteSpace(username) ? "world" : username); 
    } 

第三步

對於上面的 「試驗」 的例子,一切都結束了的URI本身(由於GET),所以你不需要下面的東西。實際上,一旦你想使用POST等,你需要發送HTTP主體中的對象。在交換數據時,請使用「數據合同」來充分利用WCF的功能。

在這裏,我將在HTTP主體,這反過來又一個串並在一個INT再送一類。

TestInput.cs

using System; 
using System.Web; 
using System.Runtime.Serialization; 

// "" needed to clear out ugly xmlns namespace tags to make it plain old XML (POX) 
// If you want them, ether take it out or specify on your own 
[DataContract(Name = "TestInput", Namespace = "")] 
public class TestInput 
{ 
    // NOTE: If Order property is skipped, the data will be serialized 
    // alphabetically per variable names!! 
    // This can kill services, so better to be explicit 
    [DataMember(Order = 0)] 
    int SomeNumber; 

    // NOTE: If Name property is used XML will have UserName instead of internalUserName 
    [DataMember(Name="UserName", Order = 1)] 
    string internalUserName;  
} 
+0

它還沒有完成改變,只能看東西! (http://wcf.codeplex.com/) – Benjol

0

如果你只是想盡可能薄的層,沒有弄亂管道或弄清楚wcf,創建一個asp.net web項目,然後右鍵單擊並添加項目選擇一個httphandler,並在事件中從pull.items [字符串鍵]中拉出xml字符串。將其作爲url的ashx?key =你的xml字符串

+0

1)如果我要的東西XML字符串(它實際上是一個大文件)到URL不會是GET而不是POST?對於POST一個原因是因爲它不是冪等我... – DeepSpace101

+0

2)我猜你的意思是請求對象,而不是響應對象提取XML字符串?如果我發送XML字符串作爲隱藏的表單字段,它看起來像我能讀懂它作爲 'XMLstr = context.Request.Form [indexHiddenXMLField]' 但我懷疑這是錯誤的方法做,因爲那麼HTTP請求看起來像 '<..> 內容類型:應用程序/ x-WWW窗體-urlencoded 的Content-Length:31 HiddenXMLField = ' 我覺得在Content-Type應該反映XML(文/ XML? )和身體應該是真正的XML。這是一種預感 - 我可能是錯的,因爲我從來沒有設計一個真正的RESTful API – DeepSpace101

+0

3)我怎樣才能消除ashx的擴展,使server.com \ API \ incoming.ashx => server.com \ API \傳入(或者也許\在最後)? .....我不得不將它分成3個評論以適應。抱歉! – DeepSpace101