2012-07-26 142 views
0

我在實現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 
} 

回答

1

有一對夫婦的事情,你應該可以解決。第一個當然是使用正確的內容類型頭的,有沒有這樣的事情application/XML

request.ContentType = "text/xml"; 

的,另外,更重要的是,確保你引用的完全相同Employee類的服務器和客戶端,否則客戶端數據協定序列化程序將在XML中發出不同的命名空間,導致服務器崩潰。基本上這個Employee類應該在服務器和客戶端應用程序之間的共享類庫中聲明。

順便說更容易能夠通過自己在未來的調試這樣的問題,而不是在你的服務端這裏簡單enable tracing張貼問題:

<system.diagnostics> 
    <sources> 
     <source name="System.ServiceModel" 
       switchValue="Information, ActivityTracing" 
       propagateActivity="true"> 
      <listeners> 
       <add name="traceListener" 
        type="System.Diagnostics.XmlWriterTraceListener" 
        initializeData= "c:\log\Traces.svclog" /> 
      </listeners> 
     </source> 
    </sources> 
</system.diagnostics> 

然後用內置於.NET SDK跟蹤查看器(SvcTraceViewer.exe)簡單地加載生成的日誌文件,所有內容都將以GUI(看起來像從90年代開始,但完成這項工作)向您展示並向您解釋。

啊順便說一句,你可能需要從你的web.config刪除以下行來禁用ASP.NET兼容性:

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

我不是100%肯定,但IIRC與REST啓用此是必要的服務(儘管在這一點上可能是錯誤的)。

+0

爲我做的事情是啓用跟蹤,結果服務postemp被擊中,但在postemp方法內崩潰,所以現在都好!謝謝 – user603007 2012-07-26 21:52:06