Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将JSON发布到WCF Rest服务时出错_C#_Wcf_Rest - Fatal编程技术网

C# 将JSON发布到WCF Rest服务时出错

C# 将JSON发布到WCF Rest服务时出错,c#,wcf,rest,C#,Wcf,Rest,我有一个WCF Rest服务,我正在尝试用Firefox Rest客户端测试它 我正在调用的方法的签名是 [WebInvoke(Method = "POST", UriTemplate = "UploadEvidenceData/{userName}/{password}/{cdb}")] public EvidenceData UploadEvidenceData(string userName, string password, string cdb, Evidenc

我有一个WCF Rest服务,我正在尝试用Firefox Rest客户端测试它

我正在调用的方法的签名是

        [WebInvoke(Method = "POST", UriTemplate = "UploadEvidenceData/{userName}/{password}/{cdb}")]
    public EvidenceData UploadEvidenceData(string userName, string password, string cdb, EvidenceData evidence)
证据的数据契约是

    [DataContract(Name = "EvidenceData", Namespace = "http://StudentEvidence.com")]
public class EvidenceData
{
    [DataMember]
    public string LearnerID { get; set; }

    [DataMember]
    public string UnitID { get; set; }

    [DataMember]
    public string AssessorID { get; set; }

    [DataMember]
    public int MethodO { get; set; }  //int

    [DataMember]
    public int MethodEwt { get; set; }  //int

    [DataMember]
    public int MethodEp { get; set; }  //int

    [DataMember]
    public int MethodQc { get; set; }  //int

    [DataMember]
    public int MethodEps { get; set; }  //int

    [DataMember]
    public int MethodPd { get; set; }  //int

    [DataMember]
    public int MethodS { get; set; }  //int

    [DataMember]
    public int MethodEwa { get; set; }  //int

    [DataMember]
    public int MethodEch { get; set; }  //int

    [DataMember]
    public int MethodEwe { get; set; }  //int

    [DataMember]
    public int MethodApel { get; set; }  //int

    [DataMember]
    public int EvidenceID { get; set; }  //int

    [DataMember]
    public string EvidenceFileName { get; set; }

    [DataMember]
    public int EvidenceFileSize { get; set; }  //int

    [DataMember]
    public string EvidenceDescription { get; set; }

    [DataMember]
    public string AssessorFeedback { get; set; }

    [DataMember]
    public string Signature { get; set; }

    [DataMember]
    public int RequirementID { get; set; }  //int

    [DataMember]
    public int ModuleID { get; set; }  //int

    public EvidenceData()
    {
        this.LearnerID = string.Empty;
        this.UnitID = string.Empty;
        this.AssessorID = string.Empty;
        this.MethodO = 0;
        this.MethodEwt = 0;
        this.MethodEp = 0;
        this.MethodQc = 0;
        this.MethodEps = 0;
        this.MethodPd = 0;
        this.MethodS = 0;
        this.MethodEwa = 0;
        this.MethodEch = 0;
        this.MethodEwe = 0;
        this.MethodApel = 0;
        this.EvidenceID = 0;
        this.EvidenceFileName = string.Empty;
        this.EvidenceFileSize = 0;
        this.EvidenceDescription = string.Empty;
        this.AssessorFeedback = string.Empty;
        this.Signature = null;
        this.RequirementID = 0;
        this.ModuleID = 0;
    }
}
当我使用客户机时,使用地址

在我的身体里

{"AssessorFeedback":"Nothing to report","AssessorID":"32fac8c2-0f9b-4cad-a2e4-55b06e968da0","EvidenceDescription":"some evidence","EvidenceFileName":"Lighthouse.jpg","EvidenceFileSize":561276,"EvidenceID":0,"LearnerID":"61b9eca6-6c3e-4bd3-8963-69d9ad3e5eeb","MethodApel":0,"MethodEch":0,"MethodEp":0,"MethodEps":0,"MethodEwa":0,"MethodEwe":0,"MethodEwt":0,"MethodO":0,"MethodPd":0,"MethodQc":0,"MethodS":0,"ModuleID":1131,"RequirementID":30476,"Signature":null,"UnitID":"c33ea6e1-ac54-46dc-a512-3a2efafd5a0b"}
我有自定义的application/json头

这一切在.net客户端上都能正常工作,但在Firefox400的rest客户端插件上却不能

非常感谢您的帮助

我的Web配置文件

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
    <httpRuntime maxRequestLength="2147483647"/>
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </assemblies>
    </compilation>
  </system.web>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </modules>
  </system.webServer>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
    <standardEndpoints>
      <webHttpEndpoint>
        <standardEndpoint name=""
                          helpEnabled="true"
                          automaticFormatSelectionEnabled="true"
                          defaultOutgoingResponseFormat="Json"
                          maxReceivedMessageSize="2147483647"
                          maxBufferSize="2147483647"
                          transferMode="Buffered" />

      </webHttpEndpoint>
    </standardEndpoints>
    <services></services>
  </system.serviceModel>
</configuration>

400通常意味着错误的Http方法。Firefox客户端是否使用GET(这需要在方法上使用不同的属性,并对web.config进行调整以允许web GET),或者您确定它是POST

此外,请检查此内容类型问题:


另请查看将
RequestFormat=WebMessageFormat.Json
添加到装饰服务方法的
[WebInvoke]
属性是否有帮助。我认为这默认为xml,这可能是.NET客户端发送的内容

不要-只是删掉你的签名:@JcFx一个简单的“谢谢”并不伤人,实际上是礼貌的。建议的编辑应该是实质性的改进,可以解决文章中的多个问题。@JcFx虽然您提供的链接也是有效的,但请看这里:无论如何,非常感谢您对本网站的承诺。@tombom我自己的“提前感谢”,加上名字,已从我的第一篇文章中删除()。从那以后,我已经看到了从其他帖子中删除的任何数量的感谢+姓名标志。我知道我们根本不应该在柱子上签字。我个人认为,只要他们不自我宣传,允许签名和感谢会提高网站的文明程度,但最重要的是,任何规则都应该始终如一地应用。我很乐意向你在这件事上的丰富经验低头。@JcFx好吧,有两个反对删除“谢谢”的理由:1。这会给审阅编辑2的其他人带来不必要的工作。在这个网站上搜索“谢谢”,你可以找到成千上万的问题/答案,所以有时候这样的编辑只是为了提高声誉。当然我不会把这归咎于你。感谢你的回复和链接,我已经在使用application/json了。我之所以使用POST,是因为它们的复杂类型是作为JSON在请求体中传递的,这就是为什么它不会出现在UriTemplate中?因此,如果我是正确的,GET将不起作用,这就是我不使用GET的原因。没错,您不应该使用GET(它在该配置下不起作用)。我只是想知道Firefox客户端是否默认使用GET?另外,您可能会发布Web.Config—我认为问题更可能出现在服务配置中,而不是实现中。另外-检查是否启用了跨域访问:我不确定Firefox测试客户端似乎是从哪个“域”调用的,但这是另一件在过去给我带来400错误的事情。上面发布的web配置,Firefox客户端默认为GET,但我改为POST。我看不出哪里出了问题,但希望有了这些信息,其他人也能做到。最后一个想法。您可以尝试将RequestFormat=WebMessageFormat.Json添加到WebInvoke属性中