HttpClient and ReadAsAsync<T>() extension method - c#

So I'm starting up a new .Net 4.0 project and will be doing some work with a public API. I'm planning on using the Microsoft HttpClient class so I installed the latest stable version of the Microsoft.Net.Http NuGet package (version 2.2.13). I'm looking at some POC code that a coworker put together, also using a NuGet package for HttpClient and notice that there's code like this:
HttpClient client = new HttpClient();
HttpResponseMessage response = client.GetAync("/uri").Result;
DomainType result = response.Content.ReadAsAsync<DomainType>().Result;
In my project, after adding the reference to the Microsoft.Net.Http package, when I try to write similar code, I noticed that HttpResponseMessage doesn't have a ReadAsAsync<T>() method. After doing some digging in my coworker's POC solution, it looks like ReadAsAsync<T>() is actually an extension method in the System.Net.Http.Formatting assembly. In this POC solution, there's a reference to System.Net.Http.Formatting, however it's pulling this file from the path C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 4\Assemblies\System.Net.Http.Formatting.dll.
My question is, is there a better solution to getting access to this extension method than referencing this assembly from my local ASP.Net installation? My concern is that this is going to cause a problem on our CI server since it's not likely to have ASP.Net installed. I guess I could copy System.Net.Http.Formatting to a Lib directory in my solution but I'm hoping there's a better option such as another NuGet package I'm missing that would give me this assembly.
Thanks!

That's part of the WebAPI client libraries package. Install the Microsoft.AspNet.WebApi.Client nuget package.

Related

"method not found" error when using a project that have references added using the HtmlSanitizer nuget

I have a C# class library project called Helpers which uses the nuget called HtmlSanitizer.
In my web application (which is located inside the same solution), I'm referencing the Helpers project. When I call one of the helper methods that instantiates a HtmlSanitizer, I get the following error:
Method not found: 'Void Ganss.XSS.HtmlSanitizer..ctor(…)'
The error disappears if I add the HtmlSanitizer nuget to the web application.
Since my Helpers project is used in many other projects and web applications (and even referenced in other solutions), it is not viable for me to add the nuget to all of them (imagine the maintenance cost if I have to upgrade the version or use a different nuget…). What's the solution?
This occurs because there are older version of the HtmlSanitizer DLL still present on disk. You need to clean the solution before building, and if not enough, manually delete the older DLLs from disk.
There might be a problem with the HtmlSanitizer nuget. It seems to add references to HtmlSanitizer version 3.0.0.0 to the project, which does not match the version of the DLL and which does not change either when you update the nuget. See bug References to HtmlSanitizer have wrong version number when using the nuget.

Refer assemblies(artifacts) from artifactory in .NET

I'm trying to make my .Net project work with Artifactory. So far I've uploaded the reference assemblies(.dll) files to artifactory using Jenkins. Now to build my solution, I want to refer those uploaded reference assemblies(artifacts) from artifactory, rather than referring them from the local path.
I tried using the MSBuild artifactory plugin, but it has a partial support for Jenkins.
Is there a way to do this?
You can not reference dll directly from Artifactory. To use them as dependencies in your project you have two options :
first one is to download them locally before your build (you can
setup a prebuild step for that)
second one is to create a nuget
package containing these dlls, to upload this nuget package to
Artifactory (this is where the MsBuild Artifactory plugin can help
you) and use it as a nuget dependency within your project where the
nuget feed used by Visual Studio will be configured to reach
Artifactory nuget repository.

Adding DnxWorkspace into my project, Roslyn

I am looking into the .NET MVC projects, and is trying really hard to implement DNX workspace so I can attempt to use DNX workspace instead of MSBuildWorkspace.
I have met the error that
Could not install package 'Microsoft.CodeAnalysis.Workspaces.Dnx 2.4.0'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.6.2', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.
There's no documentation on this error. I tried on all possible versions of .NETframework. I tried downloading and manually adding it as well. (same error)
It seems that this is the only way of changing the analyzer to be able to read MVC projects.
Any help will be appreciated!
For handling Projects with a Project Json you can also use the Package Microsoft.DotNet.ProjectModel.Workspaces which includes the ProjectJsonWorkspace class. You can use it almost the same as MSBuildWorkspace:
var project = new ProjectJsonWorkspace(projectPath);
var projects = project.CurrentSolution.Projects;
I hope this has helped you.

Unable to find System.Net.Http in web site project

I am working with a web site project within Visual Studio 2015. I usually work with web applications so this type of project is new to me.
This application consist of ASP.NET pages (aspx and .cs code behind files). I wanted to add some code to one of the code behind files that would make use of the HttpClient library, however I cannot seem to add a reference to System.Net.Http. It is not present in the references. I do see that the project is targeting .NET 4.0. Could that be the issue?
You can use NuGet to install this assembly. Just run it in the Package Manager Console(View -> Other Windows -> Package Manager Console):
Install-Package System.Net.Http
UPDATE
If you use .NET 4.0 then you can't install the latest version of System.Net.Http because it doesn't compatible with that framework. You have to use:
Install-Package Microsoft.Net.Http -Version 2.0.20710

Adding a reference to System.Web.Http in a Console application

I am using Visual Studio 2013 and have written a trivial console application in which I wish to use the class HttpError which is present in the assembly System.Web.Http. This assembly comes from NuGet. Installing the assembly does not add it to the project References which I find confusing. I then found others who indicated that the correct assembly comes with the NuGet package "Microsoft ASP.NET Web API 2.2 Core Libraries" so I installed that instead. This package downloads 3 libraries and the required assembly is present but no Reference is added to the project. I can hand edit the csproj and manually add the reference but I am thinking that doing so should not be necessary. What am I doing wrong?
To reproduce:
Create a new C# Console application (latest framework is fine)
Add the above NuGet package
Is there a reference to System.Web.Http? If not then this is my problem.
Thanks for the responses in the comments which indicated that this worked.
I cannot explain it but it was not working yesterday but it is working now. I tested it many times before posting the question.

Categories

Resources