Error 415 when sending JSON data to the WCF service

I am trying to send some JSON data to a service created in C # and wcf. In Fiddler, my POST request is as follows

Violinist:

Request Header User-Agent: Fiddler Host: localhost Content-Length: 29 Content-Type: application/json; charset=utf-8 Request Body {sn:"2705", modelCode:1702 } 

The following is the service interface. To use the POST request, the WebInvoke attribute is used.

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

The service interface implementation takes the variables passed into the argument and uses ado to send this data to 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 multiple parameters

 [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> 

In the violinist, when I click run, I get error 415, "unsupported media type." My current thought is that in my Projector service class, maybe they need to create a response object and send the 200 code ?!

+7
source share
2 answers

In order for the WCF endpoint to recognize [WebInvoke] (or [WebGet] ) annotations, it must be defined as a WebHTTP endpoint (also a REST endpoint), which means using the webHttpBinding and webHttp endpoint behavior, since you did not define any endpoints in web.config, it uses the default binding for the schema, which is BasicHttpBinding . Endpoints with this binding only respond to XML (SOAP) requests, so you get this error when sending a JSON request.

Try replacing the <system.serviceModel> section in the web.config file, which should determine your endpoint as needed:

  <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> 
+8
source

The problem is most likely that your service method requires 2 parameters. Only one body parameter may be present.

You can fix this in two ways. First way: Add BodyStyle = WebMessageBodyStyle. Processing your WebInvoke attributes, for example:

 [WebInvoke(Method = "POST", UriTemplate = "projectors", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)] void RecordBreakdown(string sn, int modelCode); 

Second way: create a class for POST data. For example, you can create a class for all the variables that you want to place in the JSON service. To create classes from JSON you can use Json2CSharp (or Insert JSON as classes in VS2012 Update 2). Next json

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

will result in the following class:

 public class Record { public string sn { get; set; } public int modelCode { get; set; } } 

After that, you will have to change your methods to accept this class as follows:

 void RecordBreakdown(string sn, int modelCode); 

in

 void RecordBreakdown(Record record); 

After that, you can now send JSON to the service.

Hope this helps!

Edit: Joining my answer below with this.

After reviewing your configuration file again, it also looks like an invalid binding configuration. The default binding for WCF is "bassicHttpBinding", which mainly uses SOAP 1.1.

Since you want to use JSON and the RESTFul service, you will have to use "webHttpBinding". Here is a link to the basic configuration that we always use for our RESTFul services. You can set the security mode to "Transport" when safe transport is needed (for example, https).

 <security mode="Transport"></security> 
+5
source

All Articles