Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception thrown when application name contains certain characters #3040

Closed
anstein-msft opened this issue Feb 18, 2022 · 5 comments · Fixed by #3455
Closed

Exception thrown when application name contains certain characters #3040

anstein-msft opened this issue Feb 18, 2022 · 5 comments · Fixed by #3455

Comments

@anstein-msft
Copy link

Cosmos .net package 3.24.0

When using the following code

CosmosClient cClient = new CosmosClient(CosmosEndpointUri, CosmosPrimaryKey, new CosmosClientOptions() { ApplicationName = MethodBase.GetCurrentMethod().DeclaringType.Name });

And the method "MethodBase.GetCurrentMethod().Name" is returning the following string:

<Run>d__9

There is a generic exception thrown...

image

System.FormatException: The format of value '6.0.0-rc.2.21480.5|N|d__9' is invalid.
at System.Net.Http.Headers.HttpHeaderParser.ParseValue(String value, Object storeValue, Int32& index)
at System.Net.Http.Headers.HttpHeaders.ParseAndAddValue(HeaderDescriptor descriptor, HeaderStoreItemInfo info, String value)
at System.Net.Http.Headers.HttpHeaders.Add(HeaderDescriptor descriptor, String value)
at System.Net.Http.Headers.HttpHeaders.Add(String name, String value)
at Microsoft.Azure.Documents.HttpClientExtension.AddUserAgentHeader(HttpClient httpClient, UserAgentContainer userAgent)
at Microsoft.Azure.Cosmos.CosmosHttpClientCore.CreateHelper(HttpClient httpClient, HttpMessageHandler httpMessageHandler, TimeSpan requestTimeout, UserAgentContainer userAgentContainer, ApiType apiType, ICommunicationEventSource eventSource)
at Microsoft.Azure.Cosmos.CosmosHttpClientCore.CreateWithConnectionPolicy(ApiType apiType, ICommunicationEventSource eventSource, ConnectionPolicy connectionPolicy, HttpMessageHandler httpMessageHandler, EventHandler1 sendingRequestEventArgs, EventHandler1 receivedResponseEventArgs)
at Microsoft.Azure.Cosmos.DocumentClient.Initialize(Uri serviceEndpoint, ConnectionPolicy connectionPolicy, Nullable1 desiredConsistencyLevel, HttpMessageHandler handler, ISessionContainer sessionContainer, Nullable1 enableCpuMonitor, IStoreClientFactory storeClientFactory, TokenCredential tokenCredential)
at Microsoft.Azure.Cosmos.DocumentClient..ctor(Uri serviceEndpoint, AuthorizationTokenProvider cosmosAuthorization, EventHandler1 sendingRequestEventArgs, ConnectionPolicy connectionPolicy, Nullable1 desiredConsistencyLevel, JsonSerializerSettings serializerSettings, ApiType apitype, EventHandler1 receivedResponseEventArgs, HttpMessageHandler handler, ISessionContainer sessionContainer, Nullable1 enableCpuMonitor, Func`2 transportClientHandlerFactory, IStoreClientFactory storeClientFactory)
at Microsoft.Azure.Cosmos.ClientContextCore.Create(CosmosClient cosmosClient, CosmosClientOptions clientOptions)
at Microsoft.Azure.Cosmos.CosmosClient..ctor(String accountEndpoint, AuthorizationTokenProvider authorizationTokenProvider, CosmosClientOptions clientOptions)
at Microsoft.Azure.Cosmos.CosmosClient..ctor(String accountEndpoint, String authKeyOrResourceToken, CosmosClientOptions clientOptions)
at D365SDKAccess.ParallelBatchExecuteMultipleEntityInsert.Run(HttpRequestMessage req, ILogger log) in C:\src\DOD Projects\ATEC\Azure Functions\D365SDKAccess\ParallelBatchExecuteMultipleEntityInsert.cs:line 77

I had to scratch my head for too long to figure out what the issue was. Ideally, a CosmosException would be thrown instead that stated that the application name was invalid (or the code handling the HTTP requests would handle this edge case).

@ealsur
Copy link
Member

ealsur commented Feb 22, 2022

The ApplicationName value is used on the Headers. The validation that you are seeing is not being done by the SDK:

System.FormatException: The format of value '6.0.0-rc.2.21480.5|N|d__9' is invalid.
at System.Net.Http.Headers.HttpHeaderParser.ParseValue(String value, Object storeValue, Int32& index)
at System.Net.Http.Headers.HttpHeaders.ParseAndAddValue(HeaderDescriptor descriptor, HeaderStoreItemInfo info, String value)
at System.Net.Http.Headers.HttpHeaders.Add(HeaderDescriptor descriptor, String value)
at System.Net.Http.Headers.HttpHeaders.Add(String name, String value)

It means the value you are passing is invalid as an HTTP header and it's being validated by the framework (System.Net.Http).

@anstein-msft
Copy link
Author

anstein-msft commented Feb 22, 2022 via email

@ealsur
Copy link
Member

ealsur commented Feb 22, 2022

We cannot do the validation before because we (SDK library) does not own the validation. Furthermore, this validation and limitation is done on HTTP requests by the System.Net namespace (HttpClient). This is not a limitation on the TCP requests which is the area we own on the SDK.

@anstein-msft
Copy link
Author

OK, I understand now.

What about a try catch around each header.Add() call (or around all of the calls collectively), and in the catch, return friendly string "Adding header "X" <example: ApplicationName" failed. Even that would have be better than current state.

In this case, you are not owning the validation but are helping the developer isolate where the issue is to fix it.

@anstein-msft
Copy link
Author

anstein-msft commented Feb 22, 2022

Something like below.

Admittedly, looking at the code, the only value I can see that could cause this issue is the ApplicationName (which gets added to the suffix), but this code accounts for future uses of properties in the userAgentContainer, in the AddUserAgentHeader method.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
2 participants