2012-10-22 5 views
21

Sto tentando di pubblicare con Amazon AWSSDK per C# e Simple Notification Service.Amazon Simple Notification Service AWSSDK C# - S.O.S

Non ci sono campioni forniti con l'SDK e non ci sono campioni sul web che potrei trovare dopo 2 ore di ricerca su Google. Mi è venuto in mente questo, ma sta generando un'eccezione che non fornisce più informazioni della singola stringa, "TopicARN" - nessuna eccezione interna - nuffin!
Se qualcuno ha inviato correttamente un messaggio con SNS tramite C# utilizzando il AWSSDK Mi piacerebbe vedere anche l'esempio di funzionamento più rudimentale. Sto usando l'ultimo SDK 1.5x

Ecco il codice:

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

Ecco il link per i codici: http://docs.aws.amazon.com/sdkfornet/latest/apidocs/Index.html –

risposta

27

Ecco un esempio che crea un argomento, imposta un nome visualizzato argomento, sottoscrive un indirizzo email al tema, invia un messaggio e cancella l'argomento. Nota che ci sono due punti in cui dovresti aspettare/controllare la tua e-mail prima di continuare. Client è l'istanza client, topicName è un nome di argomento arbitrario.

// 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. Questo funziona ed è ECCELLENTE. Ti dispiacerebbe commentare come sei arrivato a questo codice considerando la mancanza di campioni in rete tra cui il sito AWS? –

+3

Ci sono varie [risorse di documentazione sul sito AWS ufficiale] (http://aws.amazon.com/documentation/), come ad esempio [Guida introduttiva ad Amazon SNS] (http://docs.amazonwebservices.com/ SNS/ultima/GSG/Welcome.html). Puoi anche trovare informazioni utili e porre domande sui [Forum di AWS] (https://forums.aws.amazon.com/index.jspa). Ma a volte aiuta solo a far parte del team .NET SDK. :) –

+0

Perfetto. Questo ha tutto ciò di cui ho bisogno. Grazie. – CodeWarrior