You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将Blazor Server PWA注册为文件处理器时遇反序列化异常

Blazor Server PWA处理本地TXT文件时抛出Deserialization of interface types is not supported异常

我尝试将Blazor Server PWA注册为TXT文件编辑器,目前已实现Windows资源管理器右键选择该PWA打开文件,PWA能正常跳转至对应页面,但会抛出异常:Deserialization of interface types is not supported。

相关配置与代码

Manifest文件的file_handlers配置

"file_handlers": [
    {
      "action": "/test",
      "accept": {
        "text/*": [ ".txt" ]
      }
    }
  ]

处理页面代码

@page "/test"
@using KristofferStrube.Blazor.FileSystem;
@using Thinktecture.Blazor.FileHandling
@inject FileHandlingService _fileHandlingService


@code {
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            await _fileHandlingService.SetConsumerAsync(async (launchParams) =>
            {
                foreach (var fileSystemHandle in launchParams.Files)
                {
                    if (fileSystemHandle is FileSystemFileHandle fileSystemFileHandle)
                    {
                        var file = await fileSystemFileHandle.GetFileAsync();
                        var text = await file.TextAsync();
                        Console.WriteLine(text);
                    }
                }
            });
        }
    }
}

问题复现步骤

  • 创建新的Blazor Server项目,命名为TestFileHandling
  • 安装NuGet包Thinktecture.Blazor.FileHandling
  • 在Program.cs的builder.Build()之前注册服务:
    builder.Services.AddFileHandlingService(); 
    
  • 复制示例项目中的service-worker.js和service-worker.published.js到项目的wwwroot文件夹
  • 添加512x512px的图标到wwwroot文件夹,命名为icon-512.png
  • 在wwwroot文件夹添加manifest.json文件,内容如下:
    {
      "name": "TestFileHandling",
      "short_name": "Test",
      "start_url": "./",
      "display": "standalone",
      "background_color": "#ffffff",
      "theme_color": "#03173d",
      "prefer_related_applications": false,
      "icons": [
        {
          "src": "icon-512.png",
          "type": "image/png",
          "sizes": "512x512"
        }
      ],
      "file_handlers": [{
        "action": "./test",
        "accept": {
          "text/plain": [".txt"]
        }
      }]
    }
    
  • 在_Host.cshtml的head标签内插入:
    <link rel="manifest" href="/manifest.json">
    
  • 在_Host.cshtml中blazor.server.js下方插入:
    <script type="text/javascript">
        navigator.serviceWorker.register('service-worker.js');
    </script>
    
  • 在Pages文件夹创建TestPage.razor,内容如下:
    @page "/test"
    @using KristofferStrube.Blazor.FileSystem;
    @using Thinktecture.Blazor.FileHandling
    @inject FileHandlingService _fileHandlingService
    
    <p>Launch Queue is supported? @(_isFileHandlingSupported ? "yes" : "no")</p>
    
    <p>Open a TXT file by right-clicking it in Explorer/Finder and choosing this demo application.</p>
    
    <textarea @bind="_text" readonly></textarea>
    
    @code {
        private bool _isFileHandlingSupported;
        private string _text = "";
    
        protected override async Task OnAfterRenderAsync(bool firstRender)
        {
            if (firstRender)
            {
                _isFileHandlingSupported = await _fileHandlingService.IsSupportedAsync();
                if (_isFileHandlingSupported)
                {
                    await _fileHandlingService.SetConsumerAsync(async (p) =>
                    {
                        foreach (var fileSystemHandle in p.Files)
                        {
                            if (fileSystemHandle is FileSystemFileHandle fileSystemFileHandle)
                            {
                                var file = await fileSystemFileHandle.GetFileAsync();
                                var text = await file.TextAsync();
                                _text = text;
                                StateHasChanged();
                            }
                        }
                    });
                }
            }
            base.OnAfterRender(firstRender);
        }
    }
    

内容的提问来源于stack exchange,提问作者Matias Masso

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 15:44:50