我正在尝试将一些 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 端点)——这意味着使用 webHttpBinding
和 webHttp
端点行为。由于您没有在 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/