gpt4 book ai didi

c# - ServiceStack Proxy 特性代码优化

转载 作者:行者123 更新时间:2023-12-04 15:24:12 25 4
gpt4 key购买 nike

我的任务是为内部系统创建代理。代理需要为每个请求添加一个基本身份验证 header ,并记录它和响应。

我正在使用 ServiceStack 的 Proxy Feature 插件,但感觉代码是重复的,因为我对每个操作执行以下操作:

Plugins.Add(new ProxyFeature(matchingRequests: req => req.PathInfo.ToLower().StartsWith("/opretaftale"), resolveUrl: req => gensamOpretaftaleUrl)
{
ProxyRequestFilter = (req, httpWebRequest) =>
{
try
{
httpWebRequest.AddBasicAuth(clientId, clientPassword);
}
catch (System.Exception ex)
{
Log.Error(ex, "...");
}
},

TransformRequest = async (res, requestStream) =>
{
using (var reader = new StreamReader(requestStream, Encoding.UTF8))
{
var requestBody = await reader.ReadToEndAsync();
Log.Information("request: " + requestBody);
return MemoryStreamFactory.GetStream(requestBody.ToUtf8Bytes());
}
},

TransformResponse = async (res, responseStream) =>
{
using (var reader = new StreamReader(responseStream, Encoding.UTF8))
{
var responseBody = await reader.ReadToEndAsync();
Log.Information("response: " + responseBody);
return MemoryStreamFactory.GetStream(responseBody.ToUtf8Bytes());
}
}
});

是否可以对多个请求执行相同的操作,我真的需要 TransformRequest/TransformResponse 来记录请求/响应吗?我很难从 ProxyRequestFilter/ProxyResponseFilter 操作获取流内容...

谢谢!

最佳答案

您可以使用普通的 C# 进行重构,例如通过将内联 lambda 提取到通用方法中并改为引用它们:

private Func<IHttpRequest, Stream, Task<Stream>> TransformRequest =>
async (res, requestStream) => {
using var reader = new StreamReader(requestStream, Encoding.UTF8);
var requestBody = await reader.ReadToEndAsync();
Log.Information("request: " + requestBody);
return MemoryStreamFactory.GetStream(requestBody.ToUtf8Bytes());
};

哪里可以:

Plugins.Add(new ProxyFeature(...) {
TransformRequest = TransformRequest,
});

尽管在这种情况下您可能希望创建一个构造 PluginFeature 的工厂方法,例如:

ProxyFeature CreateProxyRule(Func<IHttpRequest, bool> matchingRequests,
Func<IHttpRequest, string> resolveUrl)
{
return new ProxyFeature(matchingRequests, resolveUrl)
{
ProxyRequestFilter = (req, httpWebRequest) => {
httpWebRequest.AddBasicAuth(clientId, clientPassword);
},
TransformRequest = async (res, requestStream) => {
using var reader = new StreamReader(requestStream, Encoding.UTF8);
var requestBody = await reader.ReadToEndAsync();
Log.Information("request: " + requestBody);
return MemoryStreamFactory.GetStream(requestBody.ToUtf8Bytes());
},
TransformResponse = async (res, responseStream) => {
using var reader = new StreamReader(responseStream, Encoding.UTF8);
var responseBody = await reader.ReadToEndAsync();
Log.Information("response: " + responseBody);
return MemoryStreamFactory.GetStream(responseBody.ToUtf8Bytes());
}
};
}

然后只需传入在不同代理之间可配置的所有部分,例如:

Plugins.Add(CreateProxyRule(
matchingRequests: req => req.PathInfo.ToLower().StartsWith("/opretaftale"),
resolveUrl: req => gensamOpretaftaleUrl));

由于 HTTP 请求通常是只进流,如果您想记录正文,您需要缓冲请求/响应,因此对于代理请求,您需要在 TransformRequest/TransformResponse 中执行此操作。

如果你只是想记录 ServiceStack 请求,你可以注册一个 Request Logger启用 EnableRequestBodyTracking,例如:

Plugins.Add(new RequestLogsFeature { EnableRequestBodyTracking = true });

哪个会 enable Request Buffering为了能够缓冲请求主体以进行日志记录和反序列化。

关于c# - ServiceStack Proxy 特性代码优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62657540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com