【问题标题】:Why Azure function not working with Generics?为什么 Azure 函数不适用于泛型?
【发布时间】:2018-08-30 04:52:19
【问题描述】:

我有一个要求,我必须在课堂上使用TEntity。但是在将泛型与类一起使用时,Azure 功能不起作用。它甚至没有显示在控制台上。但是如果我从类声明中删除TEntity,它会显示在控制台中。所以这不支持吗?或者有什么解决方法吗?我在互联网上搜索过,但没有找到与此相关的内容。

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);
    }
}

从类中删除 TEntity 时的控制台输出。

找到以下函数:[3/21/2018 6:52:41 AM] MyCompany.MyProject.DataFilesIngestJobs.RunScheduleAsync [3/21/2018 上午 6 点 52 分 41 秒] MyCompany.MyProject.DataFilesIngestJobs.RunOnDemandAsync

【问题讨论】:

  • 我看不出您在函数中使用了 TEntity。也许你在类中有更多需要它的函数。但据我所知,Azure 无法知道您想要该类的泛型类型,因此它无法调用其中的函数。
  • @abydal 我正在使用RunAsync 中的代码,它使用TEntity
  • 我可能会误解这一点,但我认为您应该将函数编写为RunAsync&lt;TEntity&gt;(),而不是类,然后使用指定的泛型从入口函数中调用它,如RunAsync&lt;string&gt;()
  • 您希望运行时使用哪个具体的TEntity 类?
  • @viveknuna 没有运行时会在不知道类型的情况下运行泛型函数,因为他们无法猜测使用什么类型。控制台应用程序和 ASP.NET 都不会运行未知类型的函数。他们可以使用 DI 通过将依赖项替换为已注册的类型来创建 具体 类型。

标签: c# azure generics azure-storage azure-functions


【解决方案1】:

WebJobs SDK 在搜索FunctionName 属性时显式丢弃泛型:

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

source code

我猜那是因为他们在调用函数方法时不知道使用哪种类型作为泛型参数。

【讨论】:

    猜你喜欢
    • 2012-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-05
    相关资源
    最近更新 更多