c# - 使用 POST(json 数据)调用 REST 服务方法时出现错误请求错误?

标签 c# wcf rest wcf-binding wcf-rest

您好,我是 RESTful WCF 的新手,我正在尝试使用 POST 对 web 服务方法进行简单调用,这是我的代码

Service Interface code

 [ServiceContract]
public interface IJsonSave
{

    [OperationContract]
    [WebInvoke(UriTemplate = "/SaveJason", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json,
        Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    string SaveJason(string value);
}

web.config文件代码

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
   <behaviors>
     <serviceBehaviors>
       <behavior name ="servicebehavior">
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above 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="true"/>
   </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="restbehavior">
      <webHttp/>
    </behavior>
  </endpointBehaviors>
</behaviors>
<services>
  <service name ="JsonDataSave.JsonSave"
           behaviorConfiguration ="servicebehavior" >
    <endpoint name ="SOAPEndPoint"
              contract ="JsonDataSave.IJsonSave"
              binding ="basicHttpBinding"
              address ="soap" />
    <endpoint name ="RESTEndPoint"
              contract ="JsonDataSave.IJsonSave"
              binding ="webHttpBinding"
              address ="Rest"
              behaviorConfiguration ="restbehavior" />
    <endpoint contract="IMetadataExchange"
              binding="mexHttpBinding"
              address="mex" />
  </service>
</services>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
  <modules runAllManagedModulesForAllRequests="true"/>
 </system.webServer>

</configuration>

我尝试使用 fiddler 她是我的标题

POST http://localhost:50267/JsonSave.svc/Rest/SaveJason
User-Agent: Fiddler
Content-Type: application/json
Data-Type: json
Host: localhost:50267

请求正文:

 ({"value":"asdfasd"})

它给出了 HTTP/1.1 400 Bad Request 的错误,并且在启用调试细节时它给出了以下堆栈跟踪

The server encountered an error processing the request. The exception message is 'Error    in deserializing body of request message for operation 'SaveJason'. The OperationFormatter could not deserialize any information from the Message because the Message is empty (IsEmpty = true).'. See server logs for more details. The exception stack trace is: 
at System.ServiceModel.Dispatcher.PrimitiveOperationFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.CompositeDispatchFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc) at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)

我在谷歌上搜索了很多并尝试了所有可用的解决方案,但它仍然没有工作任何帮助都会很棒。它适用于 SOAP 只是给 REST 错误!

最佳答案

为了收集有关数据流和错误位置的其他信息,您可能会受益于在服务端启用 WCF 跟踪。以下链接应提供足够的详细信息:http://msdn.microsoft.com/en-us/library/ms733025(v=vs.110).aspx

关于您的场景中的错误,我怀疑客户端和服务的消息编码设置不一致。我想知道是否可能:

BodyStyle = WebMessageBodyStyle.WrappedRequest

应该是:

BodyStyle = WebMessageBodyStyle.Wrapped

BodyStyle = WebMessageBodyStyle.Bare

因此请求和响应具有相同的包装样式。

问候,

关于c# - 使用 POST(json 数据)调用 REST 服务方法时出现错误请求错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20325516/

相关文章:

c# - 由于 Service 的 'FaultedState',WCF.ServiceChannel 无法通信

vb.net - DataContract WCF 中的命名空间

c# - 从同一进程托管和使用 WCF 服务

android - 如何降低 Google Map Api 成本或优化跟踪和 api 命中的方法

java - Jersey REST Web 服务在 Struts 2 中不工作

c# - System.Decimal 是否一致?

c# - 递归获取字符串的形式

c# - 向 C# 公开 C++ API

c# - WPF 将控件的 zindex 绑定(bind)到属性不起作用

c# - 通过事件动态更新 rest api c#