如何将 JSON 参数传递给 RESTful WCF 服务?
How to pass a JSON parameter to RESTful WCF service?
我开发了一个 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:如果我传递一个简单的字符串,它就可以工作。
任何想法请
首先我建议您需要使用 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>
我开发了一个 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:如果我传递一个简单的字符串,它就可以工作。 任何想法请
首先我建议您需要使用 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>