2012-09-17 43 views
2

我試圖使用restsharprestsharp不成功後

這是我服務

[ServiceContract] 
    public interface IService 
    { 
     [OperationContract] 
     [WebInvoke(Method="POST", UriTemplate = "/PEmploy", ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml)] 
     Employee PostGetEmploy(Employee emp); 
    } 

    [DataContract] 
    public class Employee 
    { 
     [DataMember] 
     public int EmpNo { get; set; } 
     [DataMember] 
     public string EmpName { get; set; } 
     [DataMember] 
     public string DeptName { get; set; } 
    } 

消耗休息服務(WCF),這是我怎麼稱呼它

var client = new RestClient("http://localhost:14437/Service.svc"); 
    var request = new RestRequest("XmlService/PEmploy", Method.POST); 
    myRef.Employee emp = new myRef.Employee() { EmpNo = 101, EmpName = "Mahesh", DeptName = "CTD" }; 
    request.AddParameter("Employee", emp); 
    RestResponse<myRef.Employee> response = (RestResponse<myRef.Employee>)client.Execute<myRef.Employee>(request); 

和這是我得到的例外

Exception:Caught: "Data at the root level is invalid. Line 1, position 1." (System.Xml.XmlException) 
    A System.Xml.XmlException was caught: "Data at the root level is invalid. Line 1, position 1." 

我試過序列化,但仍然得到相同的異常。 我在做什麼錯?

回答

3

這奏效了

var client = new RestClient("http://localhost:14437/Service.svc"); 
var request = new RestRequest("XmlService/PEmploy", Method.POST); 
request.RequestFormat = DataFormat.Json; 
myRef.Employee emp = new myRef.Employee() { EmpNo = 101, EmpName = "Mahesh", DeptName = "CTD" }; 
request.AddParameter("Employee", emp); 
RestResponse<myRef.Employee> response = (RestResponse<myRef.Employee>)client.Execute<myRef.Employee>(request); 
+0

對於未來的用戶:他補充說request.RequestFormat = DataFormat.Json; –