简体   繁体   English

无法在 Azure Function App 启动中 DI 自定义对象

[英]Unable to DI custom object in Azure Function App Startup

local.settings.json本地设置.json

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "tester": "e=e",
    "ProjectConfiguration": "{\"tester\": \"tes22\"}"
  }
}

Startup.cs启动文件

    class Startup : FunctionsStartup
    {

        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddOptions<ProjectConfiguration>()
                .Configure<IConfiguration>((option, configuration) =>
                {
                    System.Diagnostics.Debug.WriteLine($"hello:{configuration.GetSection("ProjectConfiguration").Value}");
                    configuration.GetSection("ProjectConfiguration").Bind(option);
                });
        }
    }

I am able to print out hello:{"tester": "tes22"} but when I try to access it in another class, it is null.我能够打印出hello:{"tester": "tes22"}但是当我尝试在另一个类中访问它时,它为空。

    public class InitFunction
    {
        private readonly ProjectConfiguration _projectConfiguration;

        public InitFunction(Microsoft.Extensions.Options.IOptions<ProjectConfiguration> options)
        {
            _projectConfiguration = options.Value;
            System.Diagnostics.Debug.WriteLine(JsonConvert.SerializeObject(options.Value));
        }

option.value printed out is {"tester":null} option.value 打印出来的是{"tester":null}

It seems like .bind is not binding the object properly.似乎.bind没有正确绑定对象。 How can I do this?我怎样才能做到这一点? Thank you!谢谢!

You need to provide the ProjectConfiguration properties as : delimited keys.您需要将ProjectConfiguration属性提供为:分隔键。

Assuming you have a configuration class like this:假设你有一个这样的配置类:

class ProjectConfiguration {
    public string Title { get; set; }
    public int ANumber { get; set; }
}

Its JSON equivalent would be:它的 JSON 等效项是:

{
  "IsEncrypted": false,
  "Values": {
    // ...
    "ProjectConfiguration:Title": "My App",
    "ProjectConfiguration:ANumber": 123
  }
}

References:参考:

https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection#working-with-options-and-settings https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection#working-with-options-and-settings

Apart from @abdusco's answer, if you do not want : -delimited keys, you could bind your ProjectConfiguration directly to the "Values" section of local.settings.config除了@abdusco 的回答,如果您不想要: -delimited 键,您可以将ProjectConfiguration直接绑定到local.settings.config"Values"部分

{
    "IsEncrypted": false,
    "Values": {
        // ...
        "Tester": "tes22"
    }
}
public class ProjectConfiguration
{
    public string Tester { get; set; }
}
builder.Services
  .AddOptions<ProjectConfiguration>()
  .Configure<IConfiguration>((pc, config) => config.Bind(pc));

Theoretically the Bind method also has an overload to specify a key, presumably to use custom sections in the config but I was not able to get it to work.理论上Bind方法也有一个重载来指定一个键,大概是在配置中使用自定义部分,但我无法让它工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM