question

durloveroy avatar image
0 Votes"
durloveroy asked SamWu-MSFT commented

Why is it not working in the server?

It works fine in my development pc. When I host to the server, then it shows errors !

CODE:

     private static object AddStockToWebAPI()
     {

         int Status = 0;
         string strMsg = "";

         List<DataAPI> lstd = new List<DataAPI>();
         lstd = GetDataForWebAPI();

         if (lstd.Count > 0)
         {

             string Baseurl = "https://XXXXXXXXXXXXXXXXXXXXX/XXXXXXXXXXXX/stockapi/";

             using (var client = new HttpClient())
             {

                 client.BaseAddress = new Uri(Baseurl);
                 client.DefaultRequestHeaders.Clear();
                 client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                 HttpResponseMessage Res = client.PostAsJsonAsync("stockupdateapi", lstd).Result;
                 if (Res.IsSuccessStatusCode)
                 {
                     var Response = Res.Content.ReadAsStringAsync().Result;
                     JObject brandJOBject = JObject.Parse(Response);

                     Status = Convert.ToInt32(brandJOBject["status"].ToString());
                     strMsg = brandJOBject["message"].ToString();

                 }
                 else
                 {
                     //ERROR: API Unreachable 

                 }

                 return Status;
             }
         }
         else
         {
             return Status;
         }

     }


ERROR:

 HttpResponseMessage Res = client.PostAsJsonAsync("stockupdateapi", lstd).Result;



ERROR DETAIL:

System.AggregateException: One or more errors occurred. ---> System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult)
at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)
--- End of inner exception stack trace ---
at System.Net.TlsStream.EndWrite(IAsyncResult asyncResult)
at System.Net.PooledStream.EndWrite(IAsyncResult asyncResult)
at System.Net.ConnectStream.WriteHeadersCallback(IAsyncResult ar)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult, TransportContext& context)
at System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar)
--- End of inner exception stack trace ---
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
at System.Threading.Tasks.Task`1.get_Result()
at ONLINESTOCK.Program.AddStockToWebAPI() in D:\WORK\RND\CONSOLEAPP\ONLINESTOCK\ONLINESTOCK\Program.cs:line 87
---> (Inner Exception #0) System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult)
at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)
--- End of inner exception stack trace ---
at System.Net.TlsStream.EndWrite(IAsyncResult asyncResult)
at System.Net.PooledStream.EndWrite(IAsyncResult asyncResult)
at System.Net.ConnectStream.WriteHeadersCallback(IAsyncResult ar)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult, TransportContext& context)
at System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar)
--- End of inner exception stack trace ---<---






dotnet-csharpwindows-server-iisdotnet-aspnet-generaldotnet-cli
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Make sure the server and client is are using the latest TLS version 1.2. Also, make sure the service is running.

Transport Layer Security (TLS) best practices with the .NET Framework

There are several issues with the design. Line 25 is an asynchronous method coded as a synchronous (.Result). The AddStockToWebAPI method should be async and the API call should have an await.

 var Response = await Res.Content.ReadAsStringAsync();

You are not using HttpClient as recommended in the reference docs. HttpClient is intended to be instantiated once and re-used throughout the life of an application.

The design only handles 2XX responses. An API might return a 404 if a resource is not found. The client should be able to handle this situation.

If you expect AddStockToWebAPI to return an integer then return an integer not an object.




0 Votes 0 ·

@durloveroy This should be a network problem, you try to visit "https://XXXXXXXXXXXXXXXXXXXXX/XXXXXXXXXXXX/stockapi/" to see if it is successful.

0 Votes 0 ·

0 Answers