c# - 将 JSON 数据发布到 WCF 服务时出现错误 415

标签 c# json wcf rest

我正在尝试将一些 JSON 数据发送到我在 c# 和 wcf 中创建的服务。在Fiddler中我的POST请求如下

fiddler :

Request Header
User-Agent: Fiddler
Host: localhost
Content-Length: 29
Content-Type: application/json; charset=utf-8

Request Body
{sn:"2705", modelCode:1702 } 

下面是服务界面。即使用 WebInvoke 属性来管理 POST 请求

[ServiceContract]
public interface IProjectorService
{
    [WebInvoke(Method="POST", UriTemplate="projectors", RequestFormat=WebMessageFormat.Json, ResponseFormat=WebMessageFormat.Json )]
    [OperationContract]
    void RecordBreakdown(Record record);
}

服务接口(interface)的实现采用传递给参数的变量,并使用 ado 将此数据发送到 SQL 数据库。

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
public partial class ProjectorService : IProjectorService
{

    public void RecordBreakdown(Record record)
    {
        //ado code to send data to db (ie do record.SerialNumber...
    }        
}

表示多个参数的POCO对象

[DataContract]
public class Record
{      
    [DataMember]
    public string SerialNumber { get; set; }
    [DataMember]
    public int ModelCode { get; set; }
}

.svc文件

<%@ ServiceHost Language="C#" Debug="true" Service="ProjectorService" CodeBehind="~/App_Code/ProjectorService.cs" %>

网络配置:

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5"/>
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https"/>
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
  </system.webServer>
</configuration>

在 fiddler 中,当我单击“执行”时,收到错误 415“不支持的媒体类型”。我目前的想法是,在我的 Projector 服务类中,也许我应该创建一个响应对象并将 200 代码发回?!

最佳答案

让 WCF 端点识别 [WebInvoke] (或 [WebGet] )注释,它需要定义为 WebHTTP 端点(又名 REST 端点)——这意味着使用 webHttpBindingwebHttp端点行为。由于您没有在 web.config 中定义任何端点,因此它使用方案的默认绑定(bind),即 BasicHttpBinding .具有该绑定(bind)的端点仅响应 XML (SOAP) 请求,这就是您在发送 JSON 请求时收到该错误的原因。

尝试更换您的 <system.serviceModel>部分在你的 web.config 中,它应该根据你的需要定义你的端点:

  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="Web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <services>
      <service name="ProjectorService">
        <endpoint address=""
                  behaviorConfiguration="Web"
                  binding="webHttpBinding"
                  contract="IProjectorService" />
      </service>
    </services>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>

关于c# - 将 JSON 数据发布到 WCF 服务时出现错误 415,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16792202/

相关文章:

java - 将 Json 对象映射到 POJO 时处理未知的 Json 属性

java - 在 java 中以编程方式构建 JSON

C# WCF 将列表类型作为对象返回时发生异常

c# - 比较字符串的不同组合

c# - 为什么参数对于字符串数组和整数数组的行为不同?

c# - 为什么在对 ItemsSource 进行排序时 ComboBox 会丢失其 SelectedItem?

c# - 如何为自定义对象而不是整个模型调用 Html.Display?

json - 从结构中删除字段或将它们隐藏在 JSON 响应中

wcf - Silverlight WCF + SSL 安全错误 - 从未请求过 crossdomain.xml

c# - WCF 客户端在外部解决方案时不工作