I am trying to send some JSON data to a service I have created in c# and wcf. In Fiddler my POST request is as follows
Fiddler:
Request Header
User-Agent: Fiddler
Host: localhost
Content-Length: 29
Content-Type: application/json; charset=utf-8
Request Body
{sn:"2705", modelCode:1702 }
Below is the service interface. That uses the WebInvoke attribute to manage the POST request
[ServiceContract]
public interface IProjectorService
{
[WebInvoke(Method="POST", UriTemplate="projectors", RequestFormat=WebMessageFormat.Json, ResponseFormat=WebMessageFormat.Json )]
[OperationContract]
void RecordBreakdown(Record record);
}
The implementation of the service interface takes the variables passed in to the argument and uses ado to send this data to a 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 object to represent the multiple parameters
[DataContract]
public class Record
{
[DataMember]
public string SerialNumber { get; set; }
[DataMember]
public int ModelCode { get; set; }
}
.svc file
<%@ 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>
In fiddler, when I click on "execute", I receive an error 415 "unsupported media type". My current line of thought is that in my Projector service class, maybe im supposed to create a response object and send the 200 code back?!