簡體   English   中英

使用WebGet時WCF服務返回400錯誤

[英]WCF Service Returns 400 error when using WebGet

我對WCF不太了解。 但是我有一個非常基本的服務正在嘗試執行。 我的服務代碼如下:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
[ServiceBehavior(IncludeExceptionDetailInFaults = false)]
[ServiceContract]
public class MyService
{
    [OperationContract]
    [WebGet(UriTemplate = "/IsValidRequest")]
    public bool IsValidRequest()
    {
        return true;
    }
}

就像我說的,這是一項非常基本的服務。 當我在瀏覽器中輸入“ http:// localhost:[port] /MyService.svc”時,我看到了服務描述頁面。 但是,“ IsValidRequest”沒有像我想的那樣列出(也許僅在.asmx上發生)。 無論哪種方式,當我在瀏覽器中輸入“ http:// localhost:[port] /MyService.svc/IsValidRequest”時,都不會返回任何內容。 在Fiddler中,我看到出現HTTP 400錯誤。 但是,沒有什么讓我對問題可能有什么產生任何懷疑。

有人可以幫我指出正確的方向嗎? 謝謝!

使用以下配置(更改名稱空間以匹配您的代碼)

<services>
  <service name="WcfService1.MyService" behaviorConfiguration="GetBehavior">
    <endpoint address="" binding="webHttpBinding" behaviorConfiguration="WebBehavior"  contract="WcfService1.MyService">

    </endpoint>

  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="GetBehavior">

      <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="WebBehavior">
      <webHttp />          
    </behavior>
  </endpointBehaviors>

</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="false" aspNetCompatibilityEnabled="true"/>

您的.svc文件應如下所示:

<%@ ServiceHost Language="C#" Debug="true" Service="WcfService1.MyService" CodeBehind="MyService.svc.cs" %>

在此處輸入圖片說明

查看消息的WSDL是無關緊要的,因為正如您在對評論的回復中所說,您希望它是基於REST的服務,而WSDL是SOAP構造。 在此基礎上,如果您有<serviceMetadata>行為,則應該刪除它,因為這與SOAP元數據有關。

為了診斷這樣的問題,您應該打開WCF中的跟蹤功能( 這里有一個簡短的截屏視頻,向您展示了如何做到)。 這應該突出顯示處理消息時出現的問題

要連接REST管道而不在服務的配置中添加部分,請在system.serviceModel部分下的配置文件中添加以下內容

<protocolMapping>
  <add scheme="http" binding="webHttpBinding"/>
</protocolMapping>
<behaviors>
  <endpointBehaviors>
    <behavior>
      <webHttp/>
    </behavior>
  </endpointBehaviors>
</behaviors>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM