Azure函数-如何在函数以外的类中访问HttpRequest数据

时间:2018-08-23 18:52:31

标签: .net azure asp.net-core .net-core azure-functions

我有一个Http触发的Azure函数,并且正在使用注入自定义属性'api' IExtensionConfigProvider

 public static IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)]HttpRequest req, TraceWriter log, ExecutionContext exeContext, [Inject(typeof(Api))]Api api)

下面是配置扩展的代码

 public class InjectConfiguration : IExtensionConfigProvider
{
    private IServiceProvider _serviceProvider;

    public void Initialize(ExtensionConfigContext context)
    {
        var services = new ServiceCollection();
        RegisterServices(services);
        _serviceProvider = services.BuildServiceProvider(true);


        context
            .AddBindingRule<InjectAttribute>()
            .BindToInput<dynamic>(i => _serviceProvider.GetRequiredService(i.Type));
    }
    private void RegisterServices(IServiceCollection services)
    {
        var conifg = new ConfigurationBuilder()
                    .SetBasePath(Directory.GetCurrentDirectory())
                    .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                    .AddEnvironmentVariables()
                    .Build();

        services.AddTransient(api =>
        {
            return new Api(config["ApiUrl"]);
        });


    }
}

我想根据请求中收到的输入在return new Api(config["ApiUrl"]);中使用其他配置设置。前-逻辑上应该是-

  services.AddTransient(api =>
        {
           // If in http request query string value is A then
            return new Api(config["ApiUrl"]);
           // If the value is B then
            return new Api(config["ApiUrl_New"]);
        });

如何在其中访问HTTP请求数据?还是说我该如何在“ Api”类中访问HttpRequest数据?

0 个答案:

没有答案