2011-10-13 106 views
1

我有WCF REST/JSON服務,我使用this模板創建它。在我的服務,我有一個方法如何從客戶端應用程序調用WCF REST/JSON服務

[WebInvoke(UriTemplate = "Create", Method = "*",RequestFormat = WebMessageFormat.Json,BodyStyle = WebMessageBodyStyle.Bare)] 
    public void Create(PictureData pictureData) 
    { 
     var context = new EFDBContext(); 
     context.PictureData.Add(pictureData); 
     context.SaveChanges(); 
    } 

PictureData這是我的實體數據,這是我嘗試通過EF在DB保存。

在我的WPF客戶端應用程序,我嘗試調用這個方法:

​​

但沒有發生

  • 我也嘗試使用method = 「POST」 在WebInvoke屬性
  • 此外,我嘗試使用HttpClient中沒有「創建」的地址,然後在客戶端中使用它。第一個參數

UPDATE

後,我嘗試這個

var dataContract = HttpContentExtensions.CreateJsonDataContract(pictureData, typeof (PictureData)); 
     var client = new HttpClient(); 
     using(var response = client.Post("http://localhost:8080/ScreenPictureService/Create", dataContract)) 
     { 
      response.EnsureStatusIs(HttpStatusCode.OK); 
     } 

我收到錯誤請求400

更新2 我發現我的問題:

  • 我用JSON.NET來序列化我的對象,並且當我收到字節數組時,它將轉換爲base64格式,但是我的服務期望使用字節數組 - 它使用字節列表解決。

  • 第二個問題 - 我試圖以高清晰度接收我的平板電腦的視頻,並且我有相同的響應(錯誤請求400),如果我將圖片分辨率更改爲800x600,服務運行良好,並且存在我的問題 - 如何增加請求消息的配額。我嘗試喲使用,裏面standardEndpoint節(web.config中)

readerQuotas的MaxArrayLength = 「2147483647」 maxBytesPerRead = 「2147483647」 MAXDEPTH = 「2147483647」 maxNameTableCharCount = 「2147483647」 maxStringContentLength = 「2147483647」

但它不起作用

+0

你是什麼意思?有沒有錯誤?你有沒有檢查事件日誌? –

+0

我的意思是,沒有收到錯誤。 –

回答

0

您是否嘗試過使用Fiddler這樣的工具監視確切的請求/響應?也許你的帖子不像你期望的那樣?

WCF服務是否知道接受REST?如果您使用的不是WCF.WebApi通常有可怕的WCF綁定配置,例如:

<service name="MyWcfServiceWebRole.xyz.IAbcService"> 
    <endpoint address="" behaviorConfiguration="webby" binding="webHttpBinding" bindingConfiguration="RestBinding" contract="MyWcfServiceWebRole.xyz.IAbcService" /> 
</service> 

<behaviors> 
    <endpointBehaviors> 
     <behavior name="webby"> 
     <webHttp /> 
     </behavior> 
    </endpointBehaviors> 
</behaviors> 

做一個簡單的REST來上班啊?

+0

我嘗試使用Fiddler進行監控,它也收到錯誤的請求400.是的,簡單的REST運行良好。 –

0

錯誤400錯誤的請求可能是由於許多可能性。嘗試對您的服務啓用跟蹤。可以通過鏈接here完成。

此外,如果你有配置的配置文件請確保您有readerQuotas sepecified如下,如果你正在使用的WebHttpBinding:

<webHttpBinding> 
    <binding name="RestBinding"> 
     <readerQuotas maxStringContentLength="5242880" maxArrayLength="16384" 
     maxBytesPerRead="4096" /> 
     <security mode="None"> 
     <transport clientCredentialType="None" /> 
     </security> 
    </binding> 
    </webHttpBinding> 

如果您使用的是REST API與全球路線定義爲您服務.asax並使用下面的標準端點使用:

<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"> 
    <readerQuotas maxStringContentLength="5242880" maxArrayLength="4194304" 
      maxBytesPerRead="4194304" /> 
</standardEndpoint> 
相關問題