简体   繁体   中英

Calling WebAPI from WebService?

I have this legacy code that is consumed by our partners which is an old webservice. I need to integrate some new functionality by adding a calling to an external WebAPI. When I try to create HttpClient in the cs file of the webservice it says the following

The type or namespace name 'HttpClient' does not exist in the namespace 'System.Net.Http' (are you missing an assembly reference?)

Am I missing something in creating an instance of HttpClient in Webservice. I have added reference to System.Net.Http in my cs file.

UPDATE: This was my workaround. I have created a new 3.5 project, added files that are from my old project, converted it to 4.5 and added references to System.Net.Http and everything is fine now.

The System.Net.Http library is only available in .NET 4.5 - which version are you running?

Alternatively, you can use HttpRequest which is available in all versions.

Seems to me like the HttpClient Class wass added in .Net 4.5 . Your old web service probably runs an older version of .Net. You might want to try to upgrade you service, and change it to .Net 4.5 if possible.

Update: Ok, so you have a .Net 4.5 project. Have you verified that you have added the correct references?

Note that including a reference to System.Net is not the same as adding one to System.Net.Http . The latter found under " Extensions " when adding references in Visual Studio (and not under Framework , where the former is found).

Update 2: Try removing existing references to System.Net.Http , and adding the following NuGet package instead:

Install-Package Microsoft.Net.Http

(Ref: http://www.nuget.org/packages/System.Net.Http and http://www.nuget.org/packages/Microsoft.Net.Http )

Make sure you are referencing System.Net.Http.dll in .NET 4.5.

Try to add reference manually by right clicking on your project in Solution Explorer and click Add References when the dialog appears, select the leftmost tab Scroll the list until you see an item called System.Net.Http double-click it and rebuild.

and make sure your project is targeting .NET 4.5 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM