我正在尝试将一些 JSON 数据发送到我在 c# 和 wcf 中创建的服务。在提琴手我的 POST 请求如下
提琴手:
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 db。
[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>
在提琴手中,当我单击“执行”时,收到错误 415“不支持的媒体类型”。我目前的想法是,在我的投影仪服务类中,也许我应该创建一个响应对象并将 200 代码发回?!