我正尝试将一些JSON数据发送到我在c#和wcf中创建的服务。在我的提琴手POST请求如下:将JSON数据发布到WCF服务时出现错误415
提琴手:
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);
}
服务接口的实现需要到参数传递的变量和使用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" %>
Web.config文件:
<?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>
在小提琴手,当我点击“执行“,我收到错误415”不支持的媒体类型PE”。我目前的想法是,在我的Projector服务类中,也许我应该创建一个响应对象并发回200代码?!
多数民众赞成在工作。我认为web绑定是使用svc文件中的工厂方法自动生成的(我还没有包含它)。除了这些更改,我需要将'BodyStyle = WebMessageBodyStyle.Bare'添加到服务方法中,因为JSON数据未包含在另一个对象中。 – beaumondo