【问题标题】:How to inject dependencies in Blazor Web Assembly using Grpc and protobuf-net如何使用 Grpc 和 protobuf-net 在 Blazor Web Assembly 中注入依赖项
【发布时间】:2020-07-02 23:51:05
【问题描述】:

我正在使用 protobuf-net 使用 Grpc 构建 Web Assembly Blazor 来处理服务。我正在尝试以这种方式注入我想要的服务:

builder.Services.AddSingleton(typeof(ICustomerService), services =>
        {
            // Create a gRPC-Web channel pointing to the backend server
            var httpClient = new HttpClient(new GrpcWebHandler(GrpcWebMode.GrpcWeb, new HttpClientHandler()));
            var channel = Grpc.Net.Client.GrpcChannel.ForAddress("https://localhost:5001", new GrpcChannelOptions { HttpClient = httpClient });

            // Now we can instantiate gRPC clients for this channel
            return channel.CreateGrpcService<ICustomerService>();
        });

然后,我将我认为应该是依赖项的内容注入到 razor 组件中:

[Inject] ICustomerService Client { get; set; }

但我收到此错误:

Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] 未处理的异常呈现组件:无法为类型“Customer_Create”的属性“Client”提供值。没有“ICustomerService”类型的注册服务。

非常感谢这里的任何帮助!

【问题讨论】:

    标签: protobuf-net blazor-client-side grpc-web blazor-webassembly


    【解决方案1】:

    我没有在 blazor 中专门尝试过这个,但总的来说:你正在寻找 Client Factory 支持,它可以工作 exactly like documented here 除非你使用 AddCodeFirstGrpcClient 方法注册:

    services.AddCodeFirstGrpcClient<IMyService>(o =>
    {
        o.Address = new Uri("...etc...");
    });
    

    【讨论】:

      猜你喜欢
      • 2018-07-22
      • 2021-01-26
      • 2023-01-04
      • 2020-10-25
      • 2020-07-23
      • 1970-01-01
      • 2020-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多