2012-08-24 94 views
0

我有一個WCF REST服務,我正在嘗試用Firefox的REST客戶端測試,錯誤張貼JSON到WCF 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; 
    } 
} 

,當我使用客戶端使用該地址http://something.com/Service/UploadEvidence/user/pass/mydb

和身體,我把

{"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"} 

和我的應用程序/ JSON

此的自定義頁眉一切工作正常,從一個.NET客戶端,而不是從Firefox 400每次其餘客戶端插件。

任何幫助是非常讚賞

我的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> 
+0

唐 - 只是修剪你的簽名:http://stackoverflow.com/faq#signatures – JcFx

+0

@JCFx一個簡單的「謝謝」不傷害,實際上是禮貌。建議的編輯應該是針對帖子中多個問題的實質性改進。 – fancyPants

+0

@JFFx雖然你提供的鏈接也是有效的,請看看這裏:http://meta.stackexchange.com/questions/139830/reject-or-approve-suggested-edits-for-removed-clutter無論如何謝謝非常感謝您對本網站的承諾。 – fancyPants

回答

0

400往往意味着錯誤的HTTP方法。 Firefox客戶端是否使用GET(這需要您的方法具有不同的屬性,並對您的web.config進行調整以允許Web獲取),或者您確定它是POST?

此外,還要檢查該內容類型的問題:Why does my C# client, POSTing to my WCF REST service, return (400) Bad Request?

而且看是否添加RequestFormat = WebMessageFormat.Json[WebInvoke]屬性裝飾你的服務方法幫助。我認爲這默認爲xml,這可能是.NET客戶端發送的。 http://msdn.microsoft.com/en-us/library/system.servicemodel.web.webinvokeattribute.requestformat.aspx

+0

欣賞響應,鏈接,我已經在使用application/json。我使用POST是因爲它們的複雜類型正在作爲JSON傳遞到請求體中,這就是爲什麼它不會出現在UriTemplate中?所以如果我是正確的GET不會工作,這就是爲什麼我不使用GET。 –

+0

沒錯,你不應該使用get(它不適用於該配置)。我只是想知道Firefox客戶端默認使用GET嗎?另外,你可能會發布你的Web.Config - 我認爲問題更可能出現在服務配置中而不是實現中。此外 - 請檢查是否啓用了跨域訪問:我不確定Firefox測試客戶端似乎在調用哪個「域」,但這是另一個在過去給我一個400錯誤的東西。 – JcFx

+0

在上面發佈了web配置,Firefox客戶端默認爲GET,但我已更改爲POST。 –