I am hoping that someone can help me. I have a WCF REST service which is working correctly for ALL GET functionality. I am not able however to get the POST methods to work. My Service contract is
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "NewEmployeeLeave/")]
string addTimeOff(mmpTimeOff data);
which is calling the following
// addTimeOff
// Add a new time off record with passed information
public string addTimeOff(mmpTimeOff data)
{
try
{
// Add the new record to the employees leave requests
mmpTimeOff newTimeOff = data;
db.TimeOffs.Add(new TimeOff
{
StartDate = newTimeOff.StartDate,
EndDate = newTimeOff.EndDate,
PersonId = newTimeOff.PersonId,
Manager = newTimeOff.Manager,
TimeOffType = newTimeOff.TimeOffType,
Status = newTimeOff.Status,
Notes = newTimeOff.Notes
});
db.SaveChanges();
}
// Make sure that we can push the error back to the client.
catch (Exception e)
{
return e.Message;
}
return "ok";
}
The client code is as follows.
client = new HttpClient();
HttpResponseMessage response=new HttpResponseMessage();
mmpPerson user = new mmpPerson();
client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
try
{
mmpTimeOff newLeave = new mmpTimeOff()
{
PersonId = user.Id,
StartDate = DateTime.Parse("2017-10-22"),
EndDate = DateTime.Parse("2017-10-28"),
TimeOffType = "Annual",
Manager = user.Manager,
Status = "Appr"
};
response = await client.PostAsJsonAsync(url + "/newemployeeleave", newLeave);
if (response.IsSuccessStatusCode)
{
ViewBag.Added = "Added New Leave Successfully";
}
When the client is run i get a null value being sent through to the addTimeOff method.
The class is defined in the service as
public class mmpTimeOff
{
[DataMember]
public int Id { get; set; }
[DataMember]
public System.DateTime StartDate { get; set; }
[DataMember]
public System.DateTime EndDate { get; set; }
[DataMember]
public string TimeOffType { get; set; }
[DataMember]
public int PersonId { get; set; }
[DataMember]
public string Status { get; set; }
[DataMember]
public Nullable<int> Manager { get; set; }
[DataMember]
public string Notes { get; set; }
}
and is bound to the client as a connected service.
How do i successfully get the object data through to the service.
All help gratefully received.
Ric