我把拖车从服务器发送到客户端,但在客户端,我不能访问这些拖车。
服务器:
// UserContract.cs
public Task<User> GetUserAsync(UserDto userDto, CallContext context = default)
{
try
{
throw new NotImplementedException();
}
catch
{
Metadata metadata = new Metadata { { "test", "testvalue" } };
throw new RpcException(new Status(StatusCode.Internal, "Error"), metadata);
}
}
客户(Blazor):
try
{
await this.FactoryGrpc.CreateService<IUserContract>().GetUserAsync(userDto);
}
catch (RpcException exp)
{
if (exp.Trailers.Count == 0)
{
this.Popup.ShowMessage("Where's the trailer?");
return;
}
this.Popup.ShowMessage(exp.Trailers.GetValue("test"));
}
它正在进入if。当它应该是1的时候,拖车数是0。
发布于 2021-03-16 14:44:22
我找到了答案。首先,我们需要在服务中添加CORS策略:
https://learn.microsoft.com/pt-br/aspnet/core/grpc/browser?view=aspnetcore-5.0
然后在CORS策略上公开标题(预告片):
public void ConfigureServices(IServiceCollection services)
{
// Add CORS (Cross-Origin Resource Sharing)
services.AddCors(
options => options.AddPolicy("AllowAll", builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.WithExposedHeaders("Grpc-Status", "Grpc-Message", "Grpc-Encoding", "Grpc- Accept-Encoding", "test");
}));
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseCors();
app.UseEndpoints(
endpoints =>
{
endpoints.MapGrpcService<UsuarioContrato>().RequireCors("AllowAll");
});
}
https://stackoverflow.com/questions/66656256
复制相似问题