locked
Calling A Function That Processes DateTime Objects Causes 400 Bad Request Error RRS feed

  • Question

  • User1231829591 posted

    I have a method that sets the value of a property called Duration equal to the difference between two DateTime objects. Simply by calling this method my WCF REST is throwing 400 Bad Request Error. Below is my code. 

    public class Vehicle
    {
    DateTime StartRent{get; set;}
    DateTime EndRent{get;set;}
    string VehicleID {get; set;}
    double Duration {get;set;}
    double Fee{get; set;}

    public Vehicle()
    {
    StartRent = DateTime.Now;
    VehicleID = RandomCharacterGenerator(15);
    }

    public string RandomCharacterGenerator(int length)
    {
    // return a random string
    }
    }

    [DataContract] public class VehicleManagement {
    [DataMember]
    public List<Vehicle> VehicleList = new VehicleList<Vehicle>(); public double GetFee(string id) { return VehicleList.Find(v = v.VehicleID == id).Fee; } public void SetDuration (string id) { DateTime startRent = VehicleList.Find(v => v.VehicleID == id).StartRent; DateTime endRent = VehicleList.Find(v =>v.VehicleID==id).EndRent = DateTime.Now; VehicleList.Find(v => v.VehicleID == id).Duration = (endRent - startRent).TotalHours; } public void SetFee(string id) {
    double duration = VehicleList.Find(v => v.VehicleID == id).Duration;
    VehicleList.Find(v = v.VehicleID==id).Fee = 25 * duration + .08(25*duration); }

    public void UserRentsAVehicle()
    {
    Vehicle v = new Vehicle();
    string id = v.VehicleID;
    v.StartRent = DateTime.Now;
    VehicleList.Add(v);
    } }

    //My Service
    [ServiceContract]
    public interface IRentalService
    {
     [WebInvoke(UriTemplate = "Process/{id}", Method = "POST", ResponseFormat = WebMessageFormat.Xml)]
     [OperationContract]
     void ProcessPayment(string id);
    
     [WebInvoke(UriTemplate = "Fee/{duration}", Method = "POST", ResponseFormat = WebMessageFormat.Xml)]
     [OperationContract]
     string GetRentalFee(string duration);
    }
    
    public class RentalService():IRentalService
    {
       VehicleManagement vm = new VehicleManagement();
    
       public void ProcessParkingPayment(string vehicleId)
       {    
         vm.SetFee(vehicleId);
       }
    
       public string GetRentalFee(string id)
       {
          return vm.GetFee(id);
       }
    }
    

    Wednesday, June 1, 2016 1:50 AM

Answers

  • User1559292362 posted

    Hi ManyTitles,

    I have a method that sets the value of a property called Duration equal to the difference between two DateTime objects. Simply by calling this method my WCF REST is throwing 400 Bad Request Error. Below is my code. 

    I create a demo by using your code, it seems that there some error on it, and modify it as below for your reference.

    #Models

     public class Vehicle
        {
            public DateTime StartRent { get; set; }
            public DateTime EndRent { get; set; }
            public string VehicleID { get; set; }
            public double Duration { get; set; }
            public double Fee { get; set; }
    
            public Vehicle()
            {
                StartRent = DateTime.Now;
                VehicleID = RandomCharacterGenerator(15);
            }
    
            public string RandomCharacterGenerator(int length)
            {
                // return a random string
    
                return "test";
            }
        }
    
        [DataContract]
        public class VehicleManagement
        {
            [DataMember]
            public List<Vehicle> VehicleList = new List<Vehicle>();
    
            public VehicleManagement()
            {
                VehicleList.Add(new Vehicle() {StartRent = DateTime.Now, EndRent = DateTime.Now, Duration = 34.56, Fee = 12.45, VehicleID = "1" });
            }
    
            public double GetFee(string id)
            {
                return VehicleList.Find(v => v.VehicleID == id).Fee;
            }
    
            public void SetDuration(string id)
            {
                DateTime startRent = VehicleList.Find(v => v.VehicleID == id).StartRent;
                DateTime endRent = VehicleList.Find(v => v.VehicleID == id).EndRent = DateTime.Now;
                VehicleList.Find(v => v.VehicleID == id).Duration = (endRent - startRent).TotalHours;
            }
    
            public void SetFee(string id)
            {
                double duration = VehicleList.Find(v => v.VehicleID == id).Duration;
                VehicleList.Find(v => v.VehicleID == id).Fee = 25 * duration + 0.08 * (25 * duration);
            }
    
            public void UserRentsAVehicle()
            {
                Vehicle v = new Vehicle();
                string id = v.VehicleID;
                v.StartRent = DateTime.Now;
                VehicleList.Add(v);
            }
        }

    #Service

    Interface 
    
    [WebInvoke(UriTemplate = "Process/{id}", Method = "POST", ResponseFormat = WebMessageFormat.Xml)]
            [OperationContract]
            void ProcessPayment(string id);
    
            [WebInvoke(UriTemplate = "Fee/{duration}", Method = "POST", ResponseFormat = WebMessageFormat.Xml)]
            [OperationContract]
            double GetRentalFee(string duration);
    
    Service:
    
    public void ProcessPayment(string vehicleId)
            {
                vm.SetFee(vehicleId);
            }
    
            public double GetRentalFee(string id)
            {
                return vm.GetFee(id);
            }

    ClientCode

     try
                {
                    string requestUrl = "http://localhost:6334/Service1.svc/Fee/1";
                    HttpWebRequest req = WebRequest.Create(requestUrl) as HttpWebRequest;
                    req.KeepAlive = false;
                    req.Method = "POST";
                    string sb = "";
                    byte[] buffer = Encoding.ASCII.GetBytes(sb);
                    req.ContentLength = buffer.Length;
                    req.ContentType = "text/xml";
                    Stream PostData = req.GetRequestStream();
                    PostData.Write(buffer, 0, buffer.Length);
                    PostData.Close();
                    HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
                    Encoding enc = System.Text.Encoding.GetEncoding(1252);
                    StreamReader loResponseStream =
                    new StreamReader(resp.GetResponseStream(), enc);
                    string Response = loResponseStream.ReadToEnd();
                    loResponseStream.Close();
                    resp.Close();
                    Console.WriteLine(Response);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }

    Best regards,

    Cole Wu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 2, 2016 8:42 AM

All replies

  • User475983607 posted

    There are a few logical errors in the code.  I suggest that you Unit test and/or debug the code.  Please see your other post for suggestion on how to unit test.

    http://forums.asp.net/t/2096189.aspx?WCF+Cannot+Access+LocalHost

    Edit: you updated the main thread and created more logical errors.  Please take a few moments step through the code.

    Wednesday, June 1, 2016 11:01 AM
  • User1559292362 posted

    Hi ManyTitles,

    I have a method that sets the value of a property called Duration equal to the difference between two DateTime objects. Simply by calling this method my WCF REST is throwing 400 Bad Request Error. Below is my code. 

    I create a demo by using your code, it seems that there some error on it, and modify it as below for your reference.

    #Models

     public class Vehicle
        {
            public DateTime StartRent { get; set; }
            public DateTime EndRent { get; set; }
            public string VehicleID { get; set; }
            public double Duration { get; set; }
            public double Fee { get; set; }
    
            public Vehicle()
            {
                StartRent = DateTime.Now;
                VehicleID = RandomCharacterGenerator(15);
            }
    
            public string RandomCharacterGenerator(int length)
            {
                // return a random string
    
                return "test";
            }
        }
    
        [DataContract]
        public class VehicleManagement
        {
            [DataMember]
            public List<Vehicle> VehicleList = new List<Vehicle>();
    
            public VehicleManagement()
            {
                VehicleList.Add(new Vehicle() {StartRent = DateTime.Now, EndRent = DateTime.Now, Duration = 34.56, Fee = 12.45, VehicleID = "1" });
            }
    
            public double GetFee(string id)
            {
                return VehicleList.Find(v => v.VehicleID == id).Fee;
            }
    
            public void SetDuration(string id)
            {
                DateTime startRent = VehicleList.Find(v => v.VehicleID == id).StartRent;
                DateTime endRent = VehicleList.Find(v => v.VehicleID == id).EndRent = DateTime.Now;
                VehicleList.Find(v => v.VehicleID == id).Duration = (endRent - startRent).TotalHours;
            }
    
            public void SetFee(string id)
            {
                double duration = VehicleList.Find(v => v.VehicleID == id).Duration;
                VehicleList.Find(v => v.VehicleID == id).Fee = 25 * duration + 0.08 * (25 * duration);
            }
    
            public void UserRentsAVehicle()
            {
                Vehicle v = new Vehicle();
                string id = v.VehicleID;
                v.StartRent = DateTime.Now;
                VehicleList.Add(v);
            }
        }

    #Service

    Interface 
    
    [WebInvoke(UriTemplate = "Process/{id}", Method = "POST", ResponseFormat = WebMessageFormat.Xml)]
            [OperationContract]
            void ProcessPayment(string id);
    
            [WebInvoke(UriTemplate = "Fee/{duration}", Method = "POST", ResponseFormat = WebMessageFormat.Xml)]
            [OperationContract]
            double GetRentalFee(string duration);
    
    Service:
    
    public void ProcessPayment(string vehicleId)
            {
                vm.SetFee(vehicleId);
            }
    
            public double GetRentalFee(string id)
            {
                return vm.GetFee(id);
            }

    ClientCode

     try
                {
                    string requestUrl = "http://localhost:6334/Service1.svc/Fee/1";
                    HttpWebRequest req = WebRequest.Create(requestUrl) as HttpWebRequest;
                    req.KeepAlive = false;
                    req.Method = "POST";
                    string sb = "";
                    byte[] buffer = Encoding.ASCII.GetBytes(sb);
                    req.ContentLength = buffer.Length;
                    req.ContentType = "text/xml";
                    Stream PostData = req.GetRequestStream();
                    PostData.Write(buffer, 0, buffer.Length);
                    PostData.Close();
                    HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
                    Encoding enc = System.Text.Encoding.GetEncoding(1252);
                    StreamReader loResponseStream =
                    new StreamReader(resp.GetResponseStream(), enc);
                    string Response = loResponseStream.ReadToEnd();
                    loResponseStream.Close();
                    resp.Close();
                    Console.WriteLine(Response);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }

    Best regards,

    Cole Wu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 2, 2016 8:42 AM