我在网上找不到任何对此有帮助的信息。
我正在编写一个 REST API,我想记录请求正文的大小(以字节为单位)以获取指标。Go net/http API 不直接提供这一点。http.Request 确实有 Content-Length 字段,但该字段可以为空,否则客户端可能会发送错误数据。
有没有办法在中间件级别实现这一点?暴力方法是读取全文并检查大小。但如果我在中间件中这样做,处理程序将无法访问主体,因为它会被读取并关闭。
假设我有班级
public class Entity : IEntity {
public Entity(IDependency dep, string url)
{
//...
}
}
public class Dependency : IDependency {
//...
}
Run Code Online (Sandbox Code Playgroud)
现在当我想使用依赖注入时,我可以做类似的事情:
IServiceCollection serviceCollection = new ServiceCollection();
serviceCollection.AddScoped<IDependency, Dependency>();
serviceCollection.AddScoped<IEntity, Entity>(); // how to inject the url
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,问题在于我不知道如何注入url值(例如" https://google.com/ ").我知道结构图提供了命名参数及其值.
有没有办法Entity在使用DI作为依赖项时将字符串注入构造函数?