简体   繁体   中英

Calling MS WCF REST Service from MVC Client POST BAD Request c#

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

Eureka!!!!

Thanks Eugene. After reading your response and checking out the link, I did some further investigation using POSTMAN and I discovered that PostAsJsonAsync is indeed doing the json conversion of the data being passed but as the StartDate and EndDate properties of the object are DateTime objects it seems that the json conversion does not like it.

I have changed the model to pass date strings instead and it is now working as expected.

Ric

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM