2013-05-28 123 views
7

我正尝试将一些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代码?!

回答

8

对于WCF端点识别[WebInvoke](或[WebGet])注释,它需要被定义为一个WebHTTP端点(也称为REST端点) - 这意味着使用webHttpBindingwebHttp端点行为。由于您没有在web.config中定义任何端点,因此它使用该方案的默认绑定,即BasicHttpBinding。使用该绑定的端点仅响应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> 
+0

多数民众赞成在工作。我认为web绑定是使用svc文件中的工厂方法自动生成的(我还没有包含它)。除了这些更改,我需要将'BodyStyle = WebMessageBodyStyle.Bare'添加到服务方法中,因为JSON数据未包含在另一个对象中。 – beaumondo

5

问题很可能是您的服务方法需要2个参数。默认只有1个身体参数可以存在。

你可以通过两种方式解决这个问题。 第一种方式: 添加BodyStyle = WebMessageBodyStyle.Wrapped您WebInvoke属性,像这样:

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

方式二:为POST数据创建一个类。例如,您可以为想要作为JSON发布到服务的所有变量创建一个类。要从JSON创建类,您可以使用Json2CSharp(或将JSON粘贴为VS2012 Update 2中的类)。 以下JSON

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

会导致下面的类:

之后,你将不得不改变你的方法接受这一类,像这样:

void RecordBreakdown(string sn, int modelCode); 

到:

void RecordBreakdown(Record record); 

之后,你现在应该可以发布JSON到服务。

希望这会有所帮助!

编辑: 加入我的答案从下面这个。

再次查看您的配置文件后,它也看起来像一个绑定错误配置。WCF的默认绑定是基本上使用SOAP 1.1的'bassicHttpBinding'。

既然你想使用JSON和RESTFul服务,你将不得不使用'webHttpBinding'。 Here is a link到我们总是用于我们的RESTFul服务的基本配置。当需要安全传输(f.e .: https)时,您可以将安全模式设置为传输。

<security mode="Transport"></security> 
+0

我曾尝试加入BodyStyle = WebMessageBodyStyle.Wrapped,但并未奏效。仍然收到415错误。我也删除了多个参数,并根据您的建议创建了一个新类,我更喜欢这种方式,我认为我已更新问题以显示这些更改,但这也不起作用,即使仍然看到错误415. – beaumondo

+1

再次查看你的配置文件,它也看起来像一个绑定错误配置。 WCF的默认绑定是基本上使用SOAP 1.1的'bassicHttpBinding'。既然你想使用JSON和RESTFul服务,你将不得不使用'webHttpBinding'。我已经包含了一个链接,指向我们通常用于RESTFul服务的基本配置。 :)信息:[链接](http://pastebin.com/Yj4bdrHp) – Xciles

相关问题