2012-10-22 35 views
21

Tôi đang cố gắng xuất bản với AWSSDK của Amazon cho C# và Dịch vụ thông báo đơn giản.Dịch vụ thông báo đơn giản Amazon AWSSDK C# - S.O.S

Không có mẫu đi kèm với SDK và không có mẫu nào ở bất kỳ nơi nào trên web mà tôi có thể tìm thấy sau 2 giờ kể từ khi Googling. Tôi đã đưa ra điều này nhưng nó là ném một ngoại lệ mà sản lượng không có nhiều thông tin hơn so với chuỗi đơn, "TopicARN" - không có ngoại lệ bên trong - nuffin!
Nếu có ai đó đã gửi thành công một tin nhắn với SNS qua C# bằng cách sử dụng AWSSDK Tôi rất thích xem ngay cả ví dụ làm việc thô sơ nhất. Tôi đang sử dụng SDK mới nhất 1.5x

Dưới đây là các mã:

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

Đây là liên kết dành cho mã: http://docs.aws.amazon.com/sdkfornet/latest/apidocs/Index.html –

Trả lời

27

Đây là một mẫu tạo ra một chủ đề, đặt tên hiển thị chủ đề, đặt mua một địa chỉ email đến chủ đề, gửi một thông điệp và xóa chủ đề. Lưu ý rằng có hai điểm bạn nên đợi/kiểm tra email trước khi tiếp tục. Client là phiên bản ứng dụng khách, topicName là tên chủ đề tùy ý.

// 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. Điều này làm việc và là tuyệt vời. Bạn có nhớ nhận xét cách bạn đến mã này xem xét việc thiếu mẫu trên mạng bao gồm Trang web AWS không? –

+3

Có nhiều [tài nguyên tài liệu trên trang web chính thức của AWS] (http://aws.amazon.com/documentation/), chẳng hạn như [Bắt đầu với Hướng dẫn SNS của Amazon] (http://docs.amazonwebservices.com/ sns/latest/gsg/Welcome.html). Bạn cũng có thể tìm thấy thông tin hữu ích và đặt câu hỏi trên [Diễn đàn AWS] (https://forums.aws.amazon.com/index.jspa). Nhưng, đôi khi nó chỉ giúp được trên nhóm .NET SDK. :) –

+0

Hoàn hảo. Điều này có mọi thứ tôi cần. Cảm ơn. – CodeWarrior

Các vấn đề liên quan