嘗試創建簡單的服務以通過遵循幾個教程返回簡單的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>
我剛剛複製並粘貼文件,正如你所說!你救了我的一天!優秀!非常感謝! – user1087261