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

InvariantGlobalization=true 时调用发送短信会产生异常 #408

Open
dashiell-zhang opened this issue Dec 11, 2023 · 2 comments
Open

Comments

@dashiell-zhang
Copy link

dashiell-zhang commented Dec 11, 2023

<Project Sdk="Microsoft.NET.Sdk.Web">
	<PropertyGroup>
		<TargetFramework>net8.0</TargetFramework>

		<GenerateDocumentationFile>True</GenerateDocumentationFile>
		<DebugType>embedded</DebugType>
		<Nullable>enable</Nullable>
		<ImplicitUsings>enable</ImplicitUsings>
//开启这个属性
		<InvariantGlobalization>true</InvariantGlobalization>
		<UserSecretsId>742f0bcb-df8d-442a-8b51-772aa0ed6c69</UserSecretsId>
		<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
	</PropertyGroup>
</Project>
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Http;
using Aliyun.Acs.Core.Profile;
using Common;
using Microsoft.Extensions.Options;
using SMS.AliCloud.Models;

namespace SMS.AliCloud
{
    public class AliCloudSMS(IOptionsMonitor<SMSSetting> config) : ISMS
    {


        private readonly string accessKeyId = config.CurrentValue.AccessKeyId;


        private readonly string accessKeySecret = config.CurrentValue.AccessKeySecret;

        public bool SendSMS(string signName, string phone, string templateCode, Dictionary<string, string> templateParams)
        {
            try
            {
                string templateParamsJson = JsonHelper.ObjectToJson(templateParams);

                IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", accessKeyId, accessKeySecret);
                DefaultAcsClient client = new(profile);
                CommonRequest request = new()
                {
                    Method = MethodType.POST,
                    Domain = "dysmsapi.aliyuncs.com"
                };
                request.AddQueryParameters("PhoneNumbers", phone);
                request.AddQueryParameters("SignName", signName);
                request.AddQueryParameters("TemplateCode", templateCode);
                request.AddQueryParameters("TemplateParam", templateParamsJson);
                request.Version = "2017-05-25";
                request.Action = "SendSms";

//这里会产生异常
                CommonResponse response = client.GetCommonResponse(request);

                string retValue = System.Text.Encoding.Default.GetString(response.HttpResponse.Content);

                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}

你好,由于 .NET 8 创建的项目默认 InvariantGlobalization 是 True 在调用阿里云的 SDK 进行短信发送时会产生 bug,必须手动修改这个值为 false 才能使用。

和这个应该属于同类型的问题
#399

@JacksonTian
Copy link
Contributor

请问有没有报错的 stack。

@dashiell-zhang
Copy link
Author

@JacksonTian 晚上好,
image

Only the invariant culture is supported in globalization-invariant mode. See https://aka.ms/GlobalizationInvariantMode for more information. (Parameter 'name')
en is an invalid culture identifier.

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