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

Thread CurrentCulture csredis hash exception with docker container #497

Open
wapco opened this issue Dec 4, 2023 · 15 comments
Open

Thread CurrentCulture csredis hash exception with docker container #497

wapco opened this issue Dec 4, 2023 · 15 comments

Comments

@wapco
Copy link

wapco commented Dec 4, 2023

RedisReader中Int64.Parse需要改成Int64.Parse(line.ToString(), NumberStyles.AllowLeadingSign | NumberStyles.AllowThousands | NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture);否则在欧洲部分国家,比如挪威(nb)环境中会报错。

这个问题很好验证,Thread.CurrentThread.CurrentCulture = new CultureInfo("nb");var test = long.Parse("-1"); 这段语句就会报错,所以需要改成Int64.Parse(line.ToString(), NumberStyles.AllowLeadingSign | NumberStyles.AllowThousands | NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture)

完整代码:
public long ReadInt(bool checkType = true)
{
if (checkType)
ExpectType(RedisMessage.Int);

        string line = ReadLine();
        return Int64.Parse(line.ToString(), NumberStyles.AllowLeadingSign | NumberStyles.AllowThousands | NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture);
    }
@2881099
Copy link
Owner

2881099 commented Dec 4, 2023

推荐使用 FreeRedis

https://github.com/2881099/FreeRedis

目前都在基于这个在更新

@wapco
Copy link
Author

wapco commented Dec 4, 2023

新项目可以用这个,但是老项目已经用了csredis的再去改动工作量很大,这个小bug如果能修复一下就好了

@wapco
Copy link
Author

wapco commented Dec 4, 2023

另外我看了FreeRedis中也是有用到TryParse或者Parse方法,在国外语言环境中,TryParse和Parse都需要加 CultureInfo.InvariantCulture 才能避免格式转化出错。

@2881099
Copy link
Owner

2881099 commented Dec 4, 2023

晚一点回复你,接小孩

@wapco
Copy link
Author

wapco commented Dec 4, 2023

同理,如果有用到 float.ToString()也需要用 .ToString(CultureInfo.InvariantCulture)代替,因为像意大利中 1.23 ToString后的值为 "1,23",因为意大利的小数点是用逗号表示,我当初对接paypal的时候就是因为.ToString(CultureInfo.InvariantCulture)方法没加CultureInfo.InvariantCulture,导致意大利的客户支付报错了。

@2881099
Copy link
Owner

2881099 commented Dec 4, 2023

int.Parse(vs, NumberStyles.Any) 是否也行,FreeRedis 多数是这样写的,之前遇到你类似的问题

@2881099
Copy link
Owner

2881099 commented Dec 4, 2023

image

我这里测试没有报错:

Thread.CurrentThread.CurrentCulture = new CultureInfo("nb");
var test = long.Parse("-1");

@wapco
Copy link
Author

wapco commented Dec 4, 2023

我验证了不行,必须int.Parse(num, NumberStyles.Any, CultureInfo.InvariantCulture); 带上CultureInfo.InvariantCulture才行,另外window环境没问题,必须在docker环境中才会报错

@wapco
Copy link
Author

wapco commented Dec 4, 2023

0F58D710-978F-46ca-A808-AFC5ABD54C6A

@2881099
Copy link
Owner

2881099 commented Dec 4, 2023

只有负数才会这样吗?

@wapco
Copy link
Author

wapco commented Dec 4, 2023

是的,可能挪威语中负数不是-1这种写法,所以导致没办法转换,加了CultureInfo.InvariantCulture就是指定它用英文的文化来转换。

@2881099
Copy link
Owner

2881099 commented Dec 4, 2023

int.TryParse(“-1”, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var len)

这样呢

@wapco
Copy link
Author

wapco commented Dec 4, 2023

这样也不会报错

@wapco
Copy link
Author

wapco commented Dec 4, 2023

先下班,你可以新建一个测试项目,然后启用docker支持,就可以验证这个问题了。。

@wapco
Copy link
Author

wapco commented Dec 5, 2023

经验证新的包正常了,谢谢

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

No branches or pull requests

2 participants