HttpRequestException in API call, depending on user - c#

I've been scratching my head about this one for a bit, and I was hoping someone better at parsing error messages might be able to point me in the right direction.
I have a .NET console application which utilizes the Dropbox API. The affected code snippet is printed below. When I run the executable from my machine (in a folder, co-located with the Dropbox DLL) it connects fine and performs some lookup as intended. When another user on the same network runs it on their machine (similar settings), the HttpRequestException printed below is logged after 20 seconds (consistently). Additionally, if I wrap this as a service for testing and run it on a third machine, it runs as expected under my account user/pass, but logs the same error when run under another user/pass.
Can anyone with better eyes at parsing error messages help steer me toward the solution? Or does anyone have ideas as to what I might be missing? I appreciate any help, even shots in the dark.
var _AccessToken = [value]
//DropboxCertHelper.InitializeCertPinning();
var httpClient = new HttpClient(new WebRequestHandler { ReadWriteTimeout = 10000 })
{
Timeout = TimeSpan.FromMinutes(20)
};
try
{
var config = new DropboxClientConfig("MyApplication")
{
HttpClient = httpClient
};
var client = new DropboxClient(_AccessToken, config);
ListFolderResult listing = new ListFolderResult();
listing = await client.Files.ListFolderAsync(_AppPath, true);
foreach (var item in listing.Entries.Where(i => i.IsFolder))
{
Log(item.PathDisplay);
}
}
catch (Exception e)
{
Log(e);
}
And this is the logged error:
System.Net.Http.HttpRequestException: An error occurred while sending the request.
---> System.Net.WebException: Unable to connect to the remote server
---> 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 162.125.7.7:443
at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
--- 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 ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
at Dropbox.Api.DropboxRequestHandler.<RequestJsonString>d__2f.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Dropbox.Api.DropboxRequestHandler.<RequestJsonStringWithRetry>d__1a.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Dropbox.Api.DropboxRequestHandler.<Dropbox.Api.Stone.ITransport.SendRpcRequestAsync>d__5`3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at MyApplication.Program.<MyMethod>d__6.MoveNext()

Related

Java.Net.ProtocolException: 'Unexpected status line: <html>' on consecutive api call through httpclient

I Have a view which lists reservations associated with currently logged in user. Once user clicks on one of those reservations, he navigates to details view which connects to api through httpclient and gets reservation details. The code is listed below.
client.BaseAddress = new Uri(this.URL);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.TokenStr);
var result = await client.GetAsync($"/api/reservation/getforid?Id={id}");
client.Dispose();
if (result.StatusCode == System.Net.HttpStatusCode.OK)
{
var str = result.Content.ReadAsStringAsync().Result.Replace("\\", " ");
var model = JsonConvert.DeserializeObject<ReservationModel>(str);
return model;
}
The details page also allows for modyfing certain data of selected reservation and upon successful editing, the view is closed and the reservation data is to be reloaded again.
What happens though is Java.Net.ProtocolException is thrown on client.getAsync(). And while requests to api is made, it seems that the httpclient doesnt await for the data, but gets some false response which is unable to be translated to response object.
To sum up:
The HttpClient connects to api without any problem when the first request to show reservation detail page is made.
After modyfing reservation, and trying to reload reservation data, the exception is thrown, even though the code/data making the request is same
EDIT:
I refactored my project so that it uses shared HttpClient instance and found the line of code which is causing the problem.
private IMvxCommand submit;
public IMvxCommand Submit
{
get
{
submit = submit ?? new MvxAsyncCommand(async () =>
{
var validationResult = validateFields();
if (validationResult)
{
await _personApi.UpdatePerson(Model);
_nav.Close(this,true);
}
});
return submit;
}
}
the _personApi.UpdatePerson() method seems to be somehow messing stuff up.
After commenting it the view closes, and ReservationDetailsView which lists persons successfully fetches data. However when i uncomment that method, upon navigating to ReservationDetailViewModel the same exception gets thrown...
you can see the UpdatePerson method below:
public async Task UpdatePerson(PersonModel model)
{
var token = GetCurrentToken();
//client = new HttpClient();
// client.BaseAddress = new Uri(this.URL);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.TokenStr);
// client.DefaultRequestHeaders.Add("content-type","application/json");
var dynamic = new { model.Id, model.Name, model.NumberPhone, model.Pesel, model.Street, model.Email, model.WhoToInfrom, model.PostalCode, model.CityPerson };
var content = new StringContent(JsonConvert.SerializeObject(dynamic), Encoding.UTF8, "application/json");
var result = await client.PostAsync($"/api/person/update", content);
}
EDIT 2:
Still stuck on the problem, doesn't matter how many times i'll use the HttpClient it works fine until i make HttpClient call from PersonApi in EditPersonViewModel which updates person.
Any subsequent call will actually connect to my API (i have breakpoints set in, no exceptions are thrown) but the client receives response as below. Where is it coming from?
This the error which IIS logs:
Event code: 3005
Event message: An unhandled exception has occurred.
Event time: 30.07.2021 11:14:01
Event time (UTC): 30.07.2021 09:14:01
Event ID: 17452691323540639122452734bf1713
Event sequence: 19
Event occurrence: 15
Event detail code: 0
Application information:
Application domain: /LM/W3SVC/62/ROOT-4-132720999030559530
Trust level: Full
Application Virtual Path: /
Application Path: D:\www\abc\AbcHoliday\AbcHoliday\
Machine name: SERVER3
Process information:
Process ID: 11392
Process name: w3wp.exe
Account name: IIS APPPOOL\abc.syntio.pl
Exception information:
Exception type: TaskCanceledException
Exception message: A task was canceled.
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__15.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Web.Http.HttpServer.<SendAsync>d__24.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Web.Http.Owin.HttpMessageHandlerAdapter.<InvokeCore>d__20.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Owin.Security.Infrastructure.AuthenticationMiddleware`1.<Invoke>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContextStage.<RunApp>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContext.<DoFinalWork>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.StageAsyncResult.End(IAsyncResult ar)
at Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContext.EndFinalWork(IAsyncResult ar)
at System.Web.HttpApplication.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step)
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
Request information:
Request URL: http://abc.syntio.pl/api/reservation/getforid?Id=6384
Request path: /api/reservation/getforid
User host address: 10.10.0.1
User:
Is authenticated: True
Authentication Type: JWT
Thread account name: IIS APPPOOL\abc.syntio.pl
Thread information:
Thread ID: 25
Thread account name: IIS APPPOOL\abc.syntio.pl
Is impersonating: False
Stack trace: at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__15.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Web.Http.HttpServer.<SendAsync>d__24.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Web.Http.Owin.HttpMessageHandlerAdapter.<InvokeCore>d__20.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Owin.Security.Infrastructure.AuthenticationMiddleware`1.<Invoke>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContextStage.<RunApp>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContext.<DoFinalWork>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.StageAsyncResult.End(IAsyncResult ar)
at Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContext.EndFinalWork(IAsyncResult ar)
at System.Web.HttpApplication.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step)
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
You're going to want a try/catch around Web API calls in the VM. You can't expect this call to succeed every single time! You can fall back on a cached response on error.
Based on the error description, it sounds like you are not able to parse the JSON correctly. You may need to provide custom JsonSerializerSettings compatible with the settings your API server is using. Newtonsoft should be throwing a JsonSerializationException with more information if the problem is due to JSON serialization/deserialization errors.
As for the Java Exceptions, Xamarin.Android by default uses AndroidClientHandler these days. There appears to be some bugs because you can catch Java exceptions (exceptions thrown from the Java.Lang.* namespace) in your .NET Standard/PCL project. That's a bad situation because the .NET Standard/PCL project doesn't know about Android.
I recommend using the following approach in a modern Xamarin.Android app using MvvmCross:
Use a single instance of HttpClient across the application. Normally the best way to do that would be to use IHttpClientFactory, but Xamarin doesn't have great support for that (yet). A good place to instantiate the HttpClient is inside your MvxAndroidSetup.
public class Setup : MvxAndroidSetup<App>
{
protected override void InitializeFirstChance()
{
base.InitializeFirstChance();
var httpClient = new HttpClient(new BasicAuthenticationDelegatingHandler(new SafeAndroidClientHandler()));
// TODO: Setup httpClient.DefaultRequestHeaders, etc.
Mvx.IoCProvider.RegisterSingleton(httpClient);
}
}
Use a SafeAndroidClientHandler to catch the Java.Lang.* exceptions being thrown by HttpClient and rethrow then wrapped in a .NET-friendly Exception. Here is my implementation. You would do something similar for the GetAsync method override.
public class SafeAndroidClientHandler : AndroidClientHandler
{
public SafeAndroidClientHandler()
{
AutomaticDecompression = System.Net.DecompressionMethods.GZip | System.Net.DecompressionMethods.Deflate;
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
try
{
return await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
}
catch (Java.Lang.Exception ex)
{
throw new HttpRequestException(ex.Message, ex);
}
}
}
Now you can simply inject HttpClient into ViewModels, Services, etc. thanks to MvvmCross IoC. If a Java.Lang.* exception is thrown it would now be caught as a HttpRequestException in the ViewModel.
HTTPClient Class says:
// HttpClient is intended to be instantiated once per application, rather than per-use. See Remarks.
static readonly HttpClient client = new HttpClient();
Given that:
Don't call client.Dispose;
Don't create an HTTPClient on each call - create it once.

System.Net.Http.HttpRequestException for Send grid send email

We are facing following exception in calling Sendgrid Send Email function in some case.
We get this exception for 20-30 emails out of 50,000 in a day.
System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: Unknown error (0xffffffff)
at System.Net.Sockets.Socket.BeginConnectEx(EndPoint remoteEP, Boolean flowContext, AsyncCallback callback, Object state)
at System.Net.Sockets.Socket.UnsafeBeginConnect(EndPoint remoteEP, AsyncCallback callback, Object state)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception) --- 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 ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at SendGrid.Helpers.Reliability.RetryDelegatingHandler.<SendAsync>d__4.MoveNext() --- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at SendGrid.BaseClient.<MakeRequest>d__20.MoveNext() --- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at SendGrid.BaseClient.<RequestAsync>d__21.MoveNext() --- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at SendGrid.BaseClient.<SendEmailAsync>d__22.MoveNext() --- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
My code to send email is
var client = new SendGridClient("API Key");
var response = await client.SendEmailAsync(message);
Can anyone please help me how can I add more verification to avoid such situation.
The Retry logic did work for me so far.
catch (Exception ex)
{
if (--retryMax == 0)
{
throw;
}
else
{
await Task.Delay(1000);
}
return null;
}

.NET Core call WCF: The server name or address could not be resolved error

I've a .NET Core 2 web api that call a connected WCF service. In my development environment it works fine, but when I deploy it in production environment, with IIS and Kestrel, I get this exception:
System.AggregateException: One or more errors occurred. (There was no endpoint listening at http://192.168.100.33:3433/Test.svc that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.) ---> System.ServiceModel.EndpointNotFoundException: There was no endpoint listening at http://192.168.100.33:3433/Test.svc that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details. ---> System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.Http.WinHttpException: The server name or address could not be resolved
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Threading.Tasks.RendezvousAwaitable`1.GetResult()
at System.Net.Http.WinHttpHandler.<StartRequest>d__105.MoveNext()
--- End of inner exception stack trace ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.ServiceModel.Channels.ServiceModelHttpMessageHandler.<SendAsync>d__41.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
at System.Net.Http.HttpClient.<FinishSendAsyncUnbuffered>d__59.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.ServiceModel.Channels.HttpChannelFactory`1.HttpClientRequestChannel.HttpClientChannelAsyncRequest.<SendRequestAsync>d__13.MoveNext()
--- End of inner exception stack trace ---
at System.Runtime.AsyncResult.End[TAsyncResult](IAsyncResult result)
at System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.End(SendAsyncResult result)
at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)
at System.ServiceModel.Channels.ServiceChannelProxy.TaskCreator.<>c__DisplayClass1_0.<CreateGenericTask>b__0(IAsyncResult asyncResult)
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at WebApplication1.Controllers.ValuesController.Get() in c:\temp\TestWS\WebApplication1\Controllers\ValuesController.cs:line 29
---> (Inner Exception #0) System.ServiceModel.EndpointNotFoundException: There was no endpoint listening at http://192.168.100.33:3433/Test.svc that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details. ---> System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.Http.WinHttpException: The server name or address could not be resolved
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Threading.Tasks.RendezvousAwaitable`1.GetResult()
at System.Net.Http.WinHttpHandler.<StartRequest>d__105.MoveNext()
--- End of inner exception stack trace ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.ServiceModel.Channels.ServiceModelHttpMessageHandler.<SendAsync>d__41.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
at System.Net.Http.HttpClient.<FinishSendAsyncUnbuffered>d__59.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.ServiceModel.Channels.HttpChannelFactory`1.HttpClientRequestChannel.HttpClientChannelAsyncRequest.<SendRequestAsync>d__13.MoveNext()
--- End of inner exception stack trace ---
at System.Runtime.AsyncResult.End[TAsyncResult](IAsyncResult result)
at System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.End(SendAsyncResult result)
at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)
at System.ServiceModel.Channels.ServiceChannelProxy.TaskCreator.<>c__DisplayClass1_0.<CreateGenericTask>b__0(IAsyncResult asyncResult)<---
The same WCF service called by classic web api on the same server works fine.
If I call it by web browser from the server it work fine too.
This is my test code:
BasicHttpBinding bindingStr = new BasicHttpBinding();
EndpointAddress endpointSessionSTR = new EndpointAddress(new Uri("http://192.168.100.33:3433/Test.svc"));
TestStrutture.StruttureClient cliStr = new TestStrutture.StruttureClient(bindingStr, endpointSessionSTR);
var retVal = cliStr.GetClassiStrutturaAsync();
retVal.Wait();
var classi = retVal.Result;
What did I forget to do?
On CMD:
netsh winhttp reset proxy
You can try to list the proxys first using.
netsh winhttp show proxy
I have not discovered why this happens, but that did the trick for me. On netcore 2.0 and 2.1.

AWS S3 AmazonServiceException A WebException with status SecureChannelFailure

Have service that receives requests, generates data and saves this in files to AWS S3.
If service receives many requests can try to save up to 20 files (2 servers x 10 workers each) in parallel.
Data generated by requests and saved to S3 can be from few KB to around ~400MB
Problem is that sometimes (seems to be when service is busy/big files to save) S3 fails with the exception below:
We discussed 2 solutions:
1) Implement a retry of S3.UploadAsync() if save fails.
Not sure if will make any difference. Assume S3 already retries internally so maybe is no point to retry. If problem is that files are too big/takes to long to save this won't solve the issue maybe make it worst.
2) Increase the TransferUtilityConfig.DefaultTimeout to, lets say 10min (default is 5min).
If problem is that saving takes more than 5 minutes, this will fix the issue but exception thrown by S3 doesn't indicate is a timeout exception so maybe this will solve anything.
3) Is this an intermittent issue in AWS infrastructure? Could retry help?
Does anyone have experience/solution when this exception happens? Any other ideas?
UPDATE:
TransferUtilityConfig() does not contain DefaultTimeout if using NET 4.5. The functionality has been moved to AmazonS3Config. This offers more parameters to control the upload: Timeout, ReadWriteTimeout, MaxErrorRetry
AmazonS3Config Class
Settings are explained here
AWS Retries and Timeouts
This is the code used by the service to save:
using (var amazonS3Client = new AmazonS3Client(RegionEndpoint.GetBySystemName(_iAwsS3Settings.RegionEndpoint)))
using (var fileTransferUtility = new TransferUtility(amazonS3Client))
using (var memoryStream = new MemoryStream(data))
{
var fileTransferUtilityRequest = new TransferUtilityUploadRequest
{
BucketName = _iAwsS3Settings.BucketName,
InputStream = memoryStream,
StorageClass = S3StorageClass.ReducedRedundancy,
PartSize = 6291456, // 6 MB.
Key = fileLocation,
CannedACL = S3CannedACL.BucketOwnerFullControl
};
await fileTransferUtility.UploadAsync(fileTransferUtilityRequest, ct);
}
This is the exception given when S3 save fails:
System.AggregateException: One or more errors occurred. --->
Amazon.Runtime.AmazonServiceException: A WebException with status
SecureChannelFailure was thrown. ---> System.Net.WebException: The
request was aborted: Could not create SSL/TLS secure channel. at
System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult
asyncResult, TransportContext& context) at
System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult
asyncResult) at
System.Threading.Tasks.TaskFactory1.FromAsyncCoreLogic(IAsyncResult
iar, Func2 endFunction, Action1 endAction, Task1 promise, Boolean
requiresSynchronization) --- End of stack trace from previous
location where exception was thrown --- at
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
task) at
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
task) at
Amazon.Runtime.Internal.HttpHandler1.<InvokeAsync>d__91.MoveNext()
--- End of stack trace from previous location where exception was thrown --- at
Amazon.Runtime.Internal.HttpHandler1.<InvokeAsync>d__91.MoveNext()
--- End of stack trace from previous location where exception was thrown --- at
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
task) at
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
task) at
Amazon.Runtime.Internal.RedirectHandler.d__11.MoveNext()
--- End of stack trace from previous location where exception was thrown --- at
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
task) at
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
task) at
Amazon.Runtime.Internal.Unmarshaller.<InvokeAsync>d__31.MoveNext()
--- End of stack trace from previous location where exception was thrown --- at
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
task) at
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
task) at
Amazon.S3.Internal.AmazonS3ResponseHandler.d__11.MoveNext()
--- End of stack trace from previous location where exception was thrown --- at
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
task) at
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
task) at
Amazon.Runtime.Internal.ErrorHandler.<InvokeAsync>d__51.MoveNext()
--- End of inner exception stack trace --- at Amazon.Runtime.Internal.WebExceptionHandler.HandleException(IExecutionContext
executionContext, WebException exception) at
Amazon.Runtime.Internal.ErrorHandler.ProcessException(IExecutionContext
executionContext, Exception exception) at
Amazon.Runtime.Internal.ErrorHandler.d__51.MoveNext()
--- End of stack trace from previous location where exception was thrown --- at
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
task) at
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
task) at
Amazon.Runtime.Internal.CallbackHandler.<InvokeAsync>d__91.MoveNext()
--- End of stack trace from previous location where exception was thrown --- at
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
task) at
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
task) at
Amazon.Runtime.Internal.CredentialsRetriever.d__71.MoveNext()
--- End of stack trace from previous location where exception was thrown --- at
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
task) at
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
task) at
Amazon.Runtime.Internal.RetryHandler.<InvokeAsync>d__101.MoveNext()
--- End of stack trace from previous location where exception was thrown --- at
Amazon.Runtime.Internal.RetryHandler.d__10`1.MoveNext()
This is kind of an old question but I just had a very similar issue and because I was unable to find an appropriate answer on Google, I wanted to contribute my solution.
I had started implementation of an older API which had the following line:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
I was able to correct the issue by changing SecurityProtocolType.Ssl3 to SecurityProtocolType.Tls12
Further explanation:
SSL3 is not generally supported due to the Poodle vulnerability:
https://security.stackexchange.com/questions/70719/ssl3-poodle-vulnerability/70724#70724
I had not added this line of code myself and as a result, I was having great difficulty finding the source of the issue. However, while researching I had come across this post regarding a similar issue: https://github.com/aws/aws-sdk-net/issues/86
Near the bottom of this discussion, someone suggested adding the following line:
ServicePointManager.CheckCertificateRevocationList = true;
This fix was not able to correct my issue but it did lead me down the correct path. After searching for other references to the ServicePointManager, I was able to find the previously mentioned line regarding the SecurityProtocolType and correct it.
S3 SDK already implements retry logic
By default, an upload is retried 4 times
Created a console application to try to reproduce the error. Console application tried to upload 10-30 files asynchronously. Changing the values in AmazonS3Config for Timeout, ReadWriteTimeout, MaxErrorRetry produced exceptions (System.Net.WebException: The operation has timed out) but not the same we had (Could not create SSL/TLS secure channel).
We hypothesized that the problem could that the service is so busy that can not create the connection, that is why get "Could not create SSL/TLS secure channel"
My exception was:
A WebException with status ConnectFailure was thrown
For me the ServiceUrl was http instead of https and also didn't need a port number. The port number was blocked by the firewall.
Before:
http://s3ws.MyDomain.com:80
After:
https://s3ws.MyDomain.com
var s3Config = new AmazonS3Config
{
RegionEndpoint = Amazon.RegionEndpoint.EUNorth1,
ServiceURL = "https://s3ws.MyDomain.com",
ForcePathStyle = true
};
return new AmazonS3Client(accessKey, secretKey, s3Config);

SignalR Websocket Exception when closing client

When starting and stopping a SignalR client that is connected to a basic self hosted server like this:
async public void Start(string url)
{
_connection = new HubConnection(url);
_proxy = _connection.CreateHubProxy("hubname");
await _connection.Start().ContinueWith((task) => IsRunning = true);
}
public void Stop()
{
_connection.Stop();
}
I get the following exception when calling the "Stop" method (including trace messages):
SignalR.Transports.TransportHeartBeat Verbose: 0 : KeepAlive(c75fe282-a86d-406a-935b-5981b47bd472)
SignalR.Transports.TransportHeartBeat Information: 0 : Connection 1bdabc19-e0a7-4649-aabb-bade071ad6d0 is New.
SignalR.Transports.WebSocketTransport Information: 0 : Abort(1bdabc19-e0a7-4649-aabb-bade071ad6d0)
SignalR.Transports.TransportHeartBeat Information: 0 : Removing connection 1bdabc19-e0a7-4649-aabb-bade071ad6d0
SignalR.Transports.WebSocketTransport Information: 0 : End(1bdabc19-e0a7-4649-aabb-bade071ad6d0)
SignalR.Transports.WebSocketTransport Verbose: 0 : DrainWrites(1bdabc19-e0a7-4649-aabb-bade071ad6d0)
SignalR.Transports.WebSocketTransport Information: 0 : CloseSocket(1bdabc19-e0a7-4649-aabb-bade071ad6d0)
App.vshost.exe Error: 0 : Error while closing the websocket: System.Net.WebSockets.WebSocketException (0x80004005): An internal WebSocket error occurred. Please see the innerException, if present, for more details. ---> System.Net.Sockets.SocketException (0x80004005): An existing connection was forcibly closed by the remote host
at System.Net.WebSockets.WebSocketConnectionStream.WebSocketConnection.WriteAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
at System.Net.WebSockets.WebSocketConnectionStream.<WriteAsync>d__11.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.WebSockets.WebSocketBase.<SendFrameAsync>d__11.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.WebSockets.WebSocketBase.WebSocketOperation.<Process>d__47.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.WebSockets.WebSocketBase.<CloseOutputAsyncCore>d__17.MoveNext()
at System.Net.WebSockets.WebSocketBase.ThrowIfConvertibleException(String methodName, Exception exception, CancellationToken cancellationToken, Boolean aborted)
at System.Net.WebSockets.WebSocketBase.<CloseOutputAsyncCore>d__17.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNet.SignalR.WebSockets.WebSocketHandler.<<CloseAsync>b__6>d__8.MoveNext()
App.vshost.exe Error: 0 : Error while closing the websocket: System.Net.WebSockets.WebSocketException (0x80004005): An internal WebSocket error occurred. Please see the innerException, if present, for more details. ---> System.Net.Sockets.SocketException (0x80004005): An existing connection was forcibly closed by the remote host
at System.Net.WebSockets.WebSocketConnectionStream.WebSocketConnection.WriteAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
at System.Net.WebSockets.WebSocketConnectionStream.<WriteAsync>d__11.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.WebSockets.WebSocketBase.<SendFrameAsync>d__11.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.WebSockets.WebSocketBase.WebSocketOperation.<Process>d__47.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.WebSockets.WebSocketBase.<CloseOutputAsyncCore>d__17.MoveNext()
at System.Net.WebSockets.WebSocketBase.ThrowIfConvertibleException(String methodName, Exception exception, CancellationToken cancellationToken, Boolean aborted)
at System.Net.WebSockets.WebSocketBase.<CloseOutputAsyncCore>d__17.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNet.SignalR.WebSockets.WebSocketHandler.<<CloseAsync>b__6>d__8.MoveNext()
SignalR.Transports.WebSocketTransport Information: 0 : CompleteRequest (1bdabc19-e0a7-4649-aabb-bade071ad6d0)
SignalR.Transports.TransportHeartBeat Verbose: 0 : KeepAlive(c75fe282-a86d-406a-935b-5981b47bd472)
SignalR.Transports.TransportHeartBeat Verbose: 0 : KeepAlive(c75fe282-a86d-406a-935b-5981b47bd472)
The setups itself seem to work fine and devices can connect, send, receive, and disconnect. But with every disconnect I get the ugly above exception. Any idea what the problem could be? I am using SignalR 2.2.0 and Owin 1.2.2.
Looks like a known issue which might be fixed in v3.
Try providing a TimeSpan to the close method to give it time to actually close while waiting for any async processes which may be in progress:
public void Stop()
{
_connection.Stop(new TimeSpan(1000));
}
This worked for me.
Ok guys, I know its lame. I am trying to shutdown signalr client when the mainwindow closes and I get a lot of exceptions that I can not catch and my process hangs. So i decided to kill the main process on form close.
private void OnClose(object sender, CancelEventArgs e){
try
{
var process = Process.GetCurrentProcess();
process.Kill();
//HubConnection.Stop();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
I solved this problem with
connection.Reconnecting += connection.Stop;
Because I found the connection state is always reconnecting
If I understand correctly, you want to stop broadcasting message when client is disconnected. So my approach is use Connection Lifetime Events in SignalR.
This is the official page : SignalRLifeTimeEvents
public override System.Threading.Tasks.Task OnDisconnected()
{
//-- When any client will close the browser or page this event will fire
return base.OnDisconnected();
}

Categories

Resources