2012-10-22 50 views
21

我試圖與亞馬遜的AWSSDK爲C#和簡單通知服務來發布。Amazon簡單通知服務AWSSDK C# - S.O.S

有沒有樣品附帶的SDK,也沒有樣品網絡2小時谷歌搜索後,我能找到的任何地方。我想出了這個,但它拋出一個異常,不會產生比單個字符串「TopicARN」更多的信息 - 沒有內部異常 - nuffin!
如果有人成功使用AWSSDK發送的消息具有SNS通過C#我很想看到即使是最基本的工作示例。我使用的是最新的SDK 1.5倍

下面的代碼:

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

這裏是鏈接代碼:http://docs.aws.amazon.com/sdkfornet/latest/apidocs/Index.html –

回答

27

下面是創建一個主題的樣本,設置一個主題的顯示名稱,認購的電子郵件地址的話題,發送消息和刪除主題。請注意,有兩個位置應在繼續之前等待/檢查您的電子郵件。 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 = "[email protected]" 
}); 

// 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 
}); 
+0

Osyan。這工作,並且是優秀的。考慮到包括AWS網站在內的網絡上缺少樣本,您是否介意評論您是如何獲得此代碼的? –

+3

在官方AWS站點上有各種[文檔資源](http://aws.amazon.com/documentation/),例如[Amazon SNS指南入門](http://docs.amazonwebservices.com/ SNS /最新/ GSG/Welcome.html)。您還可以在[AWS論壇](https://forums.aws.amazon.com/index.jspa)上找到有用的信息並提出問題。 但是,有時它只是幫助.NET SDK團隊。 :) –

+0

完美。這有我需要的一切。謝謝。 – CodeWarrior