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

SubscribeAsync() public méthode

Initiates the asynchronous execution of the Subscribe operation.
public SubscribeAsync ( SubscribeRequest request, SubscribeResponse>.AmazonServiceCallback callback, AsyncOptions options = null ) : void
request SubscribeRequest Container for the necessary parameters to execute the Subscribe operation on AmazonSimpleNotificationServiceClient.
callback SubscribeResponse>.AmazonServiceCallback An Action delegate that is invoked when the operation completes.
options AsyncOptions A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback /// procedure using the AsyncState property.
Résultat void
        public void SubscribeAsync(SubscribeRequest request, AmazonServiceCallback<SubscribeRequest, SubscribeResponse> callback, AsyncOptions options = null)
        {
            options = options == null?new AsyncOptions():options;
            var marshaller = new SubscribeRequestMarshaller();
            var unmarshaller = SubscribeResponseUnmarshaller.Instance;
            Action<AmazonWebServiceRequest, AmazonWebServiceResponse, Exception, AsyncOptions> callbackHelper = null;
            if(callback !=null )
                callbackHelper = (AmazonWebServiceRequest req, AmazonWebServiceResponse res, Exception ex, AsyncOptions ao) => { 
                    AmazonServiceResult<SubscribeRequest,SubscribeResponse> responseObject 
                            = new AmazonServiceResult<SubscribeRequest,SubscribeResponse>((SubscribeRequest)req, (SubscribeResponse)res, ex , ao.State);    
                        callback(responseObject); 
                };
            BeginInvoke<SubscribeRequest>(request, marshaller, unmarshaller, options, callbackHelper);
        }

Same methods

AmazonSimpleNotificationServiceClient::SubscribeAsync ( SubscribeRequest request, System cancellationToken = default(CancellationToken) ) : Task
AmazonSimpleNotificationServiceClient::SubscribeAsync ( string topicArn, string protocol, string endpoint, System cancellationToken = default(CancellationToken) ) : Task
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