2011-03-21 165 views
11

尋找一個WCF 4 REST服務一些指導其是基於WCF REST模板40(CS)在VS2010擴展。我花了幾天的時間試圖讓這個玩家工作,審查其他職位,而當我接近時,我似乎無法越過終點線。經過多次挫折之後,它終於到達了服務併發布(使用fiddler請求構建器),但方法參數是空的,但它在請求生成器中正確設置。我猜測這可能是配置問題,但隨着截止日期的到來,我沒有時間進行更多的研究。 FWIW,在調試時,jsonstring變量爲空。自我承認是一種noob問題,因爲這是第一次通過REST爲我,任何幫助將不勝感激!WCF REST服務的JSON數據後

在此先感謝。

的web.config

<system.web> 
    '<compilation debug="true" targetFramework="4.0" /> 
</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> 
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> 
<standardEndpoints> 
    <webHttpEndpoint> 
    <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/> 
    </webHttpEndpoint> 
</standardEndpoints> 
</system.serviceModel> 

的global.asax.cs

public class Global : HttpApplication 
    { 
     void Application_Start(object sender, EventArgs e) 
     { 
     RegisterRoutes(); 
     } 

     private void RegisterRoutes() 
     { 
     RouteTable.Routes.Add(new ServiceRoute("Scoring", new WebServiceHostFactory(), typeof(ScoringSvc))); 
     } 
    } 

服務代碼

[ServiceContract] 
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] 
public class ScoringSvc 
{ 
    [OperationContract] 
    [WebInvoke 
     (Method = "POST", 
     BodyStyle = WebMessageBodyStyle.WrappedRequest, 
     RequestFormat=WebMessageFormat.Json, 
     ResponseFormat=WebMessageFormat.Json)] 
    public string BOB(string jsonstring) 
    { 
     return "Received: " + jsonstring; 
    } 
} 

的Fiddler請求頭

Host: localhost 
Content-Length: 20 
Content-Type: application/json; charset=UTF-8 

請求主體

{"Name":"Frank"} 

原料從小提琴手響應

HTTP/1.1 200 OK 
Cache-Control: private 
Content-Length: 12 
Content-Type: application/json; charset=utf-8 
Server: Microsoft-IIS/7.5 
X-AspNet-Version: 4.0.30319 
X-Powered-By: ASP.NET 
Date: Mon, 21 Mar 2011 21:31:14 GMT 

"Received: " 
+0

忘了提及,解決方案使用IIS 7作爲web服務器而不是apnet調試服務器。 – Grogh 2011-03-21 22:05:25

回答

20

偶然發現這個鏈接WCF + REST: Where is the request data?,看到格倫的響應流傳遞給方法,然後撕裂,除了有一個StreamReader成一個字符串來獲取表單POST數據。

修改原型服務代碼如下

[OperationContract] 
[WebInvoke 
    (UriTemplate="/BOB", 
    Method = "POST", 
    BodyStyle = WebMessageBodyStyle.WrappedRequest)] 
public string BOB (Stream streamdata) 
{ 
    StreamReader reader = new StreamReader(streamdata); 
    string res = reader.ReadToEnd(); 
    reader.Close(); 
    reader.Dispose(); 
    return "Received: " + res; 
} 

而且似乎這樣的伎倆,充分JSON數組在流中傳遞,讀取到本地字符串,然後我可以使用JSON攻擊它.net將序列化到/從字典傳遞到業務邏輯。不是很漂亮,但功能。

+0

發現調用「關閉」和/或「處置」會引起響應,導致服務故障「消息已處理」 – 2017-04-14 12:47:39

-1

你有沒有試過[WebGet(UriTemplate = ..]屬性,而不是帖子看看是否能夠正常工作?下面是一個例子 - http://blogs.msdn.com/b/kaevans/archive/2007/09/04/creating-a-json-service-with-webget-and-wcf-3-5.aspx

+0

我確實有一個定義的webget方法,如預期的那樣返回,目前註釋清理代碼以專注於帖子。我有一個webinvoke方法的地方,並發現了一個帖子在stackoverflow(無法找回它現在),說不包括。在刪除之前,我根本沒有經歷過,當時收到404,刪除後我現在得到200個響應,但沒有數據。如果我將它作爲根(「」)或將端點放在 – Grogh 2011-03-22 02:44:27

+0

中,您似乎沒有關係使用什麼操作系統和版本的IIS或IIS Express或Web開發服務器?應用程序池設置了什麼框架? 404錯誤可能是由於此處相關的問題http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/2ec269e3-c1ff-4d9b-9ff3-d530f1599047 – sep15ms 2011-03-22 02:52:03

+0

2008 r2 sp1 IIS 7與.Net 4通過aspnet_regiis.exe註冊。任何指向IIS 7中添加引用的dll的指針?也試圖在安裝了IIS的win 7 SP1上,但我認爲這是IIS 7.5 ...雖然仍然沒有果汁。 – Grogh 2011-03-22 03:21:30

1

您是否嘗試過進入{「jsonstring」:「弗蘭克」}請求體(內提琴手的請求生成器)?

+0

我認爲這是真正的答案。當包裝時,json被反序列化爲方法參數的名稱。 – 2016-03-03 15:22:53

2

我用這一個工程:

[WebInvoke(ResponseFormat = WebMessageFormat.Json, 
      RequestFormat = WebMessageFormat.Json, 
      BodyStyle = WebMessageBodyStyle.WrappedRequest, 
      Method = "POST", 
      UriTemplate = "setExpositions?shelfId={shelfId}")] 
[OperationContract] 
public bool SetExpositions(int shelfId, List<WcfExposition> expositions) 
{ 
} 

其中shelfId在GET傳遞,並博覽會是在郵件正文中的JSON數據傳遞。

+8

您可以向我們展示一些適用於此操作的示例JSON數據嗎? – 2013-03-05 17:01:34

0

我認爲在BodyStyle = WebMessageBodyStyle.WrappedRequest中可能會出現問題,我認爲雖然文檔完全不清楚,但希望元素可以用方法名稱包裝。

設置解包並將請求正文設置爲'{"Name":"Frank"}'(注意圍繞它的單引號。實際上發佈的是包含JSON的字符串,我不知道爲什麼要這樣做。它讓我想起了http://thedailywtf.com/Articles/XMLd-XML.aspx他們在哪裏把XML放在他們的XML。您正在將JSON放入您的JSON中。