簡體   English   中英

Azure 網絡作業使用 .NET 核心 3.1

[英]Azure webjob using .NET core 3.1

我正在使用 .net 核心 3.1 編寫 azure 網絡作業,我收到以下運行時異常: InvalidOperationException: %EventHubName% does not resolve to a value。

我的觸發器如下所示: ProcessEvent([EventHubTrigger("%EventHubName%", ConsumerGroup = "%ConsumerGroupName%")] EventData eventData)

我已經在 program.cs 中注冊了配置

我添加了 appSettings.environment.json 文件,其中包含以下內容:

"EventHubConfig": { "EventHubConnectionString": "..", "EventHubName": "..", "EventProcessorHostName": "..", "ConsumerGroupName": "..", "StorageConnectionString": "..", "StorageContainerName": ".." },

誰能建議我可能錯過的東西?

使用%%XXX%%是讀取設置的正確方法。

當您在本地開發應用程序時,請在local.settings.json文件中搜索這些變量。

例子:

{
    "EventHubName": "myeventhubname"
}

如果您部署在 Azure 上,您需要將變量添加到應用程序設置閱讀更多

更新 0515:

解決方法:

appsettings.json and appsettings.dev.json (Remember right click these files -> select properties -> set "Copy to Output Directory" as "copy if newer") files into your project. 2 個 json 文件應該具有相同的結構(鍵),但值可以不同。 如下所示:

我的 appsettings.json 文件:

{
  "AzureWebJobsStorage": "xxxxx",
  "EventHubConnectionString": "xxxx",
  "EventHubName": "yyeventhub1",
  "ConsumerGroupName": "cg1"
}

我的 appsettings.dev.json 文件:

   {
      "AzureWebJobsStorage": "xxxxx",
      "EventHubConnectionString": "xxxx",
      "EventHubName": "yyeventhub2",
      "ConsumerGroupName": "hub2cg"
    }

2.在CustomNameResolver class 中,使用以下代碼:

public class CustomNameResolver : INameResolver
{
    public string Resolve(string name)
    {
        var config = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
        .AddJsonFile("appsettings.dev.json", optional: true, reloadOnChange: true)
        .AddEnvironmentVariables()
        .Build();

        return config[name];
    }
}

note , appsettings.json should be added before appsettings.dev.json, so the settings in appsettings.dev.json has high priority which will overwrite the same keys in appsettings.json.

3.然后運行項目並將事件發送到appsettings.dev.json中定義的yyeventhub2,然后你可以發現webjob被觸發了。


原答案:

在 function 中使用%%時有一些限制。

根據我的測試,這里有一些注意事項:

1.一些最新的 nuget 包無法使用 eventthub 觸發器,您應該使用以下版本的 nuget 包:

<ItemGroup>
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="3.0.0" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.EventHubs" Version="4.1.1" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="3.0.10" />
    <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.4" />
    <PackageReference Include="System.Configuration.ConfigurationManager" Version="4.7.0" />
  </ItemGroup>

2.在Functions.cs中不能以%EventHubConnectionString%這種格式使用eventthub命名空間連接字符串。

3.不要使用appsettings.json中的嵌套設置。

4.不要在appsettings.json中指定EventProcessorHostNameStorageContainerName webjobs SDK 會自動為您設置它們。

5.使用%%格式時,應使用自定義綁定表達式來解析名稱。

以下是我的代碼和appsettings.json:

appsettings.json (也請右鍵單擊此文件 -> select 屬性 -> 將“復制到 Output 目錄”設置為“如果更新則復制”)

{     
    "AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=xx;AccountKey=xx;BlobEndpoint=https://xxx.blob.core.windows.net/;TableEndpoint=https://xxx.table.core.windows.net/;QueueEndpoint=https://xxx.queue.core.windows.net/;FileEndpoint=https://xxx.file.core.windows.net/",
    "EventHubConnectionString": "Endpoint=sb://xxx.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=xxx",
    "EventHubName": "xxx",
    "ConsumerGroupName": "xxx"  
}

我的 NameResolver 類(創建一個名為 CustomNameResolver.cs 的自定義 class)

public class CustomNameResolver : INameResolver
{
    public string Resolve(string name)
    {
        var config = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
        //.AddJsonFile($"appsettings.{environment}.json", optional: true, reloadOnChange: true)
        .AddEnvironmentVariables()
        .Build();

        return config[name];
    }
}

程序.cs:

class Program
{     

    static void Main(string[] args)
    {
        var builder = new HostBuilder();
        var resolver = new CustomNameResolver();

        builder.ConfigureWebJobs(b =>
        {
            b.AddAzureStorageCoreServices();
            b.AddAzureStorage();
            b.AddEventHubs();
        });
        builder.ConfigureLogging((context, b) =>
        {
            b.AddConsole();
        });          

        builder.ConfigureServices(s => s.AddSingleton<INameResolver>(resolver));

        var host = builder.Build();
        using (host)
        {
            host.Run();
        }
    }
}

函數.cs:

public class Functions
{
    public static void ProcessEvent([EventHubTrigger("%EventHubName%", Connection = "EventHubConnectionString",ConsumerGroup = "%ConsumerGroupName%")] EventData eventData, ILogger logger)
    {
        logger.LogInformation("it is triggered!" + DateTime.Now.ToLongTimeString());
    }
}

測試結果:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM