【问题标题】:WCF REST Service JSON Post dataWCF REST 服务 JSON 发布数据
【发布时间】: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 调试服务器。

标签: c# wcf json rest post


【解决方案1】:

偶然发现此链接WCF + REST: Where is the request data? 并看到Glenn 将流传递给方法的响应,然后使用流读取器将其撕成字符串以获取表单发布数据。

修改原型服务代码如下

[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 对其进行攻击,将其序列化为 / 从字典中传递给业务逻辑.不是很漂亮,但很实用。

【讨论】:

  • 发现调用 Close 和/或 Dispose 会弄乱响应,导致服务错误“消息已被处理”
【解决方案2】:

我使用这个并且有效:

[WebInvoke(ResponseFormat = WebMessageFormat.Json, 
           RequestFormat = WebMessageFormat.Json,
           BodyStyle = WebMessageBodyStyle.WrappedRequest,
           Method = "POST",
           UriTemplate = "setExpositions?shelfId={shelfId}")]
[OperationContract]
public bool SetExpositions(int shelfId, List<WcfExposition> expositions)
{
}

shelfId 在 GET 中传递,expositions 在消息体中作为 JSON 数据传递。

【讨论】:

  • 您能否向我们展示一些适用于此操作的示例 JSON 数据?
【解决方案3】:

您是否尝试过在请求正文中输入 {"jsonstring":"Frank"}(在提琴手的请求生成器中)?

【讨论】:

  • 我认为这是真正的答案。包装时,json 按名称反序列化为方法参数。
【解决方案4】:

我认为BodyStyle = WebMessageBodyStyle.WrappedRequest 中可能存在问题,我认为,尽管文档完全不清楚,但希望元素用方法名称包装。

设置为展开,并将请求正文设置为'{"Name":"Frank"}'(注意它周围的单引号。你实际上发布的是一个包含 JSON 的字符串。我不知道你为什么想要这个。它让我想起了@987654321 @ 他们将 xml 放入其 xml 中。您将 JSON 放入您的 JSON。

【讨论】:

    【解决方案5】:

    您是否尝试过 [WebGet(UriTemplate = .. ] 属性而不是帖子,看看是否可行?这是一个示例 - http://blogs.msdn.com/b/kaevans/archive/2007/09/04/creating-a-json-service-with-webget-and-wcf-3-5.aspx

    【讨论】:

    • 我确实定义了一个 webget 方法,它按预期返回,目前已注释掉以清理代码以专注于帖子。我用 webinvoke 方法把它放在了适当的位置,并在 stackoverflow 上找到了一个帖子(现在找不到它),说不包括那个。在删除之前我根本没有通过,收到 404,删除后我现在收到 200 个回复,但没有数据。如果我将其保留为根 ("") 或将端点放入,这似乎并不重要
    • 您使用的是什么操作系统和版本的 IIS,或者 IIS Express 或 Web 开发服务器?应用程序池设置为什么框架? 404 错误可能是由于此处相关的问题social.msdn.microsoft.com/Forums/en-US/wcf/thread/…
    • 2008 r2 sp1 IIS 7 与 .Net 4 注册到 aspnet_regiis.exe。关于在 IIS 7 中添加引用的 dll 的任何指针?还尝试安装了 IIS 的 win 7 sp1,但我认为那是 IIS 7.5 ......虽然仍然没有果汁。
    • 更新:启用了跟踪,并且在反序列化过程中似乎出现异常...“在反序列化过程中 XML 中遇到了一个无法识别的元素 wsa,它被忽略了。”我的理解是使用 wcf 4 中的新路由,我不必担心定义端点。有人知道这是不是真的吗?
    • 不得不投反对票,因为 OA 专门询问有关发布的问题。网络上到处都是 GET 的示例,它们很容易工作,但我来这个问题是为了寻找关于做 POST 需要改变什么的答案,而这个答案说要使用 GET。
    猜你喜欢
    • 1970-01-01
    • 2011-02-27
    • 1970-01-01
    • 2014-12-31
    • 2011-02-24
    • 1970-01-01
    • 2012-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多