Yassir S
Yassir S

Reputation: 1042

Azure Functions : HttpRequestMessage does not contain a definition for 'CreateResponse'

I built a c# Azure functions app triggerred by an Http Request. The purpose of this app is to use digital twin to change the desired status of my device. In order to do that, I have imported the following dependencies (within the project.son file):

  "Microsoft.Azure.Devices": "1.17.2",
  "Microsoft.Azure.Devices.Client": "1.19.0",
  "Microsoft.Azure.Devices.Shared": "1.15.2",
  "Newtonsoft.Json": "12.0.1"

Here is my error, when I want to return a HttpResponseMessage

req.CreateResponse<string>(HttpStatusCode.BadRequest, "Please pass a status on the query string or in the request body");

I get the following error :

[run.csx] 'HttpRequestMessage' does not contain a definition for 'CreateResponse' and the best extension method overload 'HttpRequestMessageExtensions.CreateResponse<string>(HttpRequestMessage, HttpStatusCode, string)' requires a receiver of type 'HttpRequestMessage'

I suspect dependencies to cause this error but I haven't found any solution on internet.

Do you have an idea how to solve this ?

Thanks

Upvotes: 1

Views: 2183

Answers (1)

Jerry Liu
Jerry Liu

Reputation: 17800

Solution:

Go to kudu(https://<functionapp>.scm.azurewebsites.net/DebugConsole) and navigate to D:\home\data\Functions\packages\nuget, delete system.net.http package then restart the function app.

Explanation:

HttpRequestMessage locates at assembly System.Net.Http.dll. I found that Function host references the assembly from GAC(Global Assembly Cache). On Azure, file path is D:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Net.Http\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Net.Http.dll. The reference cannot be redirected to the one we install using nuget(packages you installed reference System.Net.Http.dll internally), or we'll get error.

Upvotes: 2

Related Questions