我目前正在尝试将AWS SNS的SMS服务集成到.NET 6中。我有awssdk.simplenotificationservice .7.300.32\包作为依赖项。这是我的 StartupExtension.cs 中的代码
using Amazon;
using Amazon.Runtime;
using Amazon.SimpleNotificationService;
using DataAccess.Comms;
using DataAccess.Dropdown;
using DataAccess.User;
using Service.BusinessLogic;
using Service.Interface;
using Utilities.Hash;
using Utilities.ListToDataTable;
using Utilities.Logger;
using VO.Model;
namespace API
{
public static class StartupExtention
{
public static IServiceCollection AddCustomServices(
this IServiceCollection services,
IConfiguration configuration,
Configuration configPreset,
IWebHostEnvironment environment
)
{
try
{
// other services
//setup aws creds if dev env for other envs the instance will have the role to do the same
if (environment.IsDevelopment())
{
var aws = configPreset.AWS;
var credentials = new BasicAWSCredentials(aws.AccessKey, aws.SecretKey);
var clientConfig = new AmazonSimpleNotificationServiceConfig
{
// I tried it with both below methods where aws.Region="ap-south-1"
// RegionEndpoint = RegionEndpoint.GetBySystemName(aws.Region),
// RegionEndpoint = RegionEndpoint.APSouth1
};
services.AddSingleton<IAmazonSimpleNotificationService>(
new AmazonSimpleNotificationServiceClient(credentials, clientConfig)
);
}
else
{
services.AddSingleton<IAmazonSimpleNotificationService, AmazonSimpleNotificationServiceClient>();
}
return services;
}
catch (Exception ex)
{
throw ex;
}
}
}
}
它构建项目并启动 swagger 页面,而不会抛出错误。 相关信息:对于这两种方法,clientConfig 的 RegionEndpoing 为 ap-south-1 但 serviceURL 为 null。执行以下代码时
using (var snsClient = new AmazonSimpleNotificationServiceClient())
我收到以下错误 “未配置 RegionEndpoint 或 ServiceURL”
使用以下堆栈跟踪
at Amazon.Runtime.ClientConfig.Validate()
at Amazon.Runtime.AmazonServiceClient..ctor(AWSCredentials credentials, ClientConfig config)
以“AWSSDK.Core”为来源。 使用的凭证附加了 AmazonSNSFullAccess 策略。我尝试了许多不同的解决方案,但似乎没有一个有效。任何帮助都是appriced。
我忘记使用注入的依赖项,而不是创建一个新的对象。