예제 #1
0
 public void Dispose()
 {
     _lock.EnterWriteLock();
     try
     {
         _current = null;
     }
     finally
     {
         _lock.ExitWriteLock();
     }
 }
예제 #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HttpRecorderContext"/> class.
        /// </summary>
        /// <param name="configurationFactory">Factory to allow customization per <see cref="HttpClient"/>.</param>
        /// <param name="testName">The <see cref="CallerMemberNameAttribute"/>.</param>
        /// <param name="filePath">The <see cref="CallerFilePathAttribute"/>.</param>
        /// <example>
        /// <![CDATA[
        /// // In service registration.
        /// services.AddRecorderContextSupport();
        ///
        /// // In the test case.
        /// using var context = new HttpRecorderContext();
        /// ]]>
        /// </example>
        public HttpRecorderContext(
            Func <IServiceProvider, HttpMessageHandlerBuilder, HttpRecorderConfiguration> configurationFactory = null,
            [CallerMemberName] string testName = "",
            [CallerFilePath] string filePath   = "")
        {
            ConfigurationFactory = configurationFactory;
            TestName             = testName;
            FilePath             = filePath;
            _lock.EnterWriteLock();
            try
            {
                if (_current != null)
                {
                    throw new HttpRecorderException(
                              $"Cannot use multiple {nameof(HttpRecorderContext)} at the same time. Previous usage: {_current.FilePath}, current usage: {filePath}.");
                }

                _current = this;
            }
            finally
            {
                _lock.ExitWriteLock();
            }
        }