0

I am looking for ideal approach for having an azure function to handle generic type input parameter. For ex:

    [FunctionName(nameof(SendToQueueActivityFunction))]
    public async Task SendToQueueActivity<T>([ActivityTrigger] SendToQueueRequest<T> sendToQueueRequest)

Basically, "SendToQueueActivity" should be able to send any object of type T.

The above code doesnt work. If function binding doesnt support generics, what should be the right approach to have function behave differently based on input types. I am looking for a cleaner way of implementation than something like using "dynamic" type.

Thanks for your time and inputs.

  • Based on this GitHub issue, generics are not supported and MSFT has no plans to add support. https://github.com/Azure/Azure-Functions/issues/735 – Bryan Lewis Dec 02 '20 at 01:40
  • Thanks Bryan. I sort of knew thats the case - I was actually looking for an alternative approach to achieve the same results. Thank you – code_explorer Dec 02 '20 at 02:44
  • I can't imagine how much effort it would take to make this work with POCOs. I mean how is the model binder suppose to tell what T is? What if `T : U` and both are classes that can be `new()`'d (as apposed to abstract classes) then which does it create? – Erik Philips Nov 11 '21 at 04:17

1 Answers1

0

As we can see from the Link Bryan provide, generic type is not support in Azure Function.

WebJobs SDK explicitly discards generic types when searching for FunctionName attribute:

return type.IsClass
   && (!type.IsAbstract || type.IsSealed)
   && type.IsPublic
   && !type.ContainsGenericParameters;

Refer to source code.

Reference:

  1. https://github.com/Azure/Azure-Functions/issues/735
  2. Why Azure function not working with Generics?
Doris Lv
  • 3,083
  • 1
  • 5
  • 14
  • Thanks @Doris. I am also looking for a better alternative approach to achieve the same. Thank you! – code_explorer Dec 02 '20 at 03:29
  • You're welcome @code_explorer ,would you mind accept my answer for others to refer? – Doris Lv Dec 02 '20 at 03:59
  • I knew the problem already, but was really looking for an alternative approach how to go around this problem. Any suggestions welcome please. Thanks. – code_explorer Dec 02 '20 at 04:59
  • 1
    Thanks. Could you please elaborate a bit. If you look at the example above, "SendToQueueRequest" is a generic class, which obviously did not work. – code_explorer Dec 02 '20 at 11:42
  • @code_explorer `SendToQueueRequest` is not a generic class, it's a generic method. Significant difference. The #2 example's method does not have a generic parameter because it's defined at the Class level, where-as the only code you've provided is a method that is generic. – Erik Philips Nov 11 '21 at 04:20