代码之家  ›  专栏  ›  技术社区  ›  Brian

如何访问在启动中注释的作用域服务。政务司司长,从a。CS文件不是一个文件。使用Blazor的RAZOR文件?

  •  0
  • Brian  · 技术社区  · 5 年前

    在我的Blazor应用程序中,我有一个作用域服务:

    下载文件。反恐精英

    namespace MyNamespace {
        public class DownloadingFile
        {
            public byte[] theFile { get; set; }
        }
    }
    

    我在Startup中对其进行了注释。反恐精英:

    启动。反恐精英

    services.AddScoped<DownloadingFile>();
    

    我的文件。剃须刀

    <div @onclick="MyTask">Click Me</div>
    
    @code {
        [Inject]
        private NavigationManager navigationManager { get; set; }
    
        [Inject]
        DownloadingFile downloadingFile { get; set; }
    
        private async Task MyTask(){
            APIResponse apiResponse = await APIResponse.getResponse();
            downloadingFile = apiResponse.data;
            navigationManager.NavigateTo("/api/Download/DownloadFile?filename=" + apiResponse.filename, true);
        }
    }
    

    MyNamespace
    `- Controllers
       `- DownloadController.cs
    

    下载控制器。反恐精英

    namespace MyNamespace.Controllers {
        [Route("api/[controller]")]
        [ApiController]
        public class DownloadController : ControllerBase
        {
            [HttpGet("[action]")]
            public IActionResult DownloadFile(string filename)
            {
                byte[] theByteArray = new byte[] {1, 2, 3, 4, ... };
                return File(theByteArray, "application/zip", filename);
            }
        }
    }
    

    theByteArray ,它需要是动态的。如果我更改下载控制器:

    namespace MyNamespace.Controllers {
        [Route("api/[controller]")]
        [ApiController]
        public class DownloadController : ControllerBase
        {
            // TO THIS
            private DownloadingFile downloadingFile;
            public DownloadController(DownloadingFile downloadingFile)
            {
                this.downloadingFile = downloadingFile;
            }
    
            // OR THIS
            private DownloadingFile downloadingFile;
            public DownloadController() {
                downloadingFile = new DownloadingFile();
            }
    
            [HttpGet("[action]")]
            public IActionResult DownloadFile(string filename)
            {
                return File(downloadingFile.theFile, "application/zip", filename);
            }
        }
    }
    

    DownloadingFile 总是出现 null


    编辑:


    如果我能从中得到字节数组。把剃须刀锉到地上。CS文件,一切都会好的。我该怎么做呢?

    0 回复  |  直到 5 年前
        1
  •  2
  •   MrC aka Shaun Curtis    5 年前

    让我们看看您的代码:

            APIResponse apiResponse = await APIResponse.getResponse();
            downloadingFile = apiResponse.data;
            navigationManager.NavigateTo("/api/Download/DownloadFile?filename=" + apiResponse.filename, true);
    

    你为什么要这样做 downloadingFile 到 APIResponse.data ? 这是一种注入式服务。你不能重新分配它。

    在控制器的第二个版本中

            private DownloadingFile downloadingFile;
            public DownloadController(DownloadingFile downloadingFile)
            {
                this.downloadingFile = downloadingFile;
            }
    

    DownloadingFile downloadingFile.theFile 是空的。

    您可以对trach服务实例执行以下操作:

        public class ScopedService
        {
            public Guid ID => Guid.NewGuid();
    
            public ScopedService()
            {
                Debug.WriteLine($"New ScopedService ID:{ID}");
            }
        }
    

        2
  •  0
  •   Bertramp    4 年前

    如 MrC aka Shaun Curtis :由于客户端和服务器是不同的作用域,因此您将 DownloadingFile 下载文件 你在哪里 downloadingFile = apiResponse.data 与您尝试检索该文件的方式不同,这就是文件被删除的原因 NULL .

    在我看来,您的目标是向用户提供文件下载。

    相反,您只需导航到所需文件所在的URL即可。要执行此操作,您需要将按钮交换为浏览器的标记以允许它。

    <a class="btn" href="https://myapiurl.com/filename" role="button">Click Me</a>
    
        3
  •  0
  •   Zach Ioannou    4 年前

    请尝试安装此扩展 BlazorDownloadFile

    然后注册it服务

    builder.Services.AddBlazorDownloadFile(ServiceLifetime.Scoped);
    

    如果在此阶段您已经拥有文件字节:

    APIResponse apiResponse = await APIResponse.getResponse();
    downloadingFile = apiResponse.data;
    

    @inject IBlazorDownloadFileService _blazorDownloadFile
    
    APIResponse apiResponse = await APIResponse.getResponse();
    
    _blazorDownloadFile.DownloadFile("myFile.pdf", apiResponse.data, "application/pdf");
    

    MIME types 如果你需要的话。

        4
  •  0
  •   lrpe    4 年前

    这些作用域不是共享的。 Quote from the Blazor documentation on dependency injection (粗体强调):

    剃刀 应用程序的MVC部分正常处理作用域服务,并在上重新创建服务 每个HTTP请求 在页面或视图之间导航或从页面或视图导航到组件时 . 在客户端的组件之间导航时,不会重构作用域服务,在客户端,与服务器的通信通过用户电路的信号器连接进行,而不是通过HTTP请求。

    我仍然不确定你为什么要这样做,但我认为你所要求的在ASP的环境下是不可能的。NET核心DI基础设施,除非您将服务设置为单体。

    我会按照其他海报的建议,向用户指出API,以与组件中相同的方式获取文件。如果存在安全问题,是否将其作为受保护的端点?

    推荐文章