sochix / TLSharp

Telegram client library implemented in C#
1k stars 380 forks source link

ConnectAsync() not working in ASP.MVC #792

Open 3webmaster opened 6 years ago

3webmaster commented 6 years ago

TelegramClient cannot establish connection in ASP.MVC. I use the standard code:

var apiId = <...>;
var apiHash = <...>;
var client = new TelegramClient(apiId, apiHash);
await client.ConnectAsync();

This code works in console applications, but not it ASP.MVC. Here is the error message:

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 149.154.175.100:443 Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.Sockets.SocketException: 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 149.154.175.100:443

SocketException (0x274c): 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 149.154.175.100:443] System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) +244 System.Net.Sockets.Socket.Connect(EndPoint remoteEP) +150 System.Net.Sockets.TcpClient.Connect(IPEndPoint remoteEP) +83 System.Net.Sockets.TcpClient.Connect(IPAddress address, Int32 port) +142 TLSharp.Core.Network.TcpTransport..ctor(String address, Int32 port, TcpClientConnectionHandler handler) +222 TLSharp.Core.TelegramClient..ctor(Int32 apiId, String apiHash, ISessionStore store, String sessionUserId, TcpClientConnectionHandler handler) +601 CommonClasses.<Register>d__0.MoveNext() in D:\Projects\Telegram\CommonClasses\Methods.cs:25 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +31 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +60 System.Runtime.CompilerServices.TaskAwaiter.GetResult() +28 Web.Controllers.MainController.Home() in D:\Projects\Telegram\Web\Controllers\MainController.cs:15 lambda_method(Closure , ControllerBase , Object[] ) +87 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) +229 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 parameters) +35 System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +39 System.Web.Mvc.Async.WrappedAsyncResult2.CallEndDelegate(IAsyncResult asyncResult) +70 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +42 System.Web.Mvc.Async.AsyncInvocationWithFilters.b3d() +72 System.Web.Mvc.Async.<>cDisplayClass46.b3f() +387 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +42 System.Web.Mvc.Async.<>cDisplayClass2b.b1c() +38 System.Web.Mvc.Async.<>cDisplayClass21.b1e(IAsyncResult asyncResult) +188 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +38 System.Web.Mvc.Controller.b1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +29 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +68 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +52 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +39 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +38 System.Web.Mvc.MvcHandler.b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +43 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +68 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +38 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +602 System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +195 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +128

benjaminhpang commented 6 years ago

Same here, I post my exception here https://github.com/sochix/TLSharp/issues/709

It throws that exception from time to time.

benjaminhpang commented 6 years ago

I found the issue to my problem (might not apply to you). I am using Azure function to run my program and sometimes they would save the state on file storage, and rerun later on.

This timeout error will happen when TelegramClient is deserialized and run again.