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

Rewrite culture handling logic #2068

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from

Conversation

xingsy97
Copy link
Contributor

@xingsy97 xingsy97 commented Oct 31, 2024

Based on #2067
Fix #2038

@xingsy97 xingsy97 force-pushed the refactor-culture-main branch from d0e4d1d to 7b20491 Compare October 31, 2024 09:19
@@ -160,7 +160,7 @@ private Task ProcessNegotiationRequest(IOwinContext owinContext, HostContext con
// add OriginalPath and QueryString when the clients protocol is higher than 2.0, earlier ASP.NET SignalR clients does not support redirect URL with query parameters
if (!string.IsNullOrEmpty(clientProtocol) && Version.TryParse(clientProtocol, out var version) && version >= ClientSupportQueryStringVersion)
{
var clientRequestId = _connectionRequestIdProvider.GetRequestId();
var clientRequestId = _connectionRequestIdProvider.GetRequestId("");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so for aspnet the id always start with -?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use a prefix "aspnet"

@@ -149,6 +154,12 @@ protected override Task OnClientConnectedAsync(OpenConnectionMessage message)
connection.ServiceConnection = this;

connection.Features.Set<IConnectionMigrationFeature>(null);

if (!_cultureInfoManager.TryApplyCulture(connection.RequestId))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (_cultureInfoManager.TryGetCultureFeature(connection.RequestId, out var feature)){
connection.GetHttpContext().Features.Set<IRequestCultureFeature>(feature)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

connection.GetHttpContext().Features.Set<IRequestCultureFeature>(feature) doesn't take effect
I use CultureInfo.CurrentCulture=XXX and CultureInfo.CureentUICulture=XXX

@xingsy97 xingsy97 force-pushed the refactor-culture-main branch 3 times, most recently from 7972e63 to 2152f07 Compare November 12, 2024 10:13
{
return Convert.ToBase64String(BitConverter.GetBytes(Stopwatch.GetTimestamp()));
return $"{traceIdentifier}-{Convert.ToBase64String(BitConverter.GetBytes(Stopwatch.GetTimestamp()))}";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

traceIdentifier is in such format 0HN7T92QVLV5T:00000009 https://github.com/aspnet/KestrelHttpServer/blob/master/src/Kestrel.Core/Internal/Infrastructure/CorrelationIdGenerator.cs#L19, and could be customized using middlewares.
Do we want : in the requestId? might the generated requestId too long?

Copy link
Contributor Author

@xingsy97 xingsy97 Nov 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now I use return $"{traceIdentifier}:{Stopwatch.GetTimestamp().ToString("X")}";
base64 encoding is not needed for this id will be UrlEncoded before inserted into query string
use hex to shorten the length

src/Microsoft.Azure.SignalR/HubHost/NegotiateHandler.cs Outdated Show resolved Hide resolved
);

_cultureInfoManager.TryAddCultureFeature(clientRequestId, cultureFeature);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible to check if culture feature is unchanged? If it is unchanged, there is no need to add and apply

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it that only blazor app is impacted by the culture?

Copy link
Contributor Author

@xingsy97 xingsy97 Nov 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1\ yes, we can check. However if we don't add the feature, a not found error log will be triggered when trying to apply the culture feature.
2\ added blazor check

@xingsy97 xingsy97 force-pushed the refactor-culture-main branch from 8fd466e to 47b8960 Compare November 14, 2024 08:32
@xingsy97 xingsy97 changed the title Rewrite culture info handling logic Rewrite culture handling logic Nov 15, 2024
@xingsy97 xingsy97 force-pushed the refactor-culture-main branch from e48755d to 9983800 Compare November 15, 2024 09:55
@xingsy97 xingsy97 force-pushed the refactor-culture-main branch from 3f24099 to aaad24b Compare November 19, 2024 06:58
@JialinXin
Copy link
Contributor

Add some UTs?

{
return Convert.ToBase64String(BitConverter.GetBytes(Stopwatch.GetTimestamp()));
// Before filled into query string, this id will be process by "WebUtility.UrlEncode(...)". So base64 encoding is not needed.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's replace : with - to make it more readable, also use string.IsNullorEmpty(traceidentifier) ? {Stopwatch.GetTimestamp().ToString("X") : $"{traceIdentifier}:{Stopwatch.GetTimestamp().ToString("X")}" instead of append aspnet prefix

public bool IsDefaultFeature(IRequestCultureFeature feature)
{
// this is the default feature value in blazor when no culture feature is configured by app server
return feature.RequestCulture.Culture == CultureInfo.DefaultThreadCurrentCulture && feature.RequestCulture.UICulture == CultureInfo.DefaultThreadCurrentUICulture && feature.Provider is AcceptLanguageHeaderRequestCultureProvider;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is AcceptLanguageHeaderRequestCultureProvider does not mean it is default though

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Blazor Server Side Custom CultureInfo getting overridden when using AzureSignalR
3 participants