错误415无法将json发送到wcf服务

时间:2013-05-06 18:01:21

标签: c# xml json wcf

我有一个问题 - 我需要通过POST发送json但我的错误是“错误(415)无法处理消息因为内容类型application / xml”,显然存在一些不匹配但我找不到它。这是我的Web.config

    <configuration>
  <system.serviceModel>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    <behaviors>
      <serviceBehaviors>
        <behavior name="CustomBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="restBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <bindings>
      <wsHttpBinding>
        <binding name="customBinding" closeTimeout="23:59:59" openTimeout="23:59:59" receiveTimeout="23:59:59" sendTimeout="23:59:59" maxBufferPoolSize="2147483646" maxReceivedMessageSize="2147483646">
          <readerQuotas maxDepth="64" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
          <security mode="None" />
        </binding>
      </wsHttpBinding>
    </bindings>
    <services>
      <service name="ServiceContracts.Implementation.CR.CRService" behaviorConfiguration="CustomBehavior">
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <endpoint binding="wsHttpBinding" bindingConfiguration="customBinding" contract="ServiceContracts.CR.ICRService" />
        <endpoint binding="webHttpBinding" contract="ServiceContracts.CR.ICRService" behaviorConfiguration="restBehavior" address="rest" />
      </service>
      <service name="ServiceContracts.Implementation.SP1.SPOneService" behaviorConfiguration="CustomBehavior">
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <endpoint binding="wsHttpBinding" bindingConfiguration="customBinding" contract="ServiceContracts.SP1.ISPOneService" />
        <endpoint binding="webHttpBinding" contract="ServiceContracts.SP1.ISPOneService" behaviorConfiguration="restBehavior" address="rest"/>
      </service>
    </services>
  </system.serviceModel>
  <system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="104857600" />
      </requestFiltering>
    </security>
  </system.webServer>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <httpRuntime executionTimeout="240" maxRequestLength="20480" />
  </system.web>

  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel" switchValue="Information, ActivityTracing" propagateActivity="true">
        <listeners>
          <add name="traceListener" type="System.Diagnostics.XmlWriterTraceListener" initializeData="c:\Traces.svclog" />
        </listeners>
      </source>
    </sources>
  </system.diagnostics>
</configuration>

2 个答案:

答案 0 :(得分:1)

首先,您需要获取代理阅读器以查看您实际发送的内容,我建议:Fiddler。然后,您可以将其与预期相匹配。

我建议你的网络服务ServiceStack,他们提供出色的对象发送/接收处理。但是如果你走得太远,你仍然可以将ServiceStack的优秀对象用于JSON转换器。 ServiceStack的优点在于没有配置文件。

答案 1 :(得分:0)

PUT Http Verb有同样的错误 - 解决方法是添加以下请求标题:

  

接受:application / json
  内容类型:application / json;字符集= UTF-8