错误415不能发送json到wcf服务
问题描述:
我有一个问题 - 我需要通过POST发送json,但我的错误是“错误(415)无法处理消息,因为内容类型application/xml”,显然存在一些不匹配但我找不到它。这里是我的web.config错误415不能发送json到wcf服务
<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>
答
首先,你需要获得一个代理读者看到你实际发送,我建议:Fiddler。然后你可以将它与预期的相匹配。
我也建议你的网络服务ServiceStack,他们提供了对象发送/接收的优秀处理。但是如果你太过分了,你仍然可以使用ServiceStack的卓越对象来转换JSON。关于ServiceStack整洁的事情是没有配置文件。
答
得到与 PUT HTTP动作相同的错误 - 解决方案是添加以下请求报头:
接受:应用/ JSON
内容类型:应用/ JSON; charset = utf-8