FunctionsStartup vs IWebJobsStartup,在请求上读取HttpHeaders时出现问题 [英] FunctionsStartup vs IWebJobsStartup, problems reading HttpHeaders on the request

查看:125
本文介绍了FunctionsStartup vs IWebJobsStartup,在请求上读取HttpHeaders时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在本机上构建.NET Core 3.1 Azure Functions应用程序,并试图配置启动类.当我实现从 FunctionsStartup 继承的类时,无法将.net core 3.1类库导入到我的项目中.如果这样做并尝试运行该应用程序,则会在执行窗口中出现以下错误:

I am building a .NET Core 3.1 Azure Functions application on my local and am trying to configure a startup class. When I implement a class to inherrit from FunctionsStartup I cannot import a .net core 3.1 class library into my project. If I do so and try to run the app, I get the following error in the execution window:

Microsoft.Azure.Functions.Extensions:找不到方法:Microsoft.Extensions.Configuration.IConfigurationBuilder Microsoft.Azure.WebJobs.Hosting.IWebJobsConfigurationBuilder.get_ConfigurationBuilder()".值不能为空.(参数提供者")

当我将启动基类切换到 IWebJobsStartup 时,应用程序启动正常.当我发出请求并尝试单步执行代码时,我遇到了问题.我可以单步执行代码的第一部分(因此我知道我的请求已成功接收),但是我无法单步执行我的功能之一.我得到一个下载提示,并且在VS工作区中打开一个页面,该页面中出现错误消息 TaskMethodInvker.cs ,标记行为.您需要找到TaskMethodInvoker.cs才能查看源代码当前调用堆栈框架.下面的代码显示了我的功能:

When I switch the Startup base class to IWebJobsStartup the app starts fine. When I make a request and try stepping through the code I run into a problem. I can step through an initial portion of code (so I know my request is successfully received) but I am not able to step into one of my functions. I get a download prompt and a page opens up in the VS work area that has the error message TaskMethodInvker.cs not found with the tag line You need to find TaskMethodInvoker.cs to view the source for the current call stack frame. The below code shows my function:

[FunctionName("HttpAuth")]
public async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
    ExecutionContext context,
    ILogger log)
{
    GetAzureADConfiguration(context);
    var jwt = GetJwtFromHeader(req);
}

private JwtSecurityToken GetSecurityToken(string encodedToken)
{
    ...
}

private string GetJwtFromHeader(HttpRequest req)
{
    var authorizationHeader = req.Headers?["Authorization"];
    string[] parts = authorizationHeader?.ToString().Split(null) ?? new string[0];
    return (parts.Length == 2 && parts[0].Equals("Bearer")) ? parts[1] : string.Empty;
}

正在执行 GetAzureADConfiguration 函数,而未执行 GetJwtFromHeader .似乎任何试图通过代码访问Header词典的函数都无法进入并导致我的错误.但是,如果我在监视窗口中检查对象,则会看到正确的值.

The GetAzureADConfiguration function is executing, GetJwtFromHeader is not. It seems that any function trying to access the Header dictionary via code can not be stepped into and causes my error. However, if I inspect the object in the watch window, I see the correct value.

这是我的WebJobStartUp代码:

Here is my WebJobStartUp code:

[assembly: WebJobsStartup(typeof(StartUp))]
namespace CriticalPath.Api.Functions
{
    class StartUp : IWebJobsStartup 
    {
        public void Configure(IWebJobsBuilder builder) 
        {
            ...
        }
    }
}

这是我使用FunctionsStartup尝试的方法

Here is what I tried with FunctionsStartup

[assembly: FunctionsStartup(typeof(Startup))]
namespace CriticalPath.Api.Functions
{
    class StartUp : FunctionsStartup
    {
        public void Configure(IWebJobsBuilder builder)
        {
            ...
        }
    }
}

为什么我不能逐步浏览引用请求标头的代码? FunctionsStartup IWebJobsStartup 之间有什么区别.如果要使用 IWebJobsStartup ,如何访问标头?

Why can't I step through my code that references the request's headers? What is the difference between FunctionsStartup and IWebJobsStartup. How do I access the headers if I want to use IWebJobsStartup?

推荐答案

我能够解决此问题.这是个吹毛求疵的问题:我的问题主要与尝试导入类库有关,而不是使用了与我的Azure Functions应用程序冲突的依赖项.我是怎么发现的:我创建了一个新的Azure Functions v3项目和.NET Core 3.1库.经过一些试验后,我发现从Startup类的FunctionsStartup(不是WebJobStartup)继承时,我能够成功导入一个空项目.在我的函数中,我添加了访问req.Headers词典中的条目的代码,因此我可以看到运行该函数失败的时间.当一个空项目正常工作后,我查看了一个无法正确导入的项目,并在非工作项目中一个接一个地添加了对我的空项目的引用.通过每个参考,我测试了我的Azure函数,以了解破坏它的原因.当我进入Microsoft.Extensions.Configuration v5.0时,我的功能坏了.我将此依赖关系降级到v3.1.13,该功能正常运行.因此,在无法正常工作的项目中,我使用配置"来秘密访问变量,因此版本一定不匹配.解决这个问题.然后,我想创建一个自定义属性.我切换到使用WebJobStartup,并且Web触发器可以正常工作.

I was able to fix the problem. Here is the blow-by-blow: My problem revolved around trying to import a class library thant used a dependency that conflicted with my Azure Functions app. How I found this: I created a fresh Azure Functions v3 project and .NET Core 3.1 library. After experimenting with it a bit I found that while inherriting from FunctionsStartup (not WebJobStartup) in my Startup class, I was able to successfully import an empty project. In my function, I added code that accessed an entry from the req.Headers dictionary so I can see when running the function failed. Once I got an empty project working, I looked at a project that wasn't importing correctly and one by one added references in the non-working project to my empty project. With each reference I tested my Azure Function to see what breaks it. When I got to Microsoft.Extensions.Configuration v5.0, my function broke. I downgraded this dependency to v3.1.13 and the function worked. So in the project that didn't work I was using Configuration to access variables in my secrets there must have been a mismatch in versions. Fixing this worked. I then wanted to create a custom attribute. I switched over to using WebJobStartup and it the web trigger worked.

这篇关于FunctionsStartup vs IWebJobsStartup,在请求上读取HttpHeaders时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆