WCF REST服务的JSON数据后 [英] WCF REST Service JSON Post data

查看:170
本文介绍了WCF REST服务的JSON数据后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

寻找一个WCF 4 REST服务一些指导这是基于WCF REST模板40(CS)在VS2010扩展。我花了过去几天试图让这个开溜到工作,审查其他职位,虽然我已经得到接近,我似乎无法越过终点线。太多的无奈之后,它终于击中服务和发布(使用招请求生成器),但方法参数跨为空来,但它正在请求生成器设置正确。我猜它可能在这一点上是一个配置问题,但随着最后期限的临近,我的时间不多了进行更多的研究。 FWIW,在调试,jsonstring变量为空。自诚然样的小白问题,因为这是第一次通过REST对我来说,任何帮助将非常感谢!

提前

感谢。



的web.config

 <&的System.Web GT; 
'<编译调试=真targetFramework =4.0/>
< /system.web>

< system.webServer>
<模块runAllManagedModulesForAllRequests =真正的>
<添加名称=UrlRoutingModuleTYPE =System.Web.Routing.UrlRoutingModule,System.Web程序,版本= 4.0.0.0,文化=中性公钥= b03f5f7f11d50a3a/>
< /模块>
< /system.webServer>

< system.serviceModel>
< serviceHostingEnvironment aspNetCompatibilityEnabled =真/>
< standardEndpoints>
< webHttpEndpoint>
< standardEndpoint NAME =helpEnabled =真automaticFormatSelectionEnabled =真/>
< / webHttpEndpoint>
< / standardEndpoints>
< /system.serviceModel>



的global.asax.cs

 公共类全球:HttpApplication的
{
无效的Application_Start(对象发件人,EventArgs五)
{
的RegisterRoutes();
}

私人无效的RegisterRoutes()
{
RouteTable.Routes.Add(新ServiceRoute(得分王,新WebServiceHostFactory()的typeof(ScoringSvc)) );
}
}



服务代码



  [的ServiceContract] 
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
公共类ScoringSvc
{
[OperationContract的]
[WebInvoke
(方法=POST,
BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat。 JSON,
ResponseFormat = WebMessageFormat.Json)]
公共字符串BOB(字符串jsonstring)
{
返回收到:+ jsonstring;
}
}



提琴手请求头

 主机:本地主机
的Content-Length:20
内容类型:应用程序/ JSON的;字符集= UTF-8



请求主体

  {名:弗兰克} 

原始响应从提琴手

  HTTP / 1.1 200 OK 
的Cache-Control:私人
的Content-Length:$ 12 b $ b含量类型:应用程序/ JSON的;字符集= UTF-8
服务器:Microsoft-IIS / 7.5
的X ASPNET-版本:4.0.30319
的X技术,通过:ASP.NET
日期:星期一, 2011 03月21日21点31分14秒GMT

收到:


解决方案

偶然发现这个链接 WCF + REST:哪里请求数据?并看到格伦的响应流传递给方法,然后撕裂,除了有一个StreamReader成一个字符串来获取表单POST数据。



修改原型服务代码如下:

  [OperationContract的] 
〔WebInvoke
(UriTemplate =/ BOB,
法=POST,
BodyStyle = WebMessageBodyStyle.WrappedRequest)]
公共字符串BOB(流streamdata)
{
StreamReader的读者=新的StreamReader(streamdata);
串解析度= reader.ReadToEnd();
reader.Close();
reader.Dispose();
返回收到:+资源;
}



这似乎这样的伎俩,充分JSON数组在传递流,读入当地的字符串,然后我可以用json.net序列化到/从字典传递到业务逻辑攻击它。算不上漂亮,但功能。


Looking for some guidance on a wcf 4 rest service which is based on the WCF REST Template 40(CS) extension in VS2010. I've spent the last couple of days trying to get this bugger to work, reviewing other posts, and while I've gotten close, I can't seem to cross the finish line. After much frustration, it is finally hitting the service and posting (using fiddler request builder) but the method parameter is coming across as null, but it's being set properly in the request builder. I'm guessing that it may be a config issue at this point, but as the deadline looms, I'm running out of time for more research. FWIW, in debugging, the jsonstring variable is null. Self admittedly kind of a noob question as this is the first time through REST for me, any help would be much appreciated!

Thanks in advance.

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)));
      }
   }

Service code

[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 request header

Host: localhost
Content-Length: 20
Content-Type: application/json; charset=UTF-8

request body

{"Name":"Frank"}

Raw response from fiddler

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: "

解决方案

Stumbled across this link WCF + REST: Where is the request data? and seen Glenn's response to pass a stream to the method and then rip that apart with a streamreader into a string to get the form post data.

Modified the prototype service code as follows

[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;
}

And that seems to do the trick, the full json array is passed in the stream, read into the local string, and I can then attack it using json.net to serialize into / from a dictionary to pass to the business logic. Not really pretty, but functional.

这篇关于WCF REST服务的JSON数据后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆