好吧,所以我讀了幾個文件,能夠回答我自己的問題。基本上WCF REST已經發生了很大的變化。這在早期版本的安裝中非常複雜,但在最近的版本(.NET 4和WCF REST入門工具包)中,配置的開銷要少得多。
爲了讓WCF REST快速運行,我基本上做了3件事。
- 安裝路徑中的global.asax.cs服務(我不得不手動添加/編輯以此爲Global.asax中和的global.asax.cs)
- 設置了「服務合同」(即「 doThisMethod」裏面http://localhost/doThisMethod/)
- 設置了 「數據合同」(即,如果您是通過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;
}
它還沒有完成改變,只能看東西! (http://wcf.codeplex.com/) – Benjol