2011-09-20 260 views
4

我創建使用Javascript/jQuery的一個簡單的函數WCF 400錯誤的請求

 [OperationContract] 
     [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json)] 
     string Start(); 

定義,

 public String Start() 
     { 
      JavaScriptSerializer serializer = new JavaScriptSerializer(); 
      return serializer.Serialize("Check"); 
     } 

從瀏覽器, http://localhost/service1.svc告訴我,我已經創建了一個服務和其他所有信息..看起來很好。
我試圖把這種使用 http://localhost/service1.svc/Start

我得到這個呼叫的400錯誤的請求。我希望我在這裏做的事情沒有完全錯誤。我應該能夠從瀏覽器訪問WCF服務嗎? 在我想要發佈之前,我嘗試了很多。但我無法得到這個基本的東西令我感到沮喪。

編輯& UPDATE 現在我在這個階段。該服務頁面告訴我,元數據服務被禁用,並要求我插入下面的文本

<serviceMetadata httpGetEnabled="true" /> 

我插入的文本 - 但它畢竟顯示了相同的文字!現在,這是越來越太混亂..

+0

你有與狀態代碼的任何有效載荷?如果否,請嘗試打開將錯誤消息發送回客戶端的選項。它在ServiceBehaviors/Behavior元素下是。 –

+0

它設置爲true。但我沒有得到任何有效載荷。只是狀態碼。 – Tom

+0

@ a.dimo是對的,你指定你的方法只能用POST方法調用。當您使用瀏覽器請求時,它會發送GET請求。 –

回答

1

嘗試使用GET改變後並重新啓動要求

+0

Nopes。這沒有幫助。我已經嘗試過每一種組合 - 開始,開始(int id),GET和POST – Tom

+0

你爲什麼認爲將POST改爲GET會有所幫助? – Tom

1

爲我工作。我創建了WCF Rest服務。

我使用的URL看起來像http://localhost:8080/Service1/Start

下面是代碼:

using System.ServiceModel; 
using System.ServiceModel.Activation; 
using System.ServiceModel.Web; 
using System.Web.Script.Serialization; 

namespace WcfRestService1 
{ 
    // Start the service and browse to http://<machine_name>:<port>/Service1/help to view the service's generated help page 
    // NOTE: By default, a new instance of the service is created for each call; change the InstanceContextMode to Single if you want 
    // a single instance of the service to process all calls. 
    [ServiceContract] 
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] 
    // NOTE: If the service is renamed, remember to update the global.asax.cs file 
    public class Service1 
    { 
     [OperationContract] 
     [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)] 
     public string Start() 
     { 
      JavaScriptSerializer serializer = new JavaScriptSerializer(); 
      return serializer.Serialize("Check"); 
     } 
    } 
} 
+0

嗯..我無法確定在我的代碼中出了什麼問題。 – Tom

+0

你創建了什麼樣的項目?只需WCF服務應用程序(在VS中的WCF選項卡下)或WCF REST服務應用程序(在Web選項卡下)。 –

+0

從WCF - > WCF服務應用程序>我做了一些更多的調查,這裏是我發現.. 無法導入WSDL:portType的詳細 – Tom