Click here to Skip to main content
15,905,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

I am calling this function via url from my desktop application

[HttpPost]
        [AcceptVerbs("POST")]
        async public Task<HttpResponseMessage> SyncItem(long TenantId)
        {
            try
            {
                CashVitae_TM.Tenant oTenant = new CashVitae_TM.Tenant();
                oTenant = oTenant.getTenantByTenantId(TenantId);

                string dbUN = oTenant.DBUN;
                string dbPass = oTenant.DBPass;

                long SavedItemId = 0;
                Item oitem = new Item();
                
                byte[] data = await Request.Content.ReadAsByteArrayAsync();
                string JsonInventory = System.Text.Encoding.Default.GetString(data);
                List<InventoryItem> lstInventoryItem = JsonConvert.DeserializeObject<List<InventoryItem>>(JsonInventory);
                foreach (var Inventory in lstInventoryItem)
                {
                    Item oitem1 = new Item();
                    string ItemCode = Inventory.ItemCode;
                    Item item = oitem.getItembyCode(TenantId, dbUN, dbPass, ItemCode);
                    if (item != null)
                    {
                        oitem1.Id = item.Id;
                        oitem1.Code = item.Code;
                        oitem1.CreatedDtTm = item.CreatedDtTm;
                    }
                    else
                    {
                        oitem1.Id = 0;
                        oitem1.Code = Inventory.ItemCode;
                        oitem1.CreatedDtTm = DateTime.Now;
                    }
                    oitem1.TenantId = TenantId;
                    oitem1.Name = Inventory.Name;
                    oitem1.Category = Convert.ToString(Inventory.Category);
                    oitem1.CostMethod = Convert.ToString(Inventory.MethodName);
                    oitem1.Location = Convert.ToString(Inventory.Location);
                    oitem1.UnitPrice = Inventory.UnitPrice;
                    oitem1.Quantity = Inventory.QtyAvailable;
                    oitem1.QtyOnHand = Inventory.QtyOnHand;
                    oitem1.IsActive = Inventory.IsActive;
                    oitem1.ModifiedDtTm = DateTime.Now;
                    SavedItemId = oitem1.Save(TenantId, dbUN, dbPass);
                }   
                return new HttpResponseMessage(HttpStatusCode.OK);
            }
            catch (Exception ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message);
            }
        }


and this my desktop application code
try
                        {
                            string str = Inventory();
                            using (WebClient httpclient = new WebClient())
                            {
                                //string str = "abc";
                                string url = "http://localhost:51411/api/MobileAPI/SyncItem?TenantId=" + CommonClass.tenantId;

                                ASCIIEncoding encoding = new ASCIIEncoding();
                                byte[] data = encoding.GetBytes(str);

                                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                                request.Method = "Post";
                                request.ContentLength = data.Length;
                                request.ContentType = "application/json";

                                using (Stream stream = request.GetRequestStream())
                                {
                                    stream.Write(data, 0, data.Length);
                                }
                                request.Timeout = Timeout.Infinite;
                                request.KeepAlive = true;
                                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                                response.StatusCode.ToString();
                            }
                        }
                        catch(Exception ex)
                        {
                            throw ex;
                        }


but i am getting operation has timeout exception in my desktop

What I have tried:

I am calling a method in my API to save data in web DB
I tried
request.Timeout = Timeout.Infinite;
                                request.KeepAlive = true;


then also same error i am getting, how i can handle timeout exception.
one solution I know that split data but if anyone can give else solution without split data, only once i want to send whole data.
Posted
Updated 21-Jun-17 20:34pm
v2
Comments
Richard Deeming 15-Jun-17 13:36pm    
Start by working out which operation has timed out. It's possible that the timeout error is on the server, not the client.
Member 10192073 15-Jun-17 13:44pm    
I wil check, but I have splited and now I am sending data with 100 per call....

One thing I want to tell that
Even in my desktop application I was getting timeout exception but my web API was working that time too.
Means when I got timeout exception in my desktop application that time in my web DB records inserted 156 rows but after just few seconds I again run select query in web DB I got 258 records again after few seconds I run select query and got 468 rows means even timeout exception was coming in desktop but operation was going in my web code

1 solution

Very basic Exception handling can be one of the solutions of your problem.

try
{
   using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
   {
      WebHeaderCollection headers = response.Headers;    
      using (Stream answer = response.GetResponseStream())
      {
          // Do stuff
      }
   }
}
catch (WebException e)
{
   if (e.Status == WebExceptionStatus.Timeout)
   {
      // Handle timeout exception
   }
   else throw;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900