FunctionsStartup vs IWebJobsStartup, problems reading HttpHeaders on the request(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()'.值不能为空.(参数'提供者')
当我将 Startup 基类切换到 IWebJobsStartup
时,应用程序启动正常.当我提出请求并尝试单步执行代码时,我遇到了问题.我可以单步执行代码的初始部分(因此我知道我的请求已成功接收),但我无法单步执行我的某个函数.我收到下载提示,并且在 VS 工作区中打开一个页面,其中显示错误消息 TaskMethodInvker.cs not found
带有标记行 You need to find 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 并且该功能有效.因此,在不起作用的项目中,我使用 Configuration 来访问我的秘密中的变量,版本肯定不匹配.解决这个问题.然后我想创建一个自定义属性.我切换到使用 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 时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:FunctionsStartup vs IWebJobsStartup,在请求中读取 HttpHeaders 时出现问题
基础教程推荐
- rabbitmq 的 REST API 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30