C# 不成功的职位
我正在尝试使用restsharp使用rest服务(wcf) 这是我的服务C# 不成功的职位,c#,rest,restsharp,C#,Rest,Restsharp,我正在尝试使用restsharp使用rest服务(wcf) 这是我的服务 [ServiceContract] public interface IService { [OperationContract] [WebInvoke(Method="POST", UriTemplate = "/PEmploy", ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFo
[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; }
}
这就是我所说的
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);
我尝试了序列化,但仍然得到相同的异常。
我做错了什么?我补充道
request.RequestFormat = DataFormat.Json
这就成功了
完整代码
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);
var client=new RestClient(“http://localhost:14437/Service.svc");
var请求=新的重新请求(“XmlService/PEmploy”,Method.POST);
request.RequestFormat=DataFormat.Json;
myRef.Employee emp=new myRef.Employee(){EmpNo=101,EmpName=“Mahesh”,DeptName=“CTD”};
request.AddParameter(“员工”,emp);
resresponse response=(resresponse)client.Execute(request);
对于未来的访问者:他添加了request.RequestFormat=DataFormat.Json;
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);