Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8475421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:53:17+00:00 2026-06-10T17:53:17+00:00

I am using the new Web API bits in a project, and I have

  • 0

I am using the new Web API bits in a project, and I have found that I cannot use the normal HttpMessageRequest, as I need to add client certificates to the request. As a result, I am using the HttpClient (so I can use WebRequestHandler). This all works well, except that it isn’t stub/mock friendly, at least for Rhino Mocks.

I would normally create a wrapper service around HttpClient that I would use instead, but I would like to avoid this if possible, as there are a lot of methods that I would need to wrap. I am hoping that I have missing something—any suggestions on how to stub HttpClient?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T17:53:18+00:00Added an answer on June 10, 2026 at 5:53 pm

    I use Moq and I can stub out the HttpClient. I think this the same for Rhino Mock (I haven’t tried by myself).
    If you just want to stub the HttpClient the below code should work:

    var stubHttpClient = new Mock<HttpClient>();
    ValuesController controller = new ValuesController(stubHttpClient.Object);
    

    Please correct me if I’m wrong. I guess you are referring to here is that stubbing out members within HttpClient.

    Most popular isolation/mock object frameworks won’t allow you to stub/setup on non- virtual members
    For example the below code throws an exception

    stubHttpClient.Setup(x => x.BaseAddress).Returns(new Uri("some_uri");
    

    You also mentioned that you would like to avoid creating a wrapper because you would wrap lot of HttpClient members. Not clear why you need to wrap lots of methods but you can easily wrap only the methods you need.

    For example :

    public interface IHttpClientWrapper  {   Uri BaseAddress { get;  }     }
    
    public class HttpClientWrapper : IHttpClientWrapper
    {
       readonly HttpClient client;
    
       public HttpClientWrapper()   {
           client = new HttpClient();
       }
    
       public Uri BaseAddress   {
           get
           {
               return client.BaseAddress;
           }
       }
    }
    

    The other options that I think might benefit for you (plenty of examples out there so I won’t write the code)
    Microsoft Moles Framework
    http://research.microsoft.com/en-us/projects/moles/
    Microsoft Fakes: (if you are using VS2012 Ultimate)
    http://msdn.microsoft.com/en-us/library/hh549175.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the new System.Web.Optimization and have created a bundle like this: bundles.Add(New
I am considering building some services using the new Web API that's introduced in
I have multiple projects that need to pass user login information to common-api project
Using VS2010, creating a new MVC 4 Web API project. Just wondering, can the
I have several resources that I'd like to expose using the WCF Web API.
I'm currently planning a new web project. Clients are going to connect using a
I am using the webapplicationbuilder class to create a new web application and have
I'm trying to use the new WCF Web API Preview 6 with Basic Authentication.
I have some internal-facing ASP.NET web services that have had numerous API additions over
I'm using WCF Web API to create a self-hosted/InProcess REST Service (HttpServiceHost) that the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.