Elasticsearch.Net 7 & Nest 7 - Invalid bulk update request - c#

I'm trying to create a .Net Core (netcoreapp3.1) application to send my data from Azure Eventhub to ES 7.
I'm using the following packages:
ElasticSearch.Net 7.8.1
Nest 7.8.1
The data that I retrieve from Eventhub are 2 types that inherit from the IElasticBaseEntity.
I want to bulk update a list of these objects, which can be the object with all the information or the object to update one field of an already indexed person.
The field to match / search in ES is the id field.
To simplify my example I will use these dummy classes:
public interface IElasticBaseEntity
{
string Id { get; set; }
DateTime ServerTimestamp { get; set; }
}
The person class is the one with all the information
public abstract class Person: IElasticBaseEntity
{
public string Id { get; set; }
public string Firstname {get; set;}
public string Name {get; set;}
public decimal? Score { get; set; }
}
The Score class is the update that I want to do on the indexed Person, based on the Id
public abstract class Score : IElasticBaseEntity
{
public string Id {get; set;}
public decimal? Score { get; set; }
}
I use this method to build the connection to ES
public static IElasticClient CreateInstance(ElasticsearchConfig config)
{
IConnectionPool pool;
var connection = new HttpConnection();
pool = new SniffingConnectionPool(new[] { new Uri(config.Url) }) { SniffedOnStartup = true };
var connectionSettings = new ConnectionSettings(pool, connection);
connectionSettings.BasicAuthentication(config.Username, config.Password);
connectionSettings.ServerCertificateValidationCallback(ServerCertificateValidationCallback);
connectionSettings.RequestTimeout(TimeSpan.FromMinutes(2)).EnableHttpCompression();
var client = new ElasticClient(connectionSettings);
return client;
}
So I started with the bulk command on ElasticClient.
In the past I was able to add the objects using the descriptor.Index but of course, I want an update an not an insert/create of everything.
So I've come up with this, but for some reason I keep on receiving an error in Visual Studio 2019 on "Invalid /_bulk request" without any other information.
IEnumerable<IElasticBaseEntity> list = RetrievedData();
var descriptor = new BulkDescriptor();
foreach (var eachDoc in list)
{
var doc = eachDoc;
descriptor.Update<IElasticBaseEntity>(i => i
.Id(doc.Id)
.Doc(doc)
.DocAsUpsert(true));
}
var response = await _client.BulkAsync(descriptor);
// Try to debug & see what was send
if (response.ApiCall.RequestBodyInBytes != null)
{
var jsonOutput = System.Text.Encoding.UTF8.GetString(response.ApiCall.RequestBodyInBytes);
}
The error that I receive is the following (retrieved from the response.DebugInformation):
Invalid NEST response built from a unsuccessful () low level call on POST: /persons-20200717/_bulk
Invalid Bulk items:
Audit trail of this API call:
[1] PingFailure: Node: https://myconnectiontoES:9243/ Exception: PipelineException Took: 00:00:01.2859155
[2] SniffOnFail: Took: 00:00:02.1577638
[3] SniffFailure: Node: https://myconnectiontoES:9243/ Exception: PipelineException Took: 00:00:02.0840985
OriginalException: Elasticsearch.Net.ElasticsearchClientException: Failed sniffing cluster state.. >Call: unknown resource
---> Elasticsearch.Net.PipelineException: Failed sniffing cluster state.
---> Elasticsearch.Net.PipelineException: An error occurred trying to read the response from the >specified node.
at Elasticsearch.Net.RequestPipeline.SniffAsync(CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at Elasticsearch.Net.RequestPipeline.SniffAsync(CancellationToken cancellationToken)
at Elasticsearch.Net.RequestPipeline.SniffOnConnectionFailureAsync(CancellationToken >cancellationToken)
at Elasticsearch.Net.Transport1.PingAsync(IRequestPipeline pipeline, Node node, CancellationToken >cancellationToken) at Elasticsearch.Net.Transport1.RequestAsync[TResponse](HttpMethod method, String path, >CancellationToken cancellationToken, PostData data, IRequestParameters requestParameters)
--- End of inner exception stack trace ---
Audit exception in step 1 PingFailure:
Elasticsearch.Net.PipelineException: An error occurred trying to read the response from the specified >node.
at Elasticsearch.Net.RequestPipeline.PingAsync(Node node, CancellationToken cancellationToken)
Audit exception in step 3 SniffFailure:
Elasticsearch.Net.PipelineException: An error occurred trying to read the response from the specified >node.
at Elasticsearch.Net.RequestPipeline.SniffAsync(CancellationToken cancellationToken)
Request:
<Request stream not captured or already read to completion by serializer. Set DisableDirectStreaming() >on ConnectionSettings to force it to be set on the response.>
Response:
So I looks like there is something wrong with my /_bulk request
What I've tried:
Catch the /_bulk request with fiddler > the request is invalid & not send
Try to set connectionSettings.DisableDirectStreaming(true); to print out the request > this is always null
So if anyone could point out the mistake I made in building the the /_bulk request or could point me in a direction to debug this & retrieve more information, I would be thankfull.
Currently I'm going around in circles, re-reading the documentation, google-ing, but without any result.
Thanks

The failure is occurring when attempting to read the response to sniffing the cluster. It looks like sniffing has occurred because pinging the cluster has failed though, so it's worth checking that you can make a HEAD request to the base address of the Elasticsearch cluster using the credentials the client is configured to use.
Based on the port 9243 being used in the connection to Elasticsearch, I suspect the Elasticsearch cluster is running in Elastic Cloud. SniffingConnectionPool must not be used with an Elasticsearch cluster running in Elastic Cloud, as the addresses returned in the sniff response for where to reach Elasticsearch nodes are internal addresses which will be unreachable from the client. Instead, the CloudConnectionPool should be used, which has some convenience methods to use when creating an instance of ElasticClient
var client = new ElasticClient(
"<cloud id retrieved from the Elastic Cloud web console>",
new BasicAuthenticationCredentials(config.Username, config.Password));
CloudConnectionPool will use http compression by default, but if you need more control over other configuration like request timeout, you can use
var pool = new CloudConnectionPool(
"<cloud id retrieved from the Elastic Cloud web console>",
new BasicAuthenticationCredentials(config.Username, config.Password));
var settings = new ConnectionSettings(pool)
.RequestTimeout(TimeSpan.FromMinutes(2));
var client = new ElasticClient(settings);

After going around in circles, I started creating test to check the basic ES command.
Based on the example I found for ElasticSearch.Net, I was unable to succesfully run any of the examples.
So I checked the ES Index by trying it with a curl command.
curl -H "Content-Type: application/json" -u username:password -XPOST "https://elasticsearch_url:port/indexname/stats/1" -d "{\"application\": \"test\"}"
This command failed, with the message that I'm not allowed to contact ES.
So we've checked the ES user and appearently there was an error in the role that was assigned to this user (in ElasticSearch).
Once we fixed this, I was able to run my code & execute the partial update.
IEnumerable<IElasticBaseEntity> list = RetrievedData();
var descriptor = new BulkDescriptor();
foreach (var eachDoc in list)
{
var doc = eachDoc;
descriptor.Update<IElasticBaseEntity>(i => i
.Id(doc.Id)
.Doc(doc)
.DocAsUpsert(true));
}
var response = await _client.BulkAsync(descriptor);
The issue I have now, is that this runs for about 1 hour and then I get a SnifFailure.
The connection to ES is retried but mostly ends in a "MaxTimeOutReached"
The RequestTimeOut is set to 2 minutes
The Retries & timeout is not set, so the defaults are used (-> this is retrying until the RequestTimeout of 2 minutes is reached)
As you can see in the log below: I'm able to send docs to ES and then at some point I receive this error. Mostly after running my application for 1h.
2020-08-05 11:22:01.5553| INFO| 001 ES_indexName processed documents 08/05/2020 11:22:01 1 in 202.3803 ms
2020-08-05 11:29:28.9633| INFO| 001 ES_indexName processed documents 08/05/2020 11:29:28 1 in 179.7982 ms
2020-08-05 11:40:08.4666| INFO| 001 ES_indexName processed documents 08/05/2020 11:40:07 1 in 291.5695 ms
2020-08-05 11:47:26.2924|ERROR| failed Invalid NEST response built from a unsuccessful () low level call on POST: /ES_indexName/_bulk
# Invalid Bulk items:
# Audit trail of this API call:
- [1] PingFailure: Node: https://ES_node1:port/ Exception: PipelineException Took: 00:01:40.0193513
- [2] SniffOnFail: Took: 00:05:00.0132241
- [3] SniffFailure: Node: https://ES_node2:port/ Exception: PipelineException Took: 00:01:40.0036955
- [4] SniffFailure: Node: https://ES_node3:port/ Exception: PipelineException Took: 00:01:40.0019296
- [5] SniffFailure: Node: https://ES_node1:port/ Exception: PipelineException Took: 00:01:40.0005639
- [6] MaxTimeoutReached:
# OriginalException: Elasticsearch.Net.ElasticsearchClientException: Maximum timeout reached while retrying request. Call: unknown resource
---> Elasticsearch.Net.PipelineException: Failed sniffing cluster state.
---> System.AggregateException: One or more errors occurred. (An error occurred trying to write the request data to the specified node.) (An error occurred trying to write the request data to the specified node.) (An error occurred trying to write the request data to the specified node.)
---> Elasticsearch.Net.PipelineException: An error occurred trying to write the request data to the specified node.
---> System.Threading.Tasks.TaskCanceledException: The operation was canceled.
at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean allowHttp2, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.GetHttpConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.DecompressionHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
at Elasticsearch.Net.HttpConnection.RequestAsync[TResponse](RequestData requestData, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at Elasticsearch.Net.RequestPipeline.SniffAsync(CancellationToken cancellationToken)
--- End of inner exception stack trace ---
---> (Inner Exception #1) Elasticsearch.Net.PipelineException: An error occurred trying to write the request data to the specified node.
---> System.Threading.Tasks.TaskCanceledException: The operation was canceled.
at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean allowHttp2, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.GetHttpConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.DecompressionHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
at Elasticsearch.Net.HttpConnection.RequestAsync[TResponse](RequestData requestData, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at Elasticsearch.Net.RequestPipeline.SniffAsync(CancellationToken cancellationToken)<---
---> (Inner Exception #2) Elasticsearch.Net.PipelineException: An error occurred trying to write the request data to the specified node.
---> System.Threading.Tasks.TaskCanceledException: The operation was canceled.
at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean allowHttp2, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.GetHttpConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.DecompressionHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
at Elasticsearch.Net.HttpConnection.RequestAsync[TResponse](RequestData requestData, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at Elasticsearch.Net.RequestPipeline.SniffAsync(CancellationToken cancellationToken)<---
--- End of inner exception stack trace ---
at Elasticsearch.Net.RequestPipeline.SniffAsync(CancellationToken cancellationToken)
at Elasticsearch.Net.RequestPipeline.SniffOnConnectionFailureAsync(CancellationToken cancellationToken)
at Elasticsearch.Net.Transport`1.PingAsync(IRequestPipeline pipeline, Node node, CancellationToken cancellationToken)
at Elasticsearch.Net.Transport`1.RequestAsync[TResponse](HttpMethod method, String path, CancellationToken cancellationToken, PostData data, IRequestParameters requestParameters)
--- End of inner exception stack trace ---
# Audit exception in step 1 PingFailure:
Elasticsearch.Net.PipelineException: An error occurred trying to write the request data to the specified node.
---> System.Threading.Tasks.TaskCanceledException: The operation was canceled.
at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean allowHttp2, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.GetHttpConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.DecompressionHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
at Elasticsearch.Net.HttpConnection.RequestAsync[TResponse](RequestData requestData, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at Elasticsearch.Net.RequestPipeline.PingAsync(Node node, CancellationToken cancellationToken)
# Audit exception in step 3 SniffFailure:
Elasticsearch.Net.PipelineException: An error occurred trying to write the request data to the specified node.
---> System.Threading.Tasks.TaskCanceledException: The operation was canceled.
at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean allowHttp2, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.GetHttpConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.DecompressionHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
at Elasticsearch.Net.HttpConnection.RequestAsync[TResponse](RequestData requestData, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at Elasticsearch.Net.RequestPipeline.SniffAsync(CancellationToken cancellationToken)
# Audit exception in step 4 SniffFailure:
Elasticsearch.Net.PipelineException: An error occurred trying to write the request data to the specified node.
---> System.Threading.Tasks.TaskCanceledException: The operation was canceled.
at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean allowHttp2, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.GetHttpConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.DecompressionHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
at Elasticsearch.Net.HttpConnection.RequestAsync[TResponse](RequestData requestData, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at Elasticsearch.Net.RequestPipeline.SniffAsync(CancellationToken cancellationToken)
# Audit exception in step 5 SniffFailure:
Elasticsearch.Net.PipelineException: An error occurred trying to write the request data to the specified node.
---> System.Threading.Tasks.TaskCanceledException: The operation was canceled.
at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean allowHttp2, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.GetHttpConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.DecompressionHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
at Elasticsearch.Net.HttpConnection.RequestAsync[TResponse](RequestData requestData, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at Elasticsearch.Net.RequestPipeline.SniffAsync(CancellationToken cancellationToken)
# Request:
<Request stream not captured or already read to completion by serializer. Set DisableDirectStreaming() on ConnectionSettings to force it to be set on the response.>
# Response:
<Response stream not captured or already read to completion by serializer. Set DisableDirectStreaming() on ConnectionSettings to force it to be set on the response.>
400039.9975 ms MUST RETRY```

Related

Dapr Resource Bindings HTTP GET from external service

I tried to create a Dapr component like the following:
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: nominatim
namespace: default
spec:
type: bindings.http
version: v1
metadata:
- name: url
value: https://nominatim.openstreetmap.org
- name: method
value: GET
- name: UserAgent
value: Other
Then in my service I Invoked this binging like this:
var getAddressUrl = $"{UriString}/reverse?format=jsonv2&lat={latitude}&lon={longitude}&zoom=18&addressdetails=1";
var invokeBindingAsync = _daprClient.InvokeBindingAsync<object, AddressLocationDto>(
DaprComponentsSettings.HttpNominatim,
DaprComponentsSettings.CreateBindingOperation,
null,
new Dictionary<string, string>
{
["url"] = getAddressUrl
});
var address = await invokeBindingAsync;
return address;
Unfortunately, I receive the following error:
Status(StatusCode="Internal", Detail="Error starting gRPC call. HttpRequestException: Connection refused (127.0.0.1:50001) SocketException: Connection refused", DebugException="System.Net.Http.HttpRequestException: Connection refused (127.0.0.1:50001)
---> System.Net.Sockets.SocketException (111): Connection refused
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)
at System.Net.Sockets.Socket.<ConnectAsync>g__WaitForConnectWithCancellation|283_0(AwaitableSocketAsyncEventArgs saea, ValueTask connectTask, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.DefaultConnectAsync(SocketsHttpConnectionContext context, CancellationToken cancellationToken)
at System.Net.Http.ConnectHelper.ConnectAsync(Func`3 callback, DnsEndPoint endPoint, HttpRequestMessage requestMessage, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at System.Net.Http.ConnectHelper.ConnectAsync(Func`3 callback, DnsEndPoint endPoint, HttpRequestMessage requestMessage, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.GetHttp2ConnectionAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at Grpc.Net.Client.Internal.GrpcCall`2.RunCall(HttpRequestMessage request, Nullable`1 timeout)")
I do not know how to call HTTP get like this. Please help.

Using WebClient, getting The SSL connection could not be established error sporadically but very often

This part of the app seems so simple but I keep running into the error in title.
public static async Task getAndProcessReps(string DUNS, string apikey)
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls13 | SecurityProtocolType.Tls12;
string url = string.Format("https://api.sam.gov/entity-information/v2/entities?api_key={1}&ueiDUNS={0}&includeSections=repsAndCerts", DUNS, apikey);
MemoryStream resp = new MemoryStream();
string r = "";
using (WebClient wc = new WebClient())
{
bool retry = true;
int retryCt = 0;
while (retry)
{
try
{
//resp = new MemoryStream(await wc.DownloadDataTaskAsync(new Uri(url)));
r = await wc.DownloadStringTaskAsync(new Uri(url));
retry = false;
}
catch
{
retryCt++;
System.Threading.Thread.Sleep(1000);
if (retryCt > 3)
retry = false;
}
}
}
reps.HomeSAM result = r.CreateFromJsonString<reps.HomeSAM>();
I get the following error returned occasionally but often:
System.Net.Http.HttpRequestException: The SSL connection could not be establi
shed, see inner exception.
---> System.Security.Authentication.AuthenticationException: Authentication fai
led, see inner exception.
---> System.ComponentModel.Win32Exception (0x80090321): The buffers supplied to
a function was too small.
--- End of inner exception stack trace ---
at System.Net.Security.SslStream.ForceAuthenticationAsync[TIOAdapter](TIOAdap
ter adapter, Boolean receiveFirst, Byte[] reAuthenticationData, Boolean isApm)
at System.Net.Http.ConnectHelper.EstablishSslConnectionAsyncCore(Boolean asyn
c, Stream stream, SslClientAuthenticationOptions sslOptions, CancellationToken c
ancellationToken)
--- End of inner exception stack trace ---
at System.Net.Http.ConnectHelper.EstablishSslConnectionAsyncCore(Boolean asyn
c, Stream stream, SslClientAuthenticationOptions sslOptions, CancellationToken c
ancellationToken)
at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request
, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequest
Message request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.GetHttpConnectionAsync(HttpRequestMessa
ge request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage r
equest, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToke
n)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Bool
ean async, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.SendAsyncCore(HttpRequestMessage request, HttpC
ompletionOption completionOption, Boolean async, Boolean emitTelemetryStartStop,
CancellationToken cancellationToken)
at System.Net.HttpWebRequest.SendRequest(Boolean async)
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
I am using c# Core5 on Windows Server 2012 R2.
Is there something I am doing wrong here?
I have seen some chatter about Windows 7 vs Windows 10 specifically about this part of the error:
System.ComponentModel.Win32Exception (0x80090321): The buffers supplied to
a function was too small.
Is this my server's problem, or my code's problem or the endpoint?
Could my error be about them not accepting tls1.3?

A connection attempt failed because the connected party did not properly respond after a period of time - having issue only in aws server

I am trying to access a url that ends with .json file from a different server. I have used the below code which is working fine in my development env but when I hosted the in aws server I am getting error.
Sample URL : https://sample.com/sampledata.json
public dynamic GetHttpData(string urlString)
{
string jsonResponse = null;
try
{
Uri getUri = new Uri(urlString);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(getUri);
request.Headers.Add(HttpRequestHeader.Cookie, null);
request.Accept = "*/*";
using (WebResponse response = request.GetResponse())
{
using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
{
jsonResponse = streamReader.ReadToEnd();
}
}
}
catch (Exception ex)
{
}
return jsonResponse;
}
The above code works fine in dev but getting the below error in server.
An error occurred while sending the request. 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.WebExceptionSystem.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.
--- End of inner exception stack trace ---
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.GetResult(Int16 token)
at System.Net.Security.SslStream.<FillBufferAsync>g__InternalFillBufferAsync|215_0[TReadAdapter](TReadAdapter adap, ValueTask`1 task, Int32 min, Int32 initial)
at System.Net.Security.SslStream.ReadAsyncInternal[TReadAdapter](TReadAdapter adapter, Memory`1 buffer)
at System.Net.Http.HttpConnection.FillAsync()
at System.Net.Http.HttpConnection.ReadNextResponseHeaderLineAsync(Boolean foldedHeadersAllowed)
at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.AuthenticationHelper.SendWithNtAuthAsync(HttpRequestMessage request, Uri authUri, ICredentials credentials, Boolean isProxyAuth, HttpConnection connection, HttpConnectionPool connectionPool, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithNtConnectionAuthAsync(HttpConnection connection, HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.AuthenticationHelper.SendWithAuthAsync(HttpRequestMessage request, Uri authUri, ICredentials credentials, Boolean preAuthenticate, Boolean isProxyAuth, Boolean doRequestAuth, HttpConnectionPool pool, CancellationToken cancellationToken)
at System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
at System.Net.HttpWebRequest.SendRequest()
at System.Net.HttpWebRequest.GetResponse() System.Net.Requests
You need to add time out parameter like this :
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(getUri);
// Set the 'Timeout' property of the HttpWebRequest to 600000 milliseconds.
request.Timeout=600000; //10 Minutes

Azure function httpclient ObjectDisposedException Cannot access a disposed object SslStream

I'm getting this ObjectDisposedException when I did Post request from Azure function. I see this issue both real azure function environment and in function local debug as well. I believe this is happening due to large response body from the target service. But not sure though.
Below is the code and detailed error message. I get this error in the line "await httpClient.SendAsync(requestMessage).ConfigureAwait(false)"
This code is working perfectly fine and getting 200 response when try it in local script which is not in Azure func environment.
try
{
using (HttpResponseMessage response = await httpClient.SendAsync(requestMessage).ConfigureAwait(false))
{
var responseHeaders = string.Join(" | ", response.Headers.Select(h => $"{h.Key} : {h.Value}"));
sbHeaders.Append($" :: Response- {responseHeaders}");
string content = await response.Content.ReadAsStringAsync();
try
{
response.EnsureSuccessStatusCode();
}
catch (HttpRequestException ex)
{
// This try catch is to handle any unsuccessful service response (service has handled the request)
string errorMessage = $"{requestMessage.RequestUri.ToString()} failed!. Headers: {sbHeaders.ToString()} :: Server response: {content}";
throw new CustomException(serviceAlias, response.StatusCode, errorMessage, ex);
}
var responseEntity = JsonConvert.DeserializeObject<TResponse>(content);
return responseEntity;
}
}
catch (Exception ex)
{
// This try catch is to handle any network error (service HAS NOT handled the request)
string errorMessage = $"{requestMessage.RequestUri.ToString()} failed!. Headers: {sbHeaders.ToString()} :: Server response: [{ex.GetType().Name}]{ex.Message}";
throw new CustomException(serviceAlias, HttpStatusCode.InternalServerError, errorMessage, ex);
}
System.IO.IOException: The read operation failed, see inner exception. --->
System.ObjectDisposedException: Cannot access a disposed object.\r\nObject name: 'SslStream'.\r\n at System.Net.Security.SslState.ThrowIfExceptional()\r\n
at System.Net.Security.SslState.CheckThrow(Boolean authSuccessCheck, Boolean shutdownCheck)\r\n
at System.Net.Security.SslState.CheckOldKeyDecryptedData(Memory`1 buffer)\r\n
at System.Net.Security.SslState.HandleQueuedCallback(Object& queuedStateRequest)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n
at System.Net.Security.SslStreamInternal.ReadAsyncInternal[TReadAdapter](TReadAdapter adapter, Memory`1 buffer)\r\n --- End of inner exception stack trace ---\r\n
at System.Net.Security.SslStreamInternal.ReadAsyncInternal[TReadAdapter](TReadAdapter adapter, Memory`1 buffer)\r\n
at System.Net.Http.HttpConnection.FillAsync()\r\n at System.Net.Http.HttpConnection.ReadNextResponseHeaderLineAsync(Boolean foldedHeadersAllowed)\r\n
at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)\r\n --- End of inner exception stack trace ---\r\n
at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)\r\n
at System.Net.Http.HttpConnectionPool.SendWithNtConnectionAuthAsync(HttpConnection connection, HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)\r\n
at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)\r\n
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n
at System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n
at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)\r\n
at Microsoft.Ingestion.Stitch.Function.StitchServiceClient.SendRequestMessageInternalAsync[TResponse](HttpRequestMessage requestMessage, MicroServiceAlias serviceAlias) in E:\\Agent_work\\21\\s\\Src\\Stitch.Function\\Clients\\StitchServiceClient.cs:line 229"```
Please check how using statement works.
In your code client will be disposed as soon as using block ends but you still have pending tasks on content that will try to access it. You need to get results while in client using block.

"The response ended prematurely" when connecting to insecure gRPC channel

I'm trying to establish a connection to an insecure gRPC server. I'm using gRPC for communication between two processes inside of a Docker container, that's why I don't need any encryption or strong authentication.
The server behaves as expected and I can do calls using grpcurl like that:
grpcurl -plaintext localhost:42652 SomeService.DoSomething
Now I'm trying to call the same RPC method from a .Net Core application:
// Registration of the DI service
services.AddGrpcClient<DaemonService.DaemonServiceClient>(options => {
// Enable support for unencrypted HTTP2
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
options.Address = new Uri("http://localhost:42652");
// Just a test, doesn't change anything.
options.ChannelOptionsActions.Add(channelOptions => channelOptions.Credentials = ChannelCredentials.Insecure);
});
// Call
var reply = _someServiceClient.DoSomething(new Request());
But the call in the last line results in an exception:
fail: Grpc.Net.Client.Internal.GrpcCall[6]
Error starting gRPC call.
System.Net.Http.HttpRequestException: An error occurred while sending the request.
---> System.IO.IOException: The response ended prematurely.
at System.Net.Http.HttpConnection.FillAsync()
at System.Net.Http.HttpConnection.ReadNextResponseHeaderLineAsync(Boolean foldedHeadersAllowed)
at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithNtConnectionAuthAsync(HttpConnection connection, HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at Microsoft.Extensions.Http.Logging.LoggingHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at Microsoft.Extensions.Http.Logging.LoggingScopeHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
at Grpc.Net.Client.Internal.GrpcCall`2.SendAsync(HttpRequestMessage request)
fail: Grpc.Net.Client.Internal.GrpcCall[3]
Call failed with gRPC error status. Status code: 'Cancelled', Message: 'Error starting gRPC call.'.
fail: SomeNamespace.Session.Program[0]
An error occured.
System.Net.Http.HttpRequestException: An error occurred while sending the request.
---> System.IO.IOException: The response ended prematurely.
at System.Net.Http.HttpConnection.FillAsync()
at System.Net.Http.HttpConnection.ReadNextResponseHeaderLineAsync(Boolean foldedHeadersAllowed)
at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithNtConnectionAuthAsync(HttpConnection connection, HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at Microsoft.Extensions.Http.Logging.LoggingHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at Microsoft.Extensions.Http.Logging.LoggingScopeHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
at Grpc.Net.Client.Internal.GrpcCall`2.SendAsync(HttpRequestMessage request)
at Grpc.Net.Client.Internal.GrpcCall`2.GetResponseAsync()
at Grpc.Net.Client.Internal.HttpClientCallInvoker.BlockingUnaryCall[TRequest,TResponse](Method`2 method, String host, CallOptions options, TRequest request)
at Grpc.Core.Interceptors.InterceptingCallInvoker.<BlockingUnaryCall>b__3_0[TRequest,TResponse](TRequest req, ClientInterceptorContext`2 ctx)
at Grpc.Core.ClientBase.ClientBaseConfiguration.ClientBaseConfigurationInterceptor.BlockingUnaryCall[TRequest,TResponse](TRequest request, ClientInterceptorContext`2 context, BlockingUnaryCallContinuation`2 continuation)
at Grpc.Core.Interceptors.InterceptingCallInvoker.BlockingUnaryCall[TRequest,TResponse](Method`2 method, String host, CallOptions options, TRequest request)
at SomeNamespace.RpcServices.DaemonService.DaemonServiceClient.GetVncContainerEnvironment(EmptyRequest request, CallOptions options) in /src/SomeNamespace.RpcServices/obj/Release/netcoreapp3.0/Vnc-container-daemonGrpc.cs:line 98
at SomeNamespace.RpcServices.DaemonService.DaemonServiceClient.GetVncContainerEnvironment(EmptyRequest request, Metadata headers, Nullable`1 deadline, CancellationToken cancellationToken) in /src/SomeNamespace.RpcServices/obj/Release/netcoreapp3.0/Vnc-container-daemonGrpc.cs:line 94
at SomeNamespace.Rpc.RpcEventListener.StartListening() in /src/SomeNamespace/Rpc/RpcEventListener.cs:line 32
Do you have an idea what I've to do different? I cannot find much documentation about establishing insecure connections like that.
I found the fix on my own:
It works when I move the AppContext.SetSwitch above the AddGrpcClient.
// Enable support for unencrypted HTTP2
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
// Registration of the DI service
services.AddGrpcClient<DaemonService.DaemonServiceClient>(options => {
options.Address = new Uri("http://localhost:42652");
options.ChannelOptionsActions.Add(channelOptions => channelOptions.Credentials = ChannelCredentials.Insecure);
});

Categories