Amazon.SimpleNotificationService.AmazonSimpleNotificationServiceClient.SubscribeAsync C# (CSharp) Méthode

SubscribeAsync() public méthode

Prepares to subscribe an endpoint by sending the endpoint a confirmation message. To actually create a subscription, the endpoint owner must call the ConfirmSubscription action with the token from the confirmation message. Confirmation tokens are valid for three days.
/// Indicates that the user has been denied access to the requested resource. /// /// Indicates an internal service error. /// /// Indicates that a request parameter does not comply with the associated constraints. /// /// Indicates that the requested resource does not exist. /// /// Indicates that the customer already owns the maximum allowed number of subscriptions. ///
public SubscribeAsync ( string topicArn, string protocol, string endpoint, System cancellationToken = default(CancellationToken) ) : Task
topicArn string The ARN of the topic you want to subscribe to.
protocol string The protocol you want to use. Supported protocols include:
  • http -- delivery of JSON-encoded message via HTTP POST
  • https -- delivery of JSON-encoded message via HTTPS POST
  • email -- delivery of message via SMTP
  • email-json -- delivery of JSON-encoded message via SMTP
  • sms -- delivery of message via SMS
  • sqs -- delivery of JSON-encoded message to an Amazon SQS queue
  • application -- delivery of JSON-encoded message to an EndpointArn for a mobile app and device.
  • lambda -- delivery of JSON-encoded message to an AWS Lambda function.
endpoint string The endpoint that you want to receive notifications. Endpoints vary by protocol:
  • For the http protocol, the endpoint is an URL beginning with "http://"
  • For the https protocol, the endpoint is a URL beginning with "https://"
  • For the email protocol, the endpoint is an email address
  • For the email-json protocol, the endpoint is an email address
  • For the sms protocol, the endpoint is a phone number of an SMS-enabled device
  • For the sqs protocol, the endpoint is the ARN of an Amazon SQS queue
  • For the application protocol, the endpoint is the EndpointArn of a mobile app and device.
  • For the lambda protocol, the endpoint is the ARN of an AWS Lambda function.
cancellationToken System /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. ///
Résultat Task
        public Task<SubscribeResponse> SubscribeAsync(string topicArn, string protocol, string endpoint, System.Threading.CancellationToken cancellationToken = default(CancellationToken))
        {
            var request = new SubscribeRequest();
            request.TopicArn = topicArn;
            request.Protocol = protocol;
            request.Endpoint = endpoint;
            return SubscribeAsync(request, cancellationToken);
        }

Same methods

AmazonSimpleNotificationServiceClient::SubscribeAsync ( SubscribeRequest request, System cancellationToken = default(CancellationToken) ) : Task
AmazonSimpleNotificationServiceClient::SubscribeAsync ( SubscribeRequest request, SubscribeResponse>.AmazonServiceCallback callback, AsyncOptions options = null ) : void
AmazonSimpleNotificationServiceClient::SubscribeAsync ( string topicArn, string protocol, string endpoint, SubscribeResponse>.AmazonServiceCallback callback, AsyncOptions options = null ) : void

Usage Example

Exemple #1
0
        public SubscriptionInfo GetOrSubscribe(string topicArn)
        {
            SubscriptionInfo item;

            lock (_ConfirmedSubscriptions) {
                item = _ConfirmedSubscriptions.Where(s => string.Equals(s.TopicArn, topicArn, StringComparison.InvariantCultureIgnoreCase)).SingleOrDefault();
                if (item is object)
                {
                    return(item);
                }
            }

            lock (_LocalPendingSubscriptions) {
                item = _LocalPendingSubscriptions.Where(s => string.Equals(s.TopicArn, topicArn, StringComparison.InvariantCultureIgnoreCase)).SingleOrDefault();
                if (item is object)
                {
                    //orphaned - usually is takes only a few seconds until a subscription is confirmed!
                    if (item.Initiated.AddSeconds(30) < DateTime.Now)
                    {
                        //just remoce thie item, becasue a new one will be created below...
                        _LocalPendingSubscriptions.Remove(item);
                    }
                    return(item);
                }

#if DEBUG
                if (SuppressSubscriptionDevmode)
                {
                    item = new SubscriptionInfo(this, topicArn, "<dummy>", false); //simulate already confirmed subscription
                    _LocalPendingSubscriptions.Add(item);
                    return(item);
                }
#endif
                try {
                    using (var client = new AmazonSimpleNotificationServiceClient(this.AwsCredentials, this.AwsRegion)) {
                        Task <SubscribeResponse> t;
                        if (this.SubscriptionCallbackUrl.StartsWith("https:"))
                        {
                            t = client.SubscribeAsync(topicArn, "https", this.SubscriptionCallbackUrl);
                        }
                        else
                        {
                            t = client.SubscribeAsync(topicArn, "http", this.SubscriptionCallbackUrl);
                        }

                        t.Wait();

                        if (t.IsCompleted && t.Result is object)
                        {
                            item = new SubscriptionInfo(this, topicArn, t.Result.SubscriptionArn, true);
                            _LocalPendingSubscriptions.Add(item);
                        }
                        else
                        {
                            if (t.Exception != null)
                            {
                                throw t.Exception;
                            }
                            else if (t.Result is object)
                            {
                                throw new Exception($"Subscribe-Task failed with Http-Status-Code: {t.Result.HttpStatusCode}!");
                            }
                            else
                            {
                                throw new Exception($"Subscribe-Task failed!");
                            }
                        }
                    }
                }
                catch (AggregateException aex) {
                    foreach (var subex in aex.InnerExceptions)
                    {
                        _ExceptionHandler.Invoke(subex);
                    }
                }
                catch (Exception ex) {
                    _ExceptionHandler.Invoke(ex);
                }
            }

            return(item);
        }
AmazonSimpleNotificationServiceClient