3636using System . Reflection ;
3737using System . Security . Authentication ;
3838using System . Text ;
39+ using System . Threading . Tasks ;
3940using RabbitMQ . Client . Exceptions ;
4041using RabbitMQ . Client . Framing . Impl ;
4142using RabbitMQ . Client . Impl ;
@@ -410,6 +411,19 @@ public IConnection CreateConnection()
410411 return CreateConnection ( ClientProvidedName ) ;
411412 }
412413
414+ /// <summary>
415+ /// Asynchronously reate a connection to one of the endpoints provided by the IEndpointResolver
416+ /// returned by the EndpointResolverFactory. By default the configured
417+ /// hostname and port are used.
418+ /// </summary>
419+ /// <exception cref="BrokerUnreachableException">
420+ /// When the configured hostname was not reachable.
421+ /// </exception>
422+ public ValueTask < IConnection > CreateConnectionAsync ( )
423+ {
424+ return CreateConnectionAsync ( ClientProvidedName ) ;
425+ }
426+
413427 /// <summary>
414428 /// Create a connection to one of the endpoints provided by the IEndpointResolver
415429 /// returned by the EndpointResolverFactory. By default the configured
@@ -429,6 +443,25 @@ public IConnection CreateConnection(string clientProvidedName)
429443 return CreateConnection ( EndpointResolverFactory ( LocalEndpoints ( ) ) , clientProvidedName ) ;
430444 }
431445
446+ /// <summary>
447+ /// Asynchronously create a connection to one of the endpoints provided by the IEndpointResolver
448+ /// returned by the EndpointResolverFactory. By default the configured
449+ /// hostname and port are used.
450+ /// </summary>
451+ /// <param name="clientProvidedName">
452+ /// Application-specific connection name, will be displayed in the management UI
453+ /// if RabbitMQ server supports it. This value doesn't have to be unique and cannot
454+ /// be used as a connection identifier, e.g. in HTTP API requests.
455+ /// This value is supposed to be human-readable.
456+ /// </param>
457+ /// <exception cref="BrokerUnreachableException">
458+ /// When the configured hostname was not reachable.
459+ /// </exception>
460+ public ValueTask < IConnection > CreateConnectionAsync ( string clientProvidedName )
461+ {
462+ return CreateConnectionAsync ( EndpointResolverFactory ( LocalEndpoints ( ) ) , clientProvidedName ) ;
463+ }
464+
432465 /// <summary>
433466 /// Create a connection using a list of hostnames using the configured port.
434467 /// By default each hostname is tried in a random order until a successful connection is
@@ -448,6 +481,25 @@ public IConnection CreateConnection(IList<string> hostnames)
448481 return CreateConnection ( hostnames , ClientProvidedName ) ;
449482 }
450483
484+ /// <summary>
485+ /// Asynchronously create a connection using a list of hostnames using the configured port.
486+ /// By default each hostname is tried in a random order until a successful connection is
487+ /// found or the list is exhausted using the DefaultEndpointResolver.
488+ /// The selection behaviour can be overridden by configuring the EndpointResolverFactory.
489+ /// </summary>
490+ /// <param name="hostnames">
491+ /// List of hostnames to use for the initial
492+ /// connection and recovery.
493+ /// </param>
494+ /// <returns>Open connection</returns>
495+ /// <exception cref="BrokerUnreachableException">
496+ /// When no hostname was reachable.
497+ /// </exception>
498+ public ValueTask < IConnection > CreateConnectionAsync ( IList < string > hostnames )
499+ {
500+ return CreateConnectionAsync ( hostnames , ClientProvidedName ) ;
501+ }
502+
451503 /// <summary>
452504 /// Create a connection using a list of hostnames using the configured port.
453505 /// By default each endpoint is tried in a random order until a successful connection is
@@ -474,6 +526,32 @@ public IConnection CreateConnection(IList<string> hostnames, string clientProvid
474526 return CreateConnection ( EndpointResolverFactory ( endpoints ) , clientProvidedName ) ;
475527 }
476528
529+ /// <summary>
530+ /// Asynchronously create a connection using a list of hostnames using the configured port.
531+ /// By default each endpoint is tried in a random order until a successful connection is
532+ /// found or the list is exhausted.
533+ /// The selection behaviour can be overridden by configuring the EndpointResolverFactory.
534+ /// </summary>
535+ /// <param name="hostnames">
536+ /// List of hostnames to use for the initial
537+ /// connection and recovery.
538+ /// </param>
539+ /// <param name="clientProvidedName">
540+ /// Application-specific connection name, will be displayed in the management UI
541+ /// if RabbitMQ server supports it. This value doesn't have to be unique and cannot
542+ /// be used as a connection identifier, e.g. in HTTP API requests.
543+ /// This value is supposed to be human-readable.
544+ /// </param>
545+ /// <returns>Open connection</returns>
546+ /// <exception cref="BrokerUnreachableException">
547+ /// When no hostname was reachable.
548+ /// </exception>
549+ public ValueTask < IConnection > CreateConnectionAsync ( IList < string > hostnames , string clientProvidedName )
550+ {
551+ IEnumerable < AmqpTcpEndpoint > endpoints = hostnames . Select ( h => new AmqpTcpEndpoint ( h , Port , Ssl , MaxMessageSize ) ) ;
552+ return CreateConnectionAsync ( EndpointResolverFactory ( endpoints ) , clientProvidedName ) ;
553+ }
554+
477555 /// <summary>
478556 /// Create a connection using a list of endpoints. By default each endpoint will be tried
479557 /// in a random order until a successful connection is found or the list is exhausted.
@@ -492,6 +570,24 @@ public IConnection CreateConnection(IList<AmqpTcpEndpoint> endpoints)
492570 return CreateConnection ( endpoints , ClientProvidedName ) ;
493571 }
494572
573+ /// <summary>
574+ /// Asynchronously create a connection using a list of endpoints. By default each endpoint will be tried
575+ /// in a random order until a successful connection is found or the list is exhausted.
576+ /// The selection behaviour can be overridden by configuring the EndpointResolverFactory.
577+ /// </summary>
578+ /// <param name="endpoints">
579+ /// List of endpoints to use for the initial
580+ /// connection and recovery.
581+ /// </param>
582+ /// <returns>Open connection</returns>
583+ /// <exception cref="BrokerUnreachableException">
584+ /// When no hostname was reachable.
585+ /// </exception>
586+ public ValueTask < IConnection > CreateConnectionAsync ( IList < AmqpTcpEndpoint > endpoints )
587+ {
588+ return CreateConnectionAsync ( endpoints , ClientProvidedName ) ;
589+ }
590+
495591 /// <summary>
496592 /// Create a connection using a list of endpoints. By default each endpoint will be tried
497593 /// in a random order until a successful connection is found or the list is exhausted.
@@ -516,6 +612,30 @@ public IConnection CreateConnection(IList<AmqpTcpEndpoint> endpoints, string cli
516612 return CreateConnection ( EndpointResolverFactory ( endpoints ) , clientProvidedName ) ;
517613 }
518614
615+ /// <summary>
616+ /// Asynchronously create a connection using a list of endpoints. By default each endpoint will be tried
617+ /// in a random order until a successful connection is found or the list is exhausted.
618+ /// The selection behaviour can be overridden by configuring the EndpointResolverFactory.
619+ /// </summary>
620+ /// <param name="endpoints">
621+ /// List of endpoints to use for the initial
622+ /// connection and recovery.
623+ /// </param>
624+ /// <param name="clientProvidedName">
625+ /// Application-specific connection name, will be displayed in the management UI
626+ /// if RabbitMQ server supports it. This value doesn't have to be unique and cannot
627+ /// be used as a connection identifier, e.g. in HTTP API requests.
628+ /// This value is supposed to be human-readable.
629+ /// </param>
630+ /// <returns>Open connection</returns>
631+ /// <exception cref="BrokerUnreachableException">
632+ /// When no hostname was reachable.
633+ /// </exception>
634+ public ValueTask < IConnection > CreateConnectionAsync ( IList < AmqpTcpEndpoint > endpoints , string clientProvidedName )
635+ {
636+ return CreateConnectionAsync ( EndpointResolverFactory ( endpoints ) , clientProvidedName ) ;
637+ }
638+
519639 /// <summary>
520640 /// Create a connection using an IEndpointResolver.
521641 /// </summary>
@@ -539,10 +659,52 @@ public IConnection CreateConnection(IEndpointResolver endpointResolver, string c
539659 {
540660 if ( AutomaticRecoveryEnabled )
541661 {
542- return new AutorecoveringConnection ( config , endpointResolver ) ;
662+ var c = new AutorecoveringConnection ( config , endpointResolver ) ;
663+ return c . Open ( ) ;
664+ }
665+ else
666+ {
667+ var c = new Connection ( config , endpointResolver . SelectOne ( CreateFrameHandler ) ) ;
668+ return c . Open ( ) ;
543669 }
670+ }
671+ catch ( Exception e )
672+ {
673+ throw new BrokerUnreachableException ( e ) ;
674+ }
675+ }
544676
545- return new Connection ( config , endpointResolver . SelectOne ( CreateFrameHandler ) ) ;
677+ /// <summary>
678+ /// Asynchronously create a connection using an IEndpointResolver.
679+ /// </summary>
680+ /// <param name="endpointResolver">
681+ /// The endpointResolver that returns the endpoints to use for the connection attempt.
682+ /// </param>
683+ /// <param name="clientProvidedName">
684+ /// Application-specific connection name, will be displayed in the management UI
685+ /// if RabbitMQ server supports it. This value doesn't have to be unique and cannot
686+ /// be used as a connection identifier, e.g. in HTTP API requests.
687+ /// This value is supposed to be human-readable.
688+ /// </param>
689+ /// <returns>Open connection</returns>
690+ /// <exception cref="BrokerUnreachableException">
691+ /// When no hostname was reachable.
692+ /// </exception>
693+ public ValueTask < IConnection > CreateConnectionAsync ( IEndpointResolver endpointResolver , string clientProvidedName )
694+ {
695+ ConnectionConfig config = CreateConfig ( clientProvidedName ) ;
696+ try
697+ {
698+ if ( AutomaticRecoveryEnabled )
699+ {
700+ var c = new AutorecoveringConnection ( config , endpointResolver ) ;
701+ return c . OpenAsync ( ) ;
702+ }
703+ else
704+ {
705+ var c = new Connection ( config , endpointResolver . SelectOne ( CreateFrameHandler ) ) ;
706+ return c . OpenAsync ( ) ;
707+ }
546708 }
547709 catch ( Exception e )
548710 {
0 commit comments