TelegramBotClient HttpRequestException on IIS - c#

I have created Telegram bot via webhook and ngrok. It works when launching inside VS. But when I have published to local IIS, request to site ends with "500 HTTP ERROR" and system events viewer shows that exception occurred when calling IHost.Run() method.
Description: The process was terminated due to an unhandled exception.
Exception info:
System.Net.Http.HttpRequestException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. ---> System.Net.Sockets.SocketException (10060): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
...
at Telegram.Bot.TelegramBotClient.MakeRequestAsync[TResponse](IRequest`1 request, CancellationToken cancellationToken)
...
at TemperatureBot.Program.Main(String[] args) in C:\Users\administrator\source\repos\TemperatureBot\TemperatureBot\Program.cs:line 14
On Run() method one of hosted services performs this:
botClient.SetWebhookAsync(hook);
Internet is accessed through corporative proxy.
Why it works well in VS debug but blunders on IIS?

Changed default publishing directory (/bin/Release/netcoreapp3.1/publish) to custom, and everything works

Related

Can't connect to SQLLocalDB sometimes when PC is offline

We are using SQLLocalDB to connect to a local database. No network access is required - C# Client application is on the same machine as SQLLocalDB. Sometimes I get the following error (only seems to happen on some PCs):
Microsoft.SqlServer.Management.Common.ConnectionFailureException: Failed to connect to server (localdb)\myserverinstancename. ---> System.Data.SqlClient.SqlException: Connection Timeout Expired. The timeout period elapsed while attempting to consume the pre-login handshake acknowledgement. This could be because the pre-login handshake failed or the server was unable to respond back in time. The duration spent while attempting to connect to this server was - [Pre-Login] initialization=27888; handshake=0; ---> System.ComponentModel.Win32Exception: The wait operation timed out
Oddly, it seems to happen if the PC is offline, and if connected to any sort of internet connection and retried, the issue goes away. However, in most cases the connection works fine even when there is no connection.
The closest I've found to my problem is this: Connection to SQL Server Works Sometimes
But that is discussing full SQL Server, rather than SQLLocalDB, and the solution uses "Sql Server Configuration Manager", which doens't work with SQLLocalDB.
My questions:
Does SQLLocalDb use IPv6/TCP for some reason, even when on the same PC?
Is there a way to change these settings for SQLLocalDB?
Or is there a different cause for this issue?
Edit: With help from some of the comments below I realised that the line that fails is when it calls a database creation script. The initial attempt at connection seems to work, but when I run my creation script it throws a "Failed to connect to server".
/// This line works
var server = new Server("(localdb)\myserverinstancename");
/// this line works 99% of the time, but sometimes "failed to connect to server"
server.ConnectionContext.ExecuteNonQuery(databaseCreationScript);

C# HttpClient Multiple Get Requests at the same time giving System.Net.Sockets.SocketException (10060) Exception

As Suggested by #John Wu's Answer on Stackoverflow. I am using the following code to make concurrent HTTP Get Requests on the same host for sending SMS
public async Task SendBulk(List<string> recipentURLs)
{
using (var client = new HttpClient())
{
//Start requests for all of them
var requests = recipentURLs.Select(url => client.GetAsync(url)).ToList();
//Wait for all the requests to finish
await Task.WhenAll(requests);
}
}
Now I am having the exception saying
System.Net.Http.HttpRequestException: An error occurred while sending the request.
---> System.IO.IOException: Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond..
---> System.Net.Sockets.SocketException (10060): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
The API I am sending requests to doesn't require a response (as I am only using it to send sms).
I am using this in a backgroud job by using HangFire. So I am okay if it takes a long time.
What I am currently looking is an efficient way to send multiple HTTP Get Requests at the Same host.
How can I safely handle all these concurrent requests and retry if any of it fails?
Also I am currently researching that due HTTP Throttling I cannot use 2 connection to the same host?
Closed HttpClient instances leave sockets open in the TIME_WAIT state for a short period of time. If a code path that creates and disposes of HttpClient objects is frequently used, the app may exhaust available sockets. You should use HttpClientFactory to avoid that.
See the post Make HTTP requests using IHttpClientFactory in ASP.NET Core.

exception with a null response was thrown sending an HTTP request to the remote WebDriver server

During Automation testing with selenium (C# Chrome WebDriver), I keep on getting the following error:
OpenQA.Selenium.WebDriverException : A exception with a null response was thrown sending an HTTP request to the remote WebDriver server for URL http://localhost:62856/session/1414381932731debf24d49572cafd7b6/url. The status of the exception was ConnectFailure, and the message was: Unable to connect to the remote server ----> System.Net.WebException : Unable to connect to the remote server ----> System.Net.Sockets.SocketException : No connection could be made because the target machine actively refused it [::1]:62856
I tried the following:
1. Adding the following arguments to ChromeDriver Options:
options.AddArgument("no-sandbox");
options.AddArgument("proxy-server='direct://'");
options.AddArgument("proxy-bypass-list=*");
Close all Chrome and ChromeDriver Processes prior to each run
Removing Anti-Virus from my computer
After all this, I am still left with this error.
Thanks,

C# Sql Connection can not connect to server

I'm trying to connect SQL server in my virtual machine with following connection string: "Server=qqvm;Database=a_test;Integrated Security=False;User Id=sa;Password=<pass>;Connect Timeout=0"
Firstly I had 'Network connection' exception with inner 'Access Denied' exception with empty stack trace. I did some digging and enabled 'Named Pipes' in SQL Configuration Manager.
After that I got this exception:
Connection Timeout Expired. The timeout period elapsed while attempting to consume the pre-login handshake acknowledgement. This could be because the pre-login handshake failed or the server was unable to respond back in time. The duration spent while attempting to connect to this server was - [Pre-Login] initialization=11281; handshake=1;
However, I'm still able to connect via SQL Management Studio to that server remotely. Any ideas?
Finally in my case specifying IP address instead of qqvm name solved the problem. Still don't know why, though. It worked a week ago.

Intermittent System.ServiceModel.CommunicationException

I have a console app that calls a WCF service to authorize scheduled credit card payments for a given store ID. Last night it called this service over 100 times and one of those calls, it got this error:
System.ServiceModel.CommunicationException: An error occurred while receiving the HTTP response to [service url] This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details. ---> System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive. ---> 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
What brought this to our attention was that this store had it's first 100 transactions authorized but 43 were left not authorized (not attempted). The service method simply grabs all 143 and does a foreach loop on them, authorizing them 1 at a time. The method returns a boolean result wrapped in a custom object that can also return error info if necessary, but that object should be small and does not change size in respect to the amount of transactions processed. I mention that because I read one case where this exception occurred in responses over a certain size. But it my case, it seems that something happened before it even tried to return. The service stopped processing in the middle of looping at which point the client received this exception. Any ideas?

Categories

Resources