2011-12-08 62 views
8

嘗試創建簡單的服務以通過遵循幾個教程返回簡單的JSON字符串。我遇到兩臺不同的機器,它們帶有HTTP Status Code 400錯誤請求。 實例教程 REST風格的WCF服務使用JSON pt.1 & pt.2 - http://www.youtube.com/watch?v=5BbDxB_5CZ8C#4.0 WCF REST JSON - HTTP GET CODE 400錯誤請求

我也有谷歌和搜索這裏(的StackOverflow)爲沒有成功類似的問題。

問題是,當我嘗試執行健全性檢查以瀏覽WCF服務並執行該方法時,我得到了400錯誤的請求。通過編譯服務並瀏覽此地址:http://localhost:49510/Service1.svc/GetPerson 就像教程。我曾嘗試爲3天內尋找解決方案。任何幫助表示讚賞。

這就是我所做的。

首先我創建一個簡單的WCF服務應用程序的新項目。我刪除默認Service1.svc並添加一個新的WCF服務,產生了新的Service1.svc和IService1.cs

下面是接口的代碼(IService1.cs

namespace WcfService1 
{ 
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together. 
    [ServiceContract] 
    public interface IService1 
    { 
     [OperationContract] 
     [WebInvoke(Method="GET", BodyStyle=WebMessageBodyStyle.Bare, ResponseFormat=WebMessageFormat.Json, RequestFormat=WebMessageFormat.Json, UriTemplate="GetPerson")] 
     Person GetPerson(); 
    } 

    [DataContract(Name="Person")] 
    public class Person 
    { 
     [DataMember(Name="name")] 
     public string Name { get; set; } 
    } 
} 

下面是Service1.svc

namespace WcfService1 
{ 
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together. 
    public class Service1 : IService1 
    { 
     public Person GetPerson() 
     { 
      return new Person() { Name = "Tobbe" }; 
     } 
    } 
} 

代碼和Web.config文件是原封不動,喜歡看這個的web.config

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

    <system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
    </system.web> 
    <system.serviceModel> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> 
      <serviceMetadata httpGetEnabled="true"/> 
      <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> 
      <serviceDebug includeExceptionDetailInFaults="false"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 
<system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    </system.webServer> 

</configuration> 

回答

11

休息WCF你需要做的約束力和終點設置在web.config中

替換您的整個網頁。配置由以下,也將努力

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

    <system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
    </system.web> 
    <system.serviceModel> 
    <protocolMapping> 
     <add scheme="http" binding="webHttpBinding"/> 
    </protocolMapping> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> 
      <serviceMetadata httpGetEnabled="true"/> 
      <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> 
      <serviceDebug includeExceptionDetailInFaults="false"/> 

     </behavior> 
     </serviceBehaviors> 
     <endpointBehaviors> 
     <behavior> 
      <webHttp/> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 
<system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    </system.webServer> 
</configuration> 

您有以下兩件事情

進行剩餘

使用的WebHttpBinding(更改默認http端口映射的WebHttpBinding)

<system.serviceModel> 
    <protocolMapping> 
     <add scheme="http" binding="webHttpBinding"/> 
    </protocolMapping> 
    <behaviors> 

<system.serviceModel> 

指定webHttp終點行爲

<system.serviceModel> 
    ----- 
    </protocolMapping> 
    <behaviors> 
     <endpointBehaviors> 
      <behavior> 
       <webHttp /> 
      </behavior > 
     </endpointBehaviors> 
    <behaviors> 
    ------ 
<system.serviceModel> 
+0

我剛剛複製並粘貼文件,正如你所說!你救了我的一天!優秀!非常感謝! – user1087261

4

您沒有指定任何端點...默認情況下,在WCF 4上,將使用使用basicHttpBinding的端點。它不會在這裏工作,因爲它是一個基於SOAP的綁定。你想用什麼的WebHttpBinding是基於REST ...

下面是如何覆蓋默認與WCF 4結合:

<system.serviceModel> 
    <protocolMapping> 
    <add scheme="http" binding="webHttpBinding"/> 
    </protocolMapping> 
</system.serviceModel> 

您還可以通過在加入該端點行爲,以使您的webHttp配置:

<behaviors> 
    <endpointBehaviors> 
     <behavior> 
      <webHttp /> 
     </behavior > 
    </endpointBehaviors> 
<behaviors> 

http://msdn.microsoft.com/en-us/library/bb924425.aspx

0

我不完全知道爲什麼,但是當我加入「廠」屬性我.SVC文件(你需要明確地將其拖動到Visual Studio),一切都只是工作 - 無需任何改動默認Web.config中的設置!

我加廠= 「System.ServiceModel.Activation.WebServiceHostFactory」所以我.SVC文件,從這個去:

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

這樣:

<%@ ServiceHost Language="C#" Debug="true" Service="ServiceNameSpace.ServiceName" CodeBehind="ServiceName.svc.cs" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>

的只有副作用似乎是,當您在瀏覽器中單擊.SVC文件時,您會收到'未找到端點'錯誤,但服務在您處於無論如何,它正確地調用它。如前所述,我在.NET 4.6(Simplified WCF configuration)中使用了一個默認的Web.config,因此我可能還需要添加端點詳細信息以便再次運行。

給主持人的聲明:我很抱歉在幾個問題上發表這個答案。不會再做。但是,我不認爲從兩個問題中刪除它是非常平衡的。這就是爲什麼我只在這裏重新發布這個答案。

相關問題