locked
Api resonse inconsistency RRS feed

  • Question

  • User1204792550 posted

    I have created web api for my nativescript app. But the api response is inconsistent. Some time json response is perfect and sometime empty json. As I am newbie I need help.
    this is my conroller.

    ---------------------------------------------------->

    [RoutePrefix("Sample")]
    public class SampleController : ApiController
    {
    private readonly IConnectionRepository _sampleRepository;

    public SampleController(ConnectionRepository sampleRepository)
    {
    _sampleRepository = sampleRepository;
    }

    public SampleController()
    {
    _sampleRepository = new ConnectionRepository();
    }

    [HttpGet]
    [Route("GetMembers")]
    public IHttpActionResult GetClients()
    {
    var con = ConnectionRepository.GetInstance();
    var members= con.GetMembers();
    if (clients == null)
    {
    return NotFound();
    }
    return Ok(members);
    }

    }

    here is method---------------------------------------------->

    public class ConnectionRepository : IConnectionRepository
    {
    private SqlConnection _sqlConnection;
    private SqlDataReader _sqlDataReader;

    static Configuration rootWebConfig = WebConfigurationManager.OpenWebConfiguration("/abc");
    const string DefaultConnection = "xyz";
    static ConnectionStringSettings conString = rootWebConfig.ConnectionStrings.ConnectionStrings[DefaultConnection];

    public ConnectionRepository()
    { try
    {
    _sqlConnection = new SqlConnection(conString.ConnectionString);
    }
    catch (SqlException ex)
    {
    Console.WriteLine(ex.Message);
    }
    catch (Exception e)
    {
    Console.WriteLine(e.Message);
    }

    }

    //Singleton Pattern
    static private ConnectionRepository _connectionRepository;
    public static ConnectionRepository GetInstance()
    {
    if (_connectionRepository == null)
    _connectionRepository = new ConnectionRepository();

    return _connectionRepository;
    }

    /*

    */
    public List<abcModels> GetMembers()
    {
    SqlCommand com = new SqlCommand();
    List<abcModels> clients = new List<abcModels>();

    try
    {
    _sqlCo2nnection.Open();
    // Create a object of SqlCommand class
    com.Connection = _sqlConnection; //Pass the connection object to Command
    com.CommandType = CommandType.StoredProcedure; // We will use stored procedure.
    com.CommandText = "sp";
     //Stored Procedure Name

    _sqlDataReader = com.ExecuteReader();

    while (_sqlDataReader.Read())
    {
    Console.WriteLine("length:");
    var data = new abcModels();
    data.ID = _sqlDataReader.GetInt32(0); //The 0 stands for "the 0'th column", so the first column of the result.
    data.Description = _sqlDataReader.GetString(1);
    clients.Add(data);
    Console.WriteLine(data);
    }

    }
    catch (Exception ex)
    {
    Console.Write(ex.Message);
    }
    finally
    {
    _sqlConnection.Close();
    }
    return clients;
    }

    and here is model---------------------------------------------------->

    [DataContract]
    public class abcModels
    {
    [DataMember]
    [Required]
    public int ID { get; set; }

    [DataMember]
    [Required]
    public string Description { get; set; }
    }

    Tuesday, November 7, 2017 5:09 PM

All replies

  • User753101303 posted

    Are you sure you don"t have an exception? Writing to the console is perhaps not the best solutuon as it is really easy to make an exception unnoticed...

    Don't you have an http status code to narrow down what happens?

    Tuesday, November 7, 2017 5:15 PM
  • User1204792550 posted

    I don't know but if there's is an exception then it shouldn't return me result single time but sometime it work perfectly fine.  

    Tuesday, November 7, 2017 5:42 PM
  • User475983607 posted

    You need to rethink the design.   DB connections and repos should be scoped to the request not global to the application.   The design as is will leak data to all users of the application. 

    I agree with PatriceSc, fix the exception handlers.  You're hiding errors.

    Tuesday, November 7, 2017 6:50 PM
  • User1204792550 posted

    Thank you for your response. can you please provide me any example of what you are trying to say, because i am stuck at this point since 3 days.

    Tuesday, November 7, 2017 7:02 PM
  • User1204792550 posted

    I can see that whenever my json response is empty in controller variable count is 0. so is there any way i can correct my problem at that point.

    Tuesday, November 7, 2017 7:06 PM
  • User1204792550 posted

    i am getting 204 status code whenever my json is empty.

    Tuesday, November 7, 2017 7:07 PM
  • User1120430333 posted

    i am getting 204 status code whenever my json is empty.

    What you should do is find a way to test the solution using a test harness where you can single-step into the WebAPI and follow the code, like Resharper could for sure host the WebAPI.

    Tuesday, November 7, 2017 7:42 PM
  • User753101303 posted

    204 means  no content at all is returned. So my guess is that you end up in returning a  null object. In your code I find :

    var members= con.GetMembers();
    if (clients == null)
    

    to be weird as you get members and then you test for clients (which seems privately defined and should not be available from this point). Could it be that GetMembers() can return null and then you might not see this and end up in really returning null ?

    Maybe it should be if(members==null) instead ? And see first if this 204 no content response goes away.

    Not directly related but IMO  :
    - avoid to just write an exception to the console (which is meaningless for a deployed application). For now you are sending exceptions where no one will ever know about them which will make your app much harder to debug
    - 404 not found is best suited when you try to retrieve something using an id that doesn't exists. When a list doesn't return anything I would return an empty list rather than 404 not found (technically speaking it's not you can't provide the response, the response is just empty and it is not an error).

    Tuesday, November 7, 2017 10:36 PM
  • User1204792550 posted

    it was my mistake.
    there is members in if condition.
    and still the length or Count of members is 0

    Thursday, November 9, 2017 8:52 PM
  • User1404573039 posted

    Hi radh2,

    It seems your issue has been resolved, I would suggest you mark the helpful reply as answer to close this thread.

    Regards,

    Tony

    Wednesday, November 15, 2017 8:50 AM