Exemple #1
0
        IRequestClient <TRequest, TResponse> IRequestClientFactory <TRequest, TResponse> .CreateRequestClient(ConsumeContext consumeContext,
                                                                                                              TimeSpan?timeout, TimeSpan?timeToLive, Action <SendContext <TRequest> > callback)
        {
            Action <SendContext <TRequest> > actualCallback = null;

            if (_callback != null)
            {
                if (callback != null)
                {
                    actualCallback = x =>
                    {
                        _callback(x);
                        callback(x);
                    }
                }
                ;
                else
                {
                    actualCallback = _callback;
                }
            }
            else if (callback != null)
            {
                actualCallback = callback;
            }

            RequestTimeout requestTimeout = timeout ?? RequestTimeout.None;

            var client = _destinationAddress == null
                ? _clientFactory.CreateRequestClient <TRequest>(consumeContext, requestTimeout)
                : _clientFactory.CreateRequestClient <TRequest>(consumeContext, _destinationAddress, requestTimeout);

            return(new MessageRequestClient <TRequest, TResponse>(client, timeToLive ?? _timeToLive, actualCallback));
        }
        /// <summary>
        /// Create a request client from the bus, using the default bus endpoint for responses
        /// </summary>
        /// <param name="consumeContext"></param>
        /// <param name="bus">The bus instance</param>
        /// <param name="destinationAddress">The request service address</param>
        /// <param name="timeout">The default request timeout</param>
        /// <typeparam name="TRequest">The request type</typeparam>
        public static IRequestClient <TRequest> CreateRequestClient <TRequest>(this ConsumeContext consumeContext, IBus bus, Uri destinationAddress,
                                                                               RequestTimeout timeout = default)
            where TRequest : class
        {
            var clientFactory = new ClientFactory(new BusClientFactoryContext(bus, timeout));

            return(clientFactory.CreateRequestClient <TRequest>(consumeContext, destinationAddress, timeout));
        }
Exemple #3
0
        /// <summary>
        /// Create a service client using the bus as the response endpoint for all requests and control traffic.
        /// </summary>
        /// <param name="bus">The bus instance</param>
        /// <param name="timeout">The default timeout for requests</param>
        /// <returns></returns>
        public static IServiceClient CreateServiceClient(this IBus bus, RequestTimeout timeout = default)
        {
            var clientFactoryContext = new BusClientFactoryContext(bus, timeout);
            var clientFactory        = new ClientFactory(clientFactoryContext);
            var serviceClient        = new ServiceClient(clientFactory, clientFactoryContext, bus);

            return(serviceClient);
        }
Exemple #4
0
        /// <summary>
        /// Send a request from the bus to the endpoint, and return a Task which can be awaited for the response.
        /// </summary>
        /// <param name="bus">A started bus instance</param>
        /// <param name="values">The values used to initialize the request message</param>
        /// <param name="cancellationToken">An optional cancellationToken for this request</param>
        /// <param name="timeout">An optional timeout for the request (defaults to 30 seconds)</param>
        /// <param name="callback">A callback, which can modify the <see cref="SendContext"/> of the request</param>
        /// <typeparam name="TRequest">The request type</typeparam>
        /// <typeparam name="TResponse">The response type</typeparam>
        /// <returns></returns>
        public static Task <Response <TResponse> > Request <TRequest, TResponse>(this IBus bus, object values, CancellationToken cancellationToken = default,
                                                                                 RequestTimeout timeout = default, Action <SendContext <TRequest> > callback = null)
            where TRequest : class
            where TResponse : class
        {
            var message = TypeMetadataCache <TRequest> .InitializeFromObject(values);

            return(Request <TRequest, TResponse>(bus, message, cancellationToken, timeout, callback));
        }
Exemple #5
0
        /// <summary>
        /// Create a service client using the bus as the response endpoint for all requests and control traffic.
        /// </summary>
        /// <param name="bus">The bus instance</param>
        /// <param name="timeout">The default timeout for requests</param>
        /// <returns></returns>
        public static IClientFactory CreateServiceClient(this IBus bus, RequestTimeout timeout = default)
        {
            var clientFactory = bus.CreateClientFactory(timeout);

            var serviceClient        = new ServiceClient(clientFactory);
            var serviceClientFactory = new ServiceClientFactory(serviceClient, clientFactory);

            return(serviceClientFactory);
        }
Exemple #6
0
        /// <summary>
        /// If this timeout has a value, return it, otherwise, return the other timeout
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public RequestTimeout Or(RequestTimeout other)
        {
            if (HasValue)
            {
                return(this);
            }

            return(other);
        }
Exemple #7
0
        /// <summary>
        /// Send a request from the bus to the endpoint, and return a Task which can be awaited for the response.
        /// </summary>
        /// <param name="bus">A started bus instance</param>
        /// <param name="message">The request message</param>
        /// <param name="cancellationToken">An optional cancellationToken for this request</param>
        /// <param name="timeout">An optional timeout for the request (defaults to 30 seconds)</param>
        /// <param name="callback">A callback, which can modify the <see cref="SendContext"/> of the request</param>
        /// <typeparam name="TRequest">The request type</typeparam>
        /// <typeparam name="TResponse">The response type</typeparam>
        /// <returns></returns>
        public static Task <Response <TResponse> > Request <TRequest, TResponse>(this IBus bus, TRequest message, CancellationToken cancellationToken = default,
                                                                                 RequestTimeout timeout = default, Action <SendContext <TRequest> > callback = null)
            where TRequest : class
            where TResponse : class
        {
            var requestClient = bus.CreateRequestClient <TRequest>(timeout);

            var requestHandle = requestClient.Create(message, cancellationToken);

            if (callback != null)
            {
                requestHandle.UseExecute(callback);
            }

            return(requestHandle.GetResponse <TResponse>());
        }
        /// <summary>
        /// Connects a client factory to a host receive endpoint, using the bus as the send endpoint provider
        /// </summary>
        /// <param name="receiveEndpoint"></param>
        /// <param name="timeout"></param>
        /// <returns></returns>
        public static IClientFactory CreateClientFactory(this ReceiveEndpointReady receiveEndpoint, RequestTimeout timeout = default)
        {
            var context = new ReceiveEndpointClientFactoryContext(receiveEndpoint, timeout);

            return(new ClientFactory(context));
        }
 /// <summary>
 /// Create a client factory from the bus, which uses the default bus endpoint for any response messages
 /// </summary>
 /// <param name="bus">THe bus instance</param>
 /// <param name="timeout">The default request timeout</param>
 /// <returns></returns>
 public static IClientFactory CreateClientFactory(this IBus bus, RequestTimeout timeout = default)
 {
     return(new ClientFactory(new BusClientFactoryContext(bus, timeout)));
 }
        /// <summary>
        /// Connects a new receive endpoint to the host, and creates a <see cref="IClientFactory"/>.
        /// </summary>
        /// <param name="host">The host to connect the new receive endpoint</param>
        /// <param name="timeout">The default request timeout</param>
        /// <returns></returns>
        public static Task <IClientFactory> CreateClientFactory(this IServiceBusHost host, RequestTimeout timeout = default)
        {
            var receiveEndpointHandle = host.ConnectResponseEndpoint();

            return(receiveEndpointHandle.CreateClientFactory(timeout));
        }
        /// <summary>
        /// Create a request client from the bus, using the default bus endpoint for responses, and publishing the request versus sending it.
        /// </summary>
        /// <param name="bus">The bus instance</param>
        /// <param name="timeout">The default request timeout</param>
        /// <typeparam name="TRequest">The request type</typeparam>
        public static IRequestClient <TRequest> CreateRequestClient <TRequest>(this IBus bus, RequestTimeout timeout = default)
            where TRequest : class
        {
            var clientFactory = new ClientFactory(new BusClientFactoryContext(bus, timeout));

            return(clientFactory.CreateRequestClient <TRequest>(timeout));
        }
        /// <summary>
        /// Connects a new receive endpoint to the host, and creates a <see cref="IClientFactory" />.
        /// </summary>
        /// <param name="connector">The host to connect the new receive endpoint</param>
        /// <param name="timeout">The default request timeout</param>
        /// <returns></returns>
        public static Task <IClientFactory> ConnectClientFactory(this IReceiveConnector connector, RequestTimeout timeout = default)
        {
            var endpointDefinition = new TemporaryEndpointDefinition();

            var receiveEndpointHandle = connector.ConnectReceiveEndpoint(endpointDefinition, KebabCaseEndpointNameFormatter.Instance);

            return(receiveEndpointHandle.CreateClientFactory(timeout));
        }
        /// <summary>
        /// Connects a new receive endpoint to the host, and creates a <see cref="IClientFactory" />.
        /// </summary>
        /// <param name="connector">The host to connect the new receive endpoint</param>
        /// <param name="timeout">The default request timeout</param>
        /// <returns></returns>
        public static Task <IClientFactory> CreateClientFactory(this IReceiveConnector connector, RequestTimeout timeout = default)
        {
            var receiveEndpointHandle = connector.ConnectResponseEndpoint();

            return(receiveEndpointHandle.CreateClientFactory(timeout));
        }
        /// <summary>
        /// Connects a client factory to a host receive endpoint, using the bus as the send endpoint provider
        /// </summary>
        /// <param name="receiveEndpointHandle">
        /// A handle to the receive endpoint, which is stopped when the client factory is disposed
        /// </param>
        /// <param name="timeout"></param>
        /// <returns></returns>
        public static async Task <IClientFactory> CreateClientFactory(this HostReceiveEndpointHandle receiveEndpointHandle, RequestTimeout timeout = default)
        {
            var ready = await receiveEndpointHandle.Ready.ConfigureAwait(false);

            var context = new HostReceiveEndpointClientFactoryContext(receiveEndpointHandle, ready, timeout);

            return(new ClientFactory(context));
        }
 /// <summary>
 /// Create a request client, using the specified service address, using the <see cref="IClientFactory" /> from the container.
 /// </summary>
 /// <param name="scope"></param>
 /// <param name="destinationAddress">The destination service address</param>
 /// <param name="timeout">The default timeout for requests</param>
 /// <typeparam name="T"></typeparam>
 /// <returns></returns>
 public static IRequestClient <T> CreateRequestClient <T>(this ILifetimeScope scope, Uri destinationAddress, RequestTimeout timeout = default)
     where T : class
 {
     return(scope.Resolve <IClientFactory>().CreateRequestClient <T>(destinationAddress, timeout));
 }
Exemple #16
0
 /// <summary>
 /// Create a request client, using the specified service address, using the <see cref="IClientFactory" /> from the container.
 /// </summary>
 /// <param name="provider"></param>
 /// <param name="destinationAddress">The destination service address</param>
 /// <param name="timeout">The default timeout for requests</param>
 /// <typeparam name="T"></typeparam>
 /// <returns></returns>
 public static IRequestClient <T> CreateRequestClient <T>(this IServiceProvider provider, Uri destinationAddress, RequestTimeout timeout = default)
     where T : class
 {
     return(provider.GetRequiredService <IClientFactory>().CreateRequestClient <T>(destinationAddress, timeout));
 }
Exemple #17
0
 /// <summary>
 /// Creates a new RPC client factory on RabbitMQ using the direct reply-to feature
 /// </summary>
 /// <param name="connector">The connector, typically the bus instance</param>
 /// <param name="timeout">The default request timeout</param>
 /// <returns></returns>
 public static Task <IClientFactory> CreateReplyToClientFactory(this IReceiveConnector connector, RequestTimeout timeout = default)
 {
     var endpointDefinition = new ReplyToEndpointDefinition(default, 1000);
        /// <summary>
        /// Connects a new receive endpoint to the host, and creates a <see cref="IClientFactory"/>.
        /// </summary>
        /// <param name="host">The host to connect the new receive endpoint</param>
        /// <param name="timeout">The default request timeout</param>
        /// <returns></returns>
        public static Task <IClientFactory> CreateClientFactory(this IRabbitMqHost host, RequestTimeout timeout = default)
        {
            var receiveEndpointHandle = ConnectResponseEndpoint(host);

            return(receiveEndpointHandle.CreateClientFactory(timeout));
        }
Exemple #19
0
        /// <summary>
        /// Connects a new receive endpoint to the host, and creates a <see cref="IClientFactory"/>.
        /// </summary>
        /// <param name="host">The host to connect the new receive endpoint</param>
        /// <param name="timeout">The default request timeout</param>
        /// <returns></returns>
        public static Task <IClientFactory> CreateClientFactory(this IInMemoryHost host, RequestTimeout timeout = default)
        {
            var receiveEndpointHandle = host.ConnectReceiveEndpoint(host.Topology.CreateTemporaryResponseQueueName());

            return(receiveEndpointHandle.CreateClientFactory(timeout));
        }
Exemple #20
0
        /// <summary>
        /// Send a request from the bus to the endpoint, and return a Task which can be awaited for the response.
        /// </summary>
        /// <param name="bus">A started bus instance</param>
        /// <param name="message">The request message</param>
        /// <param name="cancellationToken">An optional cancellationToken for this request</param>
        /// <param name="timeout">An optional timeout for the request (defaults to 30 seconds)</param>
        /// <param name="callback">A callback, which can modify the <see cref="SendContext" /> of the request</param>
        /// <typeparam name="TRequest">The request type</typeparam>
        /// <typeparam name="TResponse">The response type</typeparam>
        /// <returns></returns>
        public static async Task <Response <TResponse> > Request <TRequest, TResponse>(this IBus bus, TRequest message,
                                                                                       CancellationToken cancellationToken = default,
                                                                                       RequestTimeout timeout = default, Action <SendContext <TRequest> > callback = null)
            where TRequest : class
            where TResponse : class
        {
            IRequestClient <TRequest> requestClient = bus.CreateRequestClient <TRequest>(timeout);

            using (RequestHandle <TRequest> requestHandle = requestClient.Create(message, cancellationToken))
            {
                if (callback != null)
                {
                    requestHandle.UseExecute(callback);
                }

                return(await requestHandle.GetResponse <TResponse>().ConfigureAwait(false));
            }
        }
Exemple #21
0
        /// <summary>
        /// Send a request from the bus to the endpoint, and return a Task which can be awaited for the response.
        /// </summary>
        /// <param name="bus">A started bus instance</param>
        /// <param name="destinationAddress">The service address</param>
        /// <param name="values">The values used to initialize the request message</param>
        /// <param name="cancellationToken">An optional cancellationToken for this request</param>
        /// <param name="timeout">An optional timeout for the request (defaults to 30 seconds)</param>
        /// <param name="callback">A callback, which can modify the <see cref="SendContext" /> of the request</param>
        /// <typeparam name="TRequest">The request type</typeparam>
        /// <typeparam name="TResponse">The response type</typeparam>
        /// <returns></returns>
        public static async Task <Response <TResponse> > Request <TRequest, TResponse>(this IBus bus, Uri destinationAddress, object values,
                                                                                       CancellationToken cancellationToken = default, RequestTimeout timeout = default, Action <SendContext <TRequest> > callback = null)
            where TRequest : class
            where TResponse : class
        {
            var message = await MessageInitializerCache <TRequest> .InitializeMessage(values, cancellationToken).ConfigureAwait(false);

            return(await Request <TRequest, TResponse>(bus, destinationAddress, message, cancellationToken, timeout, callback).ConfigureAwait(false));
        }
        /// <summary>
        /// Create a request client from the bus, creating a response endpoint, and publishing the request versus sending it.
        /// </summary>
        /// <param name="host">The host to connect the new receive endpoint</param>
        /// <param name="timeout">The default request timeout</param>
        /// <returns></returns>
        public static async Task <IRequestClient <T> > CreateRequestClient <T>(this IRabbitMqHost host, RequestTimeout timeout = default)
            where T : class
        {
            var clientFactory = await CreateClientFactory(host, timeout).ConfigureAwait(false);

            return(clientFactory.CreateRequestClient <T>());
        }
Exemple #23
0
        /// <summary>
        /// Connects a new receive endpoint to the host, and creates a <see cref="IClientFactory"/>.
        /// </summary>
        /// <param name="host">The host to connect the new receive endpoint</param>
        /// <param name="destinationAddress">The request service address</param>
        /// <param name="timeout">The default request timeout</param>
        /// <returns></returns>
        public static async Task <IRequestClient <T> > CreateRequestClient <T>(this IServiceBusHost host, Uri destinationAddress, RequestTimeout timeout = default)
            where T : class
        {
            var clientFactory = await CreateClientFactory(host, timeout).ConfigureAwait(false);

            return(clientFactory.CreateRequestClient <T>(destinationAddress));
        }