Skip to content

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

@dashiell-zhang

Description

@dashiell-zhang
<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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions