2011-12-14 99 views
2

我一直在設計.NET 4.0中的WCF REST服務。我的GET請求正常工作,但涉及將數據發佈到服務器的任何請求都失敗,出現HTTP 400 Bad RequestWCF REST 4.0 PUT/POST/DELETE不起作用(400錯誤請求)

這是我的簡單的服務:

[ServiceContract] 
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] 
public class Service1 
{ 
    [WebGet(UriTemplate = "")] 
    public string HelloWorld() 
    { 
     return "hello world"; 
    } 

    [WebInvoke(UriTemplate = "", Method = "POST")] 
    public string HelloWorldPost(string name) 
    { 
     return "hello " + name; 
    } 
} 

我的web.config:

<?xml version="1.0"?> 
<configuration> 

    <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"/> 
    <protocolMapping> 
     <add scheme="http" binding="webHttpBinding" />  
    </protocolMapping> 
    <standardEndpoints> 
     <webHttpEndpoint> 
     <!-- 
      Configure the WCF REST service base address via the global.asax.cs file and the default endpoint 
      via the attributes on the <standardEndpoint> element below 
     --> 
     <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/> 
     </webHttpEndpoint> 
    </standardEndpoints> 
    </system.serviceModel> 

</configuration> 

而且我的Global.asax:

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

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

基本上,一切都是從模板默認,但我剛剛簡化了Service1。我已經嘗試通過調試器運行它並通過Fiddler傳遞請求並在IIS中運行它並執行相同的操作,以及使用簡單的控制檯應用程序來僞造POST,但我總是得到400 Bad Request錯誤,我不知道爲什麼。我已經看遍了整個互聯網,無法找到任何東西。

我試圖同時滿足以下兩個要求的例子(沒有工作):

XML:

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">String content</string> 

JSON:

"String content" 
+0

@爲什麼「rest」標籤被移除,這篇文章是關於REST網絡服務的...... – shuniar 2011-12-16 16:16:22

+0

這篇文章是關於HTTP,WCF,ASP,.NET等的問題。它與REST架構。這是一個實現問題,而不是設計或體系結構問題。 – 2011-12-16 16:56:35

+0

HelloWorld的GET版本是否工作? – 2011-12-18 18:31:05

回答

6

你是否在正確設置Content-Type頭的請求?對於XML請求應該是text/xml,對於JSON應該是application/json。當我在Fiddler中設置Content-Type時,您的代碼適用於我。

你也應該設置Accept頭在你的GET要麼text/xmlapplication/json這取決於你想在響應的格式。這是正常的POST作爲服務器會認爲你想在同一響應格式作爲請求,因爲您在web.config中設置了automaticFormatSelectionEnabled="true"。有關WCF REST格式選擇的更多詳細信息,請參閱:http://blogs.msdn.com/b/endpoint/archive/2010/01/18/automatic-and-explicit-format-selection-in-wcf-webhttp-services.aspx

1

您的屬性不應該在實施中,它們應該在操作合同中。您還需要確保您擁有UriTemplate中包含的任何命名參數。它們區分大小寫,因此它們必須完全匹配。

IService.cs

[ServiceContract] 
public class IService1 
{ 
    [WebGet(UriTemplate = "")] 
    [OperationContract] 
    public string HelloWorld(); 

    [WebInvoke(UriTemplate = "/{name}", Method = "POST")] 
    [OperationContract] 
    public string HelloWorldPost(string name); 
} 

Service.cs

public class Service1 : IService 
{ 

    public string HelloWorld() 
    { 
     return "hello world"; 
    } 

    public string HelloWorldPost(string name) 
    { 
     return "hello " + name; 
    } 
} 

你需要在你的web.config文件配置服務以及System.ServiceModel

<system.serviceModel> 
    <services> 
     <service name="Service1"> 
     <endpoint address="basic" binding="basicHttpBinding" contract="IService1" /> 
     </service> 
    <services> 
</system.serviceModel> 

這是一些主要概念,應該讓你開始朝正確的方向發展。如果你想要一個好的測試項目,只需在VS2010中使用「WCF Application」項目模板即可。它包含了大部分所需的部件。希望這可以幫助!