Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CS0433: Ambiguous Reference System.Net.Http.HttpRequestMessageExtensions

I am using VS2015 with Resharper for WebAPI 2.0 project. Trying to use System.Net.Http.HttpRequestMessageExtensions.GetRequestContext gives me an error saying

Error   CS0433  The type 'HttpRequestMessageExtensions' exists in both 'System.Net.Http.Formatting, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' and 'System.Web.Http, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' 

I've tried editing web.config to read

<compilation debug="true" targetFramework="4.5.1" batch="false" />

Also as suggested in numerous posts - restarted visual studio, clear resharper cache, rebuild the solution.

Nothing helps and I am still seeing this error. Any other suggestions?

like image 859
keeda Avatar asked Apr 04 '16 21:04

keeda


1 Answers

You need to use a “extern alias” to manage two classes with the same namespace.

First, define de name of your custom alias in the properties of the assembly:

enter image description here

Then, in the code:

using System.Web.Mvc;

namespace WebApplication1.Controllers
{
    extern alias Alias1;
    extern alias Alias2;

    using namespace1 = Alias1::System.Net.Http.HttpRequestMessageExtensions;
    using namespace2 = Alias2::System.Net.Http.HttpRequestMessageExtensions;

    public class HomeController : Controller
    {
        public void Test()
        {
            // ...
            namespace1.GetRequestContext(request);
            //namespace2.GetRequestContext(request); // error


        }
like image 68
Jose M. Avatar answered Oct 21 '22 19:10

Jose M.