Intranoggin

Blither, Blather, Web Content Management.
Blog » Azure Functions: System.Net.Http–A Very Painful Error

Azure Functions: System.Net.Http–A Very Painful Error

I ran into this error and it took me waaaayy too many hours to troubleshoot. To cut to the chase, there are two versions of System.Net.Http. One that is part of the .NET Framework and comes with VS, the other can get installed via NuGet. If you see an error that looks like this :

Exception while executing function: Functions.AlexaAskTeenageDaughter. mscorlib: Exception has been thrown by the target of an invocation. AlexaFunctions: Method not found: 'System.Net.Http.HttpResponseMessage System.Net.Http.HttpRequestMessageExtensions.CreateResponse(System.Net.Http.HttpRequestMessage, System.Net.HttpStatusCode, !!0)'.

You can correct it by deleting your project reference to System.Net.Http and adding it in again from the list of Framework references.

Here’s how I caused the issue: I was converting an old function project to the new precompiled version. As part of the process I was adding in the NuGet reference to System.Net.Http.Formatting.Extension.

System.Net.Http.Formatting.Extension

This also installs the NuGet package for System.Net.Http

System.Net.Http

If you already had a reference to your local version of the .dll, you won’t have an issue, but if not, this creates a new copy of the file in your project.

Problem Child

What’s odd: The project compiles fine. However when you run it local, you’ll see this error:

Function started (Id=40b33564-7ad0-4657-8ef3-ebe7a8511543)
Function completed (Failure, Id=40b33564-7ad0-4657-8ef3-ebe7a8511543)

A ScriptHost error has occurred
Exception while executing function: Functions.AlexaAskTeenageSon. mscorlib: Exception has been thrown by the target of an invocation. AlexaFunctions: Method not found: 'System.Threading.Tasks.Task`1<!!0> System.Net.Http.HttpContentExtensions.ReadAsAsync(System.Net.Http.HttpContent)'.
Exception while executing function: Functions.AlexaAskTeenageSon. mscorlib: Exception has been thrown by the target of an invocation. AlexaFunctions: Method not found: 'System.Threading.Tasks.Task`1<!!0> System.Net.Http.HttpContentExtensions.ReadAsAsync(System.Net.Http.HttpContent)'.

Exception while executing function: Functions.AlexaAskTeenageSon
Exception while executing function: Functions.AlexaAskTeenageSon. mscorlib: Exception has been thrown by the target of an invocation. AlexaFunctions: Method not found: 'System.Threading.Tasks.Task`1<!!0> System.Net.Http.HttpContentExtensions.ReadAsAsync(System.Net.Http.HttpContent)'.
Executed 'Functions.AlexaAskTeenageSon' (Failed, Id=40b33564-7ad0-4657-8ef3-ebe7a8511543)
mscorlib: Exception while executing function: Functions.AlexaAskTeenageSon. mscorlib: Exception has been thrown by the target of an invocation. AlexaFunctions: Method not found: 'System.Threading.Tasks.Task`1<!!0> System.Net.Http.HttpContentExtensions.ReadAsAsync(System.Net.Http.HttpContent)'.
  Function had errors. See Azure WebJobs SDK dashboard for details. Instance ID is '40b33564-7ad0-4657-8ef3-ebe7a8511543'
mscorlib: Exception while executing function: Functions.AlexaAskTeenageSon. mscorlib: Exception has been thrown by the target of an invocation. AlexaFunctions: Method not found: 'System.Threading.Tasks.Task`1<!!0> System.Net.Http.HttpContentExtensions.ReadAsAsync(System.Net.Http.HttpContent)'.

Publishing it to Azure and executing it there will show something like this in the logs:

Exception while executing function: Functions.AlexaAskTeenageDaughter. mscorlib: Exception has been thrown by the target of an invocation. AlexaFunctions: Method not found: 'System.Net.Http.HttpResponseMessage System.Net.Http.HttpRequestMessageExtensions.CreateResponse(System.Net.Http.HttpRequestMessage, System.Net.HttpStatusCode, !!0)'.

Luckily, it’s an easy fix: first go into the project references. Then locate the System.Net.Http reference. Right click and remove it.

Remove Reference

If you have more than one project in your solution, you may want to remove the reference from all of them.

Now right click the references and select Add. Browse into the framework references and check the System.Net.Http box. Click OK. Repeat for any projects where you removed the reference and you should be good to go.

Add Reference


Posted: 3/30/2017 10:14:00 AM by Ryan Miller | with 0 comments