寻找有关基于cf2010中的WCF REST模板40(CS)扩展的wcf 4 rest服务的指南。我花了最后两天的时间来尝试使该Bugger正常工作,并复习其他帖子,尽管我已经接近了,但似乎无法越过终点。经过很多挫败之后,它最终到达了服务并发布(使用fiddler请求构建器),但是method参数作为null出现,但已在请求构建器中正确设置。我猜这可能是配置问题,但是由于截止日期迫在眉睫,我没有时间进行更多研究。FWIW,在调试中,jsonstring变量为null。自我承认是一个菜鸟问题,因为这是我第一次通过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; } }
提琴手请求标头
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: "
偶然发现此链接WCF + REST:请求数据在哪里?并且看到Glenn的响应将流传递给该方法,然后使用streamreader将其撕成字符串以获取表单发布数据。
修改了原型服务代码如下
[OperationContract] [WebInvoke (UriTemplate="/BOB", Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest)] public string BOB (Stream streamdata) { StreamReader reader = new StreamReader(streamdata); string res = reader.ReadToEnd(); reader.Close(); reader.Dispose(); return "Received: " + res; }
这似乎可以解决问题,完整的json数组在流中传递,读入本地字符串,然后我可以使用json.net对其进行攻击,以将其序列化为/从字典序列化后再传递给业务逻辑。不是很漂亮,但是功能强大。