【问题标题】:How to pass a JSON parameter to RESTful WCF service?如何将 JSON 参数传递给 RESTful WCF 服务?
【发布时间】:2015-06-03 07:22:46
【问题描述】:

我开发了一个 Restful WCF 服务,我需要传递一个 json 字符串作为输入。 json 字符串是可变的。 这是我的服务:

[WebGet(UriTemplate = "{id}", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Xml)]
public string GetById(string id)
{

    string sampleItem = id;


    return sampleItem;
}

这是一个 json 示例:

{  
   "name":"obj1",
   "x":11,
   "y":20,
   "obj":{  
      "testKey":"val"
   },
   "z":30,
   "tab":[  
      1,
      2,
      46
   ],
   "employees":[  
      {  
         "firstName":"John",
         "lastName":"Doe"
      },
      {  
         "firstName":"Anna",
         "lastName":"Smith"
      },
      {  
         "firstName":"Peter",
         "lastName":"Jones"
      }
   ]
}

http://localhost:7626/Service1/myjsonstring

我收到此错误:Erreur HTTP 400 - 错误请求。

P.S:如果我传递一个简单的字符串,它就可以工作。 有什么想法请教

【问题讨论】:

  • 首先你的 JSON 无效 obj 应该用 " 包裹。第二件事,你真的想接收 JSON 作为字符串吗?也许你想得到一个反序列化的模型,WCF 可以毫无问题地做到这一点?
  • 我想把它作为字符串传入。
  • 你从哪里调用这个服务?
  • 我不是在开发使用该服务的应用程序

标签: c# json wcf rest


【解决方案1】:

首先,我建议您需要为此目的使用 POST 方法。读取原始字符串的最简单方法是将其作为Stream 获取。所以你的服务接口可能是这样的:

[ServiceContract]
public interface IService1
{
    [WebInvoke(Method = "POST", UriTemplate = "PostJson")]
    string PostJson(Stream request);
}

及实施:

public class Service1 : IService1
{
    public string PostJson(Stream request)
    {
        using (var reader = new StreamReader(request))
        {
            return "You posted: " + reader.ReadToEnd();
        }
    }
}

还请检查您的 Web.Config 中的配置是否正确:

<system.serviceModel>
  <services>
    <service name="WcfService1.Service1">
      <endpoint address="" behaviorConfiguration="restfulBehavior" 
                binding="webHttpBinding" bindingConfiguration=""
                contract="WcfService1.IService1">
      </endpoint>
    </service>
  </services>
  <behaviors>
    <serviceBehaviors>
      <behavior>
        <!-- To avoid disclosing metadata information, set the value below to false before deployment -->
        <serviceMetadata httpGetEnabled="true"/>
        <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
        <serviceDebug includeExceptionDetailInFaults="false"/>
      </behavior>
    </serviceBehaviors>
    <endpointBehaviors>
      <behavior name="restfulBehavior">
        <webHttp />
      </behavior>
    </endpointBehaviors>
  </behaviors>
  <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>

【讨论】:

  • 谢谢你的回答,但我认为Web.Config还是有问题
  • 调试时我得到这个:“添加服务失败。服务的元数据不可访问。检查您的服务是否正在运行并公开元数据”
  • 我编辑了答案,现在您可以从我的配置中看到完整的 system.serviceModel 部分。您也可以查看thisthis 的文章。这两篇文章都详细介绍了如何正确地为 restful WCF 制作 web.configs。
  • 感谢您解决了问题,但现在我仍然无法测试操作“PostJson”..“WCF 测试客户端不支持此操作,因为它使用 System.IO.Stream”跨度>
  • 是的,WCF 测试客户端不支持流。但是因为你在做 HTTP 的东西,你可以使用一些其他的测试客户端。我最喜欢的是Fiddler,它免费且流行,所以找教程不成问题。
猜你喜欢
  • 2012-07-03
  • 1970-01-01
  • 2016-05-18
  • 1970-01-01
  • 2012-12-04
  • 1970-01-01
  • 2017-07-10
  • 2014-03-04
  • 1970-01-01
相关资源
最近更新 更多