-1

I have a requirement that I have to use TEntity with the class. But Azure function not working when using Generics with class. It's not even showing on the console also. But If I remove TEntity from class declaration it's showing in the console. So is this not supported? or is there any workaround for this? I have searched on internet but found nothing related to this.

public static class DataFilesIngestJobs<TEntity>
{
    [FunctionName("IngestDataFilesScheduled")]
    public static async Task RunScheduleAsync(
        [TimerTrigger("%DataFiles:IngestJob:ScheduleExpressionTrigger%")] TimerInfo timer,
        ExecutionContext context,
        TraceWriter log)
    {
        await RunAsync(context, log);
    }

    [FunctionName("IngestDataFilesOnDemand")]
    public static async Task RunOnDemandAsync(
        [QueueTrigger("%DataFiles:IngestJob:OnDemandQueueNameTrigger%", Connection = "ConnectionStrings:BlobStorageAccount")] string queueItem,
        ExecutionContext context,
        TraceWriter log)
    {
        await RunAsync(context, log);
    }
}

Console output on removing TEntity from class.

Found the following functions: [3/21/2018 6:52:41 AM] MyCompany.MyProject.DataFilesIngestJobs.RunScheduleAsync [3/21/2018 6:52:41 AM] MyCompany.MyProject.DataFilesIngestJobs.RunOnDemandAsync

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
  • I cannot really see that you are using the TEntity in your functions. Perhaps you have more functions in the class that do need it. But as far as I know Azure can't know what generic type you want for the class, and therefore it wont be able to call the functions in it. – abydal Mar 21 '18 at 07:14
  • @abydal I'm using the code in `RunAsync` which uses `TEntity`. – Vivek Nuna Mar 21 '18 at 07:18
  • 1
    I might missunderstand this but I think you should write the function as `RunAsync()`, not the class, and then call it from the entry functions with the generic specified like so `RunAsync()` – abydal Mar 21 '18 at 07:41
  • 2
    Which concrete `TEntity` class would you expect runtime to use? – Mikhail Shilkov Mar 21 '18 at 08:20
  • @viveknuna No runtime will run a generic function without knowing the type, simply because they can't *guess* what type to use. Neither console applications nor ASP.NET will run a function with an unknown type. They can use DI to create *concrete* types by replacing dependencies with registered types. – Panagiotis Kanavos Mar 21 '18 at 08:25

1 Answers1

6

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

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

see source code.

I guess that's because they wouldn't know which type to use as generic parameter when calling the function method.

Mikhail Shilkov
  • 34,128
  • 3
  • 68
  • 107