aliyun / aliyun-openapi-net-sdk

Alibaba Cloud SDK for .NET
https://www.nuget.org/profiles/aliyun-openapi-sdk
Other
541 stars 625 forks source link

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

Open dashiell-zhang opened 10 months ago

dashiell-zhang commented 10 months ago
<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 才能使用。

和这个应该属于同类型的问题 https://github.com/aliyun/aliyun-openapi-net-sdk/issues/399

JacksonTian commented 10 months ago

请问有没有报错的 stack。

dashiell-zhang commented 10 months ago

@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.