我正在尝试编写一些中间件来通过代理服务Azure Blob.正在调用处理程序,正在检索blob,但我的图像未显示.
我写了一个服务来连接存储帐户并创建一个Blob客户端.我编写了使用该服务的中间件,然后下载所请求的blob并将其写入Response.通常,我希望将blob作为字节数组或流下载并将其写入OutputStream,而这似乎不是使用.net核心中的新httpContext的选项.
我的中间件:
namespace SampleApp1.WebApp.Middleware
{
public class BlobFileViewHandler
{
public BlobFileViewHandler(RequestDelegate next)
{
}
public async Task Invoke(HttpContext httpContext, IBlobService svc)
{
string container = httpContext.Request.Query["container"];
string itemPath = httpContext.Request.Query["path"];
Blob cbb = await svc.GetBlobAsync(container, itemPath);
httpContext.Response.ContentType = cbb.ContentType;
await httpContext.Response.Body.WriteAsync(cbb.Contents, 0, cbb.Contents.Length);
}
}
// Extension method used to add the middleware to the HTTP request pipeline.
public static class BlobFileViewHandlerExtensions
{
public static IApplicationBuilder UseBlobFileViewHandler(this IApplicationBuilder builder)
{
return builder.UseMiddleware<BlobFileViewHandler>();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我使用Startup中的Map函数调用中间件,如下所示:
app.Map(new PathString("/thumbs"), …Run Code Online (Sandbox Code Playgroud)