[英]Azure Functions Runtime v3 Middleware
有沒有辦法在 azure 中間件中訪問請求和響應 object。
使用日志記錄中間件的教程我已經做到了這一點:
public class ExceptionLoggingMiddleware : IFunctionsWorkerMiddleware
{
public async Task Invoke(FunctionContext context, FunctionExecutionDelegate next)
{
try
{
// Code before function execution here
await next(context);
// Code after function execution here
}
catch (Exception ex)
{
var log = context.GetLogger<ExceptionLoggingMiddleware>();
log.LogWarning(ex, string.Empty);
}
}
}
但我也想訪問響應和請求 object。 像狀態碼、正文參數、查詢參數等。這可能嗎?
雖然沒有直接的方法可以做到這一點,但有一個訪問HttpRequestData
的解決方法(不是最好的解決方案,但在修復之前它應該可以工作。 ):
public static class FunctionContextExtensions
{
public static HttpRequestData GetHttpRequestData(this FunctionContext functionContext)
{
try
{
KeyValuePair<Type, object> keyValuePair = functionContext.Features.SingleOrDefault(f => f.Key.Name == "IFunctionBindingsFeature");
object functionBindingsFeature = keyValuePair.Value;
Type type = functionBindingsFeature.GetType();
var inputData = type.GetProperties().Single(p => p.Name == "InputData").GetValue(functionBindingsFeature) as IReadOnlyDictionary<string, object>;
return inputData?.Values.SingleOrDefault(o => o is HttpRequestData) as HttpRequestData;
}
catch
{
return null;
}
}
}
你可以這樣使用它:
public class CustomMiddleware : IFunctionsWorkerMiddleware
{
public async Task Invoke(FunctionContext context, FunctionExecutionDelegate next)
{
HttpRequestData httpRequestData = context.GetHttpRequestData();
// do something with httpRequestData
await next(context);
}
}
查看此以了解更多詳細信息。
對於Http Response
,AFAIK 沒有解決方法。 此外,請查看GH Issue#530 ,其中表示將很快添加相關文檔。 此功能看起來很受歡迎,預計很快就會得到修復(在撰寫本文時)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.