代码之家  ›  专栏  ›  技术社区  ›  Cijun Zhu

在Azure逻辑应用程序自定义代码中使用依赖注入

  •  0
  • Cijun Zhu  · 技术社区  · 1 年前

    我通过visualstudio代码用自定义代码创建了一个azure逻辑应用程序工作区。这段代码看起来像一个.net 8隔离的azure函数。我可以在工作流操作中成功运行该功能。

    下一步是实现依赖注入。对于隔离模型中的Azure函数(这是类型),我应该在Program.cs中有一个主机配置。但我没有这个文件。我还尝试创建在流程模型中工作的Startup.cs。它也不起作用。我怀疑主机构建过程被封装在一个名为“微软”的过程中。Azure。工作流程。功能。CustomCodeNetWxWorker.exe“。因此,我无法在服务收集中注册任何内容。现在唯一注册的对象是ILoggerFactory。

    有光吗?

    任何帮助都将不胜感激。

    重现步骤:

    1. 打开Visual Studio代码。(使用Azure逻辑应用程序,安装了auzurite。)

    2. 在Azure扩展中创建Azure逻辑应用工作区。

    3. 选择具有自定义项目的逻辑应用程序,其余部分使用默认值。

    4. 此项目中只有示例函数。

      namespace My.Fun
      {
          using System;
          using System.Collections.Generic;
          using System.Threading.Tasks;
          using Microsoft.Azure.Functions.Extensions.Workflows;
          using Microsoft.Azure.Functions.Worker;
          using Microsoft.Extensions.Logging;
      
          /// <summary>
          /// Represents the function flow invoked function.
          /// </summary>
          public class function
          {
              private readonly ILogger<function> logger;
      
              public function(ILoggerFactory loggerFactory)
              {
                  logger = loggerFactory.CreateLogger<function>();
              }
      
              /// <summary>
              /// Executes the logic app workflow.
              /// </summary>
              /// <param name="zipCode">The zip code.</param>
              /// <param name="temperatureScale">The temperature scale (e.g., Celsius or Fahrenheit).</param>
              [Function("function")]
              public Task<Weather> Run([WorkflowActionTrigger] int zipCode, string temperatureScale)
              {
                  this.logger.LogInformation("Starting function with Zip Code: " + zipCode + " and Scale: " + temperatureScale);
      
                  // Generate random temperature within a range based on the temperature scale
                  Random rnd = new Random();
                  var currentTemp = temperatureScale == "Celsius" ? rnd.Next(1, 30) : rnd.Next(40, 90);
                  var lowTemp = currentTemp - 10;
                  var highTemp = currentTemp + 10;
      
                  // Create a Weather object with the temperature information
                  var weather = new Weather()
                  {
                      ZipCode = zipCode,
                      CurrentWeather = $"The current weather is {currentTemp} {temperatureScale}",
                      DayLow = $"The low for the day is {lowTemp} {temperatureScale}",
                      DayHigh = $"The high for the day is {highTemp} {temperatureScale}"
                  };
      
                  return Task.FromResult(weather);
              }
      
    5. 正如所观察到的,ILogger已注册并准备好使用。但是没有Startup.cs或Program.cs来更新函数宿主。

    0 回复  |  直到 1 年前