我在實現RESTful WCF服務,第一條裂縫,但不能得到它後我的對象:( 它崩潰的clientstuff代碼(見下文)。這可能是什麼?修復由於 爲什麼遠程服務器返回一個錯誤:(400)錯誤的請求。有
部分網頁。配置
<system.serviceModel>
<services>
<service name="MyRest.Service1" behaviorConfiguration="ServBehave">
<!--Endpoint for REST-->
<endpoint address="XMLService" binding="webHttpBinding" behaviorConfiguration="restPoxBehavior" contract="MyRest.IService1" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServBehave">
<!-- 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 for the REST endpoint for Help enability-->
<behavior name="restPoxBehavior">
<webHttp helpEnabled="true" />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
客戶端代碼:
public string ClientStuff()
{
var ServiceUrl = "http://localhost/MyRest/Service1.svc/XMLService/";
var empserializer = new DataContractSerializer(typeof(MyRest.Employee));
var url = new Uri(ServiceUrl + "PostEmp");
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/XML";
var emp = new MyRest.Employee { DeptName = "sale", EmpName = "ted", EmpNo = 11112 };
using (var requeststream = request.GetRequestStream())
{
empserializer.WriteObject(requeststream, emp);
}
var response = (HttpWebResponse)request.GetResponse();// crashes here with error in title
var statuscode = response.StatusCode;
return statuscode.ToString();
}
service1.svc.cs
public bool PostEmp(Employee employee)
{
//something
return true;
}
的ServiceContract
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/PostEmp", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
bool PostEmp(Employee employee);
// TODO: Add your service operations here
}
爲我做的事情是啓用跟蹤,結果服務postemp被擊中,但在postemp方法內崩潰,所以現在都好!謝謝 – user603007 2012-07-26 21:52:06