我正在尝试使用亚马逊的AWSSDK发布C#和简单通知服务。
SDK没有附带样本,在网上搜索2小时后,网上没有任何样本。我想出了这个,但它抛出的异常不会产生比单个字符串更多的信息,“TopicARN” - 没有内部异常 - nuffin!
如果有人使用 AWSSDK 通过C#成功发送了SNS
消息,我很乐意看到即使是最基本的工作示例。我使用的是最新的SDK 1.5x
以下是代码:
string resourceName = "arn:aws:sns:us-east-1:xxxxxxxxxxxx:StackOverFlowStub";
AmazonSimpleNotificationServiceClient snsclient = new AmazonSimpleNotificationServiceClient(accesskey,secretkey);
AddPermissionRequest permissionRequest = new AddPermissionRequest()
.WithActionNames("Publish")
.WithActionNames(accesskey)
.WithActionNames("PrincipleAllowControl")
.WithActionNames(resourceName);
snsclient.AddPermission(permissionRequest);
PublishRequest pr = new PublishRequest();
pr.WithMessage("Test Msg");
pr.WithTopicArn(resourceName);
pr.WithSubject("Test Subject");
snsclient.Publish(pr);
答案 0 :(得分:28)
以下示例创建主题,设置主题显示名称,为主题订阅电子邮件地址,发送消息并删除主题。请注意,在继续之前,您应该等待/检查电子邮件的两个位置。 Client
是客户端实例,topicName
是一个任意主题名称。
// Create topic
string topicArn = client.CreateTopic(new CreateTopicRequest
{
Name = topicName
}).CreateTopicResult.TopicArn;
// Set display name to a friendly value
client.SetTopicAttributes(new SetTopicAttributesRequest
{
TopicArn = topicArn,
AttributeName = "DisplayName",
AttributeValue = "StackOverflow Sample Notifications"
});
// Subscribe an endpoint - in this case, an email address
client.Subscribe(new SubscribeRequest
{
TopicArn = topicArn,
Protocol = "email",
Endpoint = "sample@example.com"
});
// When using email, recipient must confirm subscription
Console.WriteLine("Please check your email and press enter when you are subscribed...");
Console.ReadLine();
// Publish message
client.Publish(new PublishRequest
{
Subject = "Test",
Message = "Testing testing 1 2 3",
TopicArn = topicArn
});
// Verify email receieved
Console.WriteLine("Please check your email and press enter when you receive the message...");
Console.ReadLine();
// Delete topic
client.DeleteTopic(new DeleteTopicRequest
{
TopicArn = topicArn
});