【发布时间】:2011-03-21 22:03:33
【问题描述】:
寻找有关基于 VS2010 中的 WCF REST 模板 40(CS) 扩展的 wcf 4 休息服务的一些指导。在过去的几天里,我一直在试图让这个混蛋工作,查看其他帖子,虽然我已经接近了,但我似乎无法越过终点线。在经历了很多挫折之后,它终于击中了服务并发布(使用提琴手请求构建器),但是方法参数出现为空,但它在请求构建器中被正确设置。我猜这可能是一个配置问题,但随着截止日期的临近,我没有时间进行更多研究了。 FWIW,在调试中, jsonstring 变量为空。诚然,这是一个菜鸟问题,因为这对我来说是第一次通过 REST,任何帮助都将不胜感激!
提前致谢。
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 调试服务器。