尋找一個WCF 4 REST服務一些指導其是基於WCF REST模板40(CS)在VS2010擴展。我花了幾天的時間試圖讓這個玩家工作,審查其他職位,而當我接近時,我似乎無法越過終點線。經過多次挫折之後,它終於到達了服務併發布(使用fiddler請求構建器),但方法參數是空的,但它在請求生成器中正確設置。我猜測這可能是配置問題,但隨着截止日期的到來,我沒有時間進行更多的研究。 FWIW,在調試時,jsonstring變量爲空。自我承認是一種noob問題,因爲這是第一次通過REST爲我,任何幫助將不勝感激!WCF REST服務的JSON數據後
在此先感謝。
的web.config
<system.web>
'<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</modules>
</system.webServer>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
的global.asax.cs
public class Global : HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes();
}
private void RegisterRoutes()
{
RouteTable.Routes.Add(new ServiceRoute("Scoring", new WebServiceHostFactory(), typeof(ScoringSvc)));
}
}
服務代碼
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class ScoringSvc
{
[OperationContract]
[WebInvoke
(Method = "POST",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat=WebMessageFormat.Json,
ResponseFormat=WebMessageFormat.Json)]
public string BOB(string jsonstring)
{
return "Received: " + jsonstring;
}
}
的Fiddler請求頭
Host: localhost
Content-Length: 20
Content-Type: application/json; charset=UTF-8
請求主體
{"Name":"Frank"}
原料從小提琴手響應
HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 12
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Mon, 21 Mar 2011 21:31:14 GMT
"Received: "
忘了提及,解決方案使用IIS 7作爲web服務器而不是apnet調試服務器。 – Grogh 2011-03-21 22:05:25