Azure-Functions icon indicating copy to clipboard operation
Azure-Functions copied to clipboard

Add POCO example with HTTP binding

Open lindydonna opened this issue 9 years ago • 12 comments

Most customers don't know you can bind to both an HttpRequestMessage and a POCO. We should add it to the docs.

Example of POCO: https://github.com/Azure-Samples/functions-dotnet-codercards/blob/master/CoderCards/CardGenerator.cs#L47

lindydonna avatar Jul 18 '17 23:07 lindydonna

In the example above, you're binding the HttpTrigger directly to a POCO, which is awesome, but is it possible to bind to literally both the HttpRequestMessage and a POCO at the same time?

For example: public static HttpResponseMessage Run([HttpTrigger(...)]HttpRequestMessage request, [HttpTrigger(...)]POCO deserializedRequestBody)?

Alternatively, it is possible to create a HttpResponseMessage without needing the bound request?

duckwaffle avatar Jul 19 '17 02:07 duckwaffle

You can do this, but you'd put the trigger attribute on just the POCO. So it would be:

Run([HttpTrigger(...)] POCO myObj, HttpRequestMessage request)

HttpRequestMessage would then be populated in a similar manner as special objects like TraceWriter.

lindydonna avatar Jul 19 '17 18:07 lindydonna

Neat! Thanks for the info :)

duckwaffle avatar Jul 19 '17 23:07 duckwaffle

This feature is awesome! I stumbled upon it by accident when building .csx functions and it has been a lifesaver. For some reason I can't get this to work with the new VS 2017 tooling. Using either POCO or HttpRequestMessage works but not both at the same time. Maybe I'm missing something obvious?

startup error: Error indexing method 'ArticleInfo.Run'. Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'req' to type HttpRequestMessage

My signature is

public static async Task Run([HttpTrigger(...)] POCO myObj, HttpRequestMessage req)

frli avatar Aug 18 '17 11:08 frli

@MikeStall can you take a look at the signature @frli shared above?

ahmelsayed avatar Aug 18 '17 15:08 ahmelsayed

With the new tooling and the configuration from attribute model, this is not currently supported. It is a gap we intend to address, but in the meantime, the only option when using the new tooling is to deserialize the object from the request yourself.

fabiocav avatar Aug 18 '17 16:08 fabiocav

@frli I am guessing that you are using the HttpRequestMessage to get headers/query parameters and/or for creating the response back? If it's headers/query params:

public static async Task<HttpResponseMessage> Run([HttpTrigger(...)]POCO req, IDictionary<string, string> headers, IDictionary<string, string> query)

(then just build a response back rather then using the helpers off HttpRequestMessage)

The HttpTrigger will bind the Header and Query dictionaries to the 2 dictionaries in the signature above, which is what we do.

If we need anything more detailed then we just use the main request object and in the function deserialize the object.

TheeJamesLee avatar Aug 18 '17 16:08 TheeJamesLee

@fri , @ahmedelnably - I filed https://github.com/Azure/azure-webjobs-sdk-extensions/issues/273 to track fixing this in VS Tooling.

MikeStall avatar Aug 18 '17 16:08 MikeStall

@TheeJamesLee I needed to get a look at the headers and that did the trick, thank you! 👏 👍 @MikeStall @fabiocav thanks for the clarification and filing the issue 🙇

Just to clarify my scenario and why I would need access to a POCO and something more raw at the same time:

I have a generic json webhook trigger and a documentdb input with document lookup via id. The document id is well known and constructed from incoming attributes in the POCO posted in the http trigger and set using runtime bindings. For various reasons I also need access to the incoming http headers. Having access to both the POCO and the HttpRequestMessage (or the headers dictionary mentioned by @TheeJamesLee ) allows me to do this in a comfortable way.

frli avatar Aug 21 '17 07:08 frli

Resolved with https://github.com/Azure/azure-webjobs-sdk-extensions/pull/284 . Should be available in the next deployment.

MikeStall avatar Sep 08 '17 00:09 MikeStall

i really need to be able to

public static async Task Run([HttpTrigger(...)] POCO myObj, HttpRequestMessage req)

because i am using durable f unction and i also have to return

return starter.CreateCheckStatusResponse(req, orchestrationId);

raasmasood avatar Dec 13 '18 06:12 raasmasood

What serializer are you using for this feature? I am running into issues with Guid properties on my POCOs. I have tried the usual Newtonsoft attributes and converters, without luck. Is there another way to customize the serialization logic?

ahaugedk avatar May 14 '19 09:05 ahaugedk