Deciding between HttpClient and WebClient

后端 未结 6 1231
情话喂你
情话喂你 2020-11-22 14:37

Our web app is running in .Net Framework 4.0. The UI calls controller methods through ajax calls.

We need to consume REST service from our vendor. I am evaluating

6条回答
  •  粉色の甜心
    2020-11-22 15:18

    Perhaps you could think about the problem in a different way. WebClient and HttpClient are essentially different implementations of the same thing. What I recommend is implementing the Dependency Injection pattern with an IoC Container throughout your application. You should construct a client interface with a higher level of abstraction than the low level HTTP transfer. You can write concrete classes that use both WebClient and HttpClient, and then use the IoC container to inject the implementation via config.

    What this would allow you to do would be to switch between HttpClient and WebClient easily so that you are able to objectively test in the production environment.

    So questions like:

    Will HttpClient be a better design choice if we upgrade to .Net 4.5?

    Can actually be objectively answered by switching between the two client implementations using the IoC container. Here is an example interface that you might depend on that doesn't include any details about HttpClient or WebClient.

    /// 
    /// Dependency Injection abstraction for rest clients. 
    /// 
    public interface IClient
    {
        /// 
        /// Adapter for serialization/deserialization of http body data
        /// 
        ISerializationAdapter SerializationAdapter { get; }
    
        /// 
        /// Sends a strongly typed request to the server and waits for a strongly typed response
        /// 
        /// The expected type of the response body
        /// The type of the request body if specified
        /// The request that will be translated to a http request
        /// 
        Task> SendAsync(Request request);
    
        /// 
        /// Default headers to be sent with http requests
        /// 
        IHeadersCollection DefaultRequestHeaders { get; }
    
        /// 
        /// Default timeout for http requests
        /// 
        TimeSpan Timeout { get; set; }
    
        /// 
        /// Base Uri for the client. Any resources specified on requests will be relative to this.
        /// 
        Uri BaseUri { get; set; }
    
        /// 
        /// Name of the client
        /// 
        string Name { get; }
    }
    
    public class Request
    {
        #region Public Properties
        public IHeadersCollection Headers { get; }
        public Uri Resource { get; set; }
        public HttpRequestMethod HttpRequestMethod { get; set; }
        public TRequestBody Body { get; set; }
        public CancellationToken CancellationToken { get; set; }
        public string CustomHttpRequestMethod { get; set; }
        #endregion
    
        public Request(Uri resource,
            TRequestBody body,
            IHeadersCollection headers,
            HttpRequestMethod httpRequestMethod,
            IClient client,
            CancellationToken cancellationToken)
        {
            Body = body;
            Headers = headers;
            Resource = resource;
            HttpRequestMethod = httpRequestMethod;
            CancellationToken = cancellationToken;
    
            if (Headers == null) Headers = new RequestHeadersCollection();
    
            var defaultRequestHeaders = client?.DefaultRequestHeaders;
            if (defaultRequestHeaders == null) return;
    
            foreach (var kvp in defaultRequestHeaders)
            {
                Headers.Add(kvp);
            }
        }
    }
    
    public abstract class Response : Response
    {
        #region Public Properties
        public virtual TResponseBody Body { get; }
    
        #endregion
    
        #region Constructors
        /// 
        /// Only used for mocking or other inheritance
        /// 
        protected Response() : base()
        {
        }
    
        protected Response(
        IHeadersCollection headersCollection,
        int statusCode,
        HttpRequestMethod httpRequestMethod,
        byte[] responseData,
        TResponseBody body,
        Uri requestUri
        ) : base(
            headersCollection,
            statusCode,
            httpRequestMethod,
            responseData,
            requestUri)
        {
            Body = body;
        }
    
        public static implicit operator TResponseBody(Response readResult)
        {
            return readResult.Body;
        }
        #endregion
    }
    
    public abstract class Response
    {
        #region Fields
        private readonly byte[] _responseData;
        #endregion
    
        #region Public Properties
        public virtual int StatusCode { get; }
        public virtual IHeadersCollection Headers { get; }
        public virtual HttpRequestMethod HttpRequestMethod { get; }
        public abstract bool IsSuccess { get; }
        public virtual Uri RequestUri { get; }
        #endregion
    
        #region Constructor
        /// 
        /// Only used for mocking or other inheritance
        /// 
        protected Response()
        {
        }
    
        protected Response
        (
        IHeadersCollection headersCollection,
        int statusCode,
        HttpRequestMethod httpRequestMethod,
        byte[] responseData,
        Uri requestUri
        )
        {
            StatusCode = statusCode;
            Headers = headersCollection;
            HttpRequestMethod = httpRequestMethod;
            RequestUri = requestUri;
            _responseData = responseData;
        }
        #endregion
    
        #region Public Methods
        public virtual byte[] GetResponseData()
        {
            return _responseData;
        }
        #endregion
    }
    

    Full code

    HttpClient Implementation

    You can use Task.Run to make WebClient run asynchronously in its implementation.

    Dependency Injection, when done well helps alleviate the problem of having to make low level decisions upfront. Ultimately, the only way to know the true answer is try both in a live environment and see which one works the best. It's quite possible that WebClient may work better for some customers, and HttpClient may work better for others. This is why abstraction is important. It means that code can quickly be swapped in, or changed with configuration without changing the fundamental design of the app.

提交回复
热议问题