コード例 #1
0
        /// <summary>
        ///     Gets a <see cref="ISoapClient" /> instance from the factory and releases
        ///     when the action completes.
        /// </summary>
        /// <typeparam name="TSoapClient">The SOAP client type</typeparam>
        /// <typeparam name="TResult">The result type</typeparam>
        /// <param name="factory">The factory to use</param>
        /// <param name="action">The action to execute</param>
        /// <returns>The action result</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static TResult GetAndRelease <TSoapClient, TResult>(this ISoapClientFactory factory, Func <TSoapClient, TResult> action)
            where TSoapClient : ISoapClient
        {
            if (factory == null)
            {
                throw new ArgumentNullException(nameof(factory));
            }
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            var client = factory.Get <TSoapClient>();

            try
            {
                return(action(client));
            }
            finally
            {
                factory.Release(client);
            }
        }
コード例 #2
0
        /// <summary>
        ///     Gets a <see cref="ISoapClient" /> instance from the factory and releases
        ///     when the action completes.
        /// </summary>
        /// <typeparam name="TSoapClient">The SOAP client type</typeparam>
        /// <typeparam name="TResult">The result type</typeparam>
        /// <param name="factory">The factory to use</param>
        /// <param name="action">The action to execute</param>
        /// <param name="ct">The cancellation token</param>
        /// <returns>A task that can be awaited for the result</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static async Task <TResult> GetAndReleaseAsync <TSoapClient, TResult>(
            this ISoapClientFactory factory, Func <TSoapClient, CancellationToken, Task <TResult> > action, CancellationToken ct = default(CancellationToken))
            where TSoapClient : ISoapClient
        {
            if (factory == null)
            {
                throw new ArgumentNullException(nameof(factory));
            }
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            var client = factory.Get <TSoapClient>();

            try
            {
                return(await action(client, ct).ConfigureAwait(false));
            }
            finally
            {
                factory.Release(client);
            }
        }