寻找一个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