Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net Blazer Web程序集应用程序无法使用阻止的CORS策略调用grpc Web调用_Asp.net_Blazor_Grpc_.net Core 3.1_Grpc Web - Fatal编程技术网

Asp.net Blazer Web程序集应用程序无法使用阻止的CORS策略调用grpc Web调用

Asp.net Blazer Web程序集应用程序无法使用阻止的CORS策略调用grpc Web调用,asp.net,blazor,grpc,.net-core-3.1,grpc-web,Asp.net,Blazor,Grpc,.net Core 3.1,Grpc Web,我正在通过grpc web服务调用测试Blazer webassmbly应用程序。但呼叫失败,并显示以下消息。有没有办法解决这个问题?这些都是我笔记本电脑的本地版本,还没有部署到任何地方 blazor.webassembly.js:1 Debugging hotkey: Shift+Alt+D (when application has focus) blazor.webassembly.js:1 blazor Loaded 6.51 MB resourcesThis application

我正在通过grpc web服务调用测试Blazer webassmbly应用程序。但呼叫失败,并显示以下消息。有没有办法解决这个问题?这些都是我笔记本电脑的本地版本,还没有部署到任何地方

 blazor.webassembly.js:1 Debugging hotkey: Shift+Alt+D (when application has focus)
blazor.webassembly.js:1 blazor Loaded 6.51 MB resourcesThis application was built with linking (tree shaking) disabled. Published applications will be significantly smaller.
dotnet.3.2.0.js:1 >>>mono_wasm_get_loaded_files
GRPCTest:1 Access to fetch at 'https://localhost:5001/greet.Greeter/SayHello' from origin 'https://localhost:44326' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
:5001/greet.Greeter/SayHello:1 Failed to load resource: net::ERR_FAILED
blazor.webassembly.js:1 crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: Status(StatusCode="Internal", Detail="Error starting gRPC call. JSException: TypeError: Failed to fetch", DebugException="WebAssembly.JSException: TypeError: Failed to fetch
        at System.Net.Http.WebAssemblyHttpHandler.doFetch (System.Threading.Tasks.TaskCompletionSource`1[TResult] tcs, System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) <0x301c200 + 0x00a30> in <filename unknown>:0 
        at System.Net.Http.WebAssemblyHttpHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) <0x2fed868 + 0x00174> in <filename unknown>:0 
        at Grpc.Net.Client.Web.GrpcWebHandler.SendAsyncCore (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x00092] in /_/src/Grpc.Net.Client.Web/GrpcWebHandler.cs:137 
        at Grpc.Net.Client.Internal.GrpcCall`2[TRequest,TResponse].RunCall (System.Net.Http.HttpRequestMessage request, System.Nullable`1[T] timeout) [0x0020c] in /_/src/Grpc.Net.Client/Internal/GrpcCall.cs:477 ")
Grpc.Core.RpcException: Status(StatusCode="Internal", Detail="Error starting gRPC call. JSException: TypeError: Failed to fetch", DebugException="WebAssembly.JSException: TypeError: Failed to fetch
  at System.Net.Http.WebAssemblyHttpHandler.doFetch (System.Threading.Tasks.TaskCompletionSource`1[TResult] tcs, System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) <0x301c200 + 0x00a30> in <filename unknown>:0 
  at System.Net.Http.WebAssemblyHttpHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) <0x2fed868 + 0x00174> in <filename unknown>:0 
  at Grpc.Net.Client.Web.GrpcWebHandler.SendAsyncCore (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x00092] in /_/src/Grpc.Net.Client.Web/GrpcWebHandler.cs:137 
  at Grpc.Net.Client.Internal.GrpcCall`2[TRequest,TResponse].RunCall (System.Net.Http.HttpRequestMessage request, System.Nullable`1[T] timeout) [0x0020c] in /_/src/Grpc.Net.Client/Internal/GrpcCall.cs:477 ")
  at GRPCBlazorApp.Pages.GRPCTest.OnInitializedAsync () [0x0005a] in C:\Users\sbaku\source\repos\GRPCBlazorApp\GRPCBlazorApp\Pages\GRPCTest.razor:16 
  at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync () <0x2c61318 + 0x0013a> in <filename unknown>:0 
  at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask (System.Threading.Tasks.Task taskToHandle) <0x2eda540 + 0x000b6> in <filename unknown>:0 
f.printErr @ blazor.webassembly.js:1
f.preRun.push.window.Blazor._internal.dotNetCriticalError @ blazor.webassembly.js:1
_mono_wasm_invoke_js_unmarshalled @ dotnet.3.2.0.js:1
do_icall @ 00755c3a:0x10f924
do_icall_wrapper @ 00755c3a:0x50b6a
interp_exec_method @ 00755c3a:0x2588e
interp_runtime_invoke @ 00755c3a:0xf7391
mono_jit_runtime_invoke @ 00755c3a:0xddb3d
do_runtime_invoke @ 00755c3a:0x3ba85
mono_runtime_try_invoke @ 00755c3a:0xcfdb
try_invoke_perform_wait_callback @ 00755c3a:0xb62e0
worker_callback @ 00755c3a:0x9a7a3
fire_tp_callback @ 00755c3a:0x105481
mono_background_exec @ 00755c3a:0xeb5ee
Module._mono_background_exec @ dotnet.3.2.0.js:1
pump_message @ dotnet.3.2.0.js:1
setTimeout (async)
_schedule_background_exec @ dotnet.3.2.0.js:1
mono_threads_schedule_background_job @ 00755c3a:0x14dbd
ves_icall_System_Threading_ThreadPool_RequestWorkerThread @ 00755c3a:0xa0025
ves_icall_System_Threading_ThreadPool_RequestWorkerThread_raw @ 00755c3a:0x9fecf
do_icall @ 00755c3a:0x10f790
do_icall_wrapper @ 00755c3a:0x50b6a
interp_exec_method @ 00755c3a:0x2588e
interp_runtime_invoke @ 00755c3a:0xf7391
mono_jit_runtime_invoke @ 00755c3a:0xddb3d
do_runtime_invoke @ 00755c3a:0x3ba85
mono_runtime_try_invoke @ 00755c3a:0xcfdb
mono_runtime_invoke @ 00755c3a:0x44b39
mono_wasm_invoke_method @ 00755c3a:0xca6a7
Module._mono_wasm_invoke_method @ dotnet.3.2.0.js:1
call_method @ dotnet.3.2.0.js:1
set_task_failure @ dotnet.3.2.0.js:1
(anonymous) @ dotnet.3.2.0.js:1
Promise.then (async)
js_to_mono_obj @ dotnet.3.2.0.js:1
_mono_wasm_invoke_js_with_args @ dotnet.3.2.0.js:1
do_icall @ 00755c3a:0x10f8b1
do_icall_wrapper @ 00755c3a:0x50b6a
interp_exec_method @ 00755c3a:0x2588e
interp_runtime_invoke @ 00755c3a:0xf7391
mono_jit_runtime_invoke @ 00755c3a:0xddb3d
do_runtime_invoke @ 00755c3a:0x3ba85
mono_runtime_invoke_checked @ 00755c3a:0x96a2
mono_runtime_try_invoke_array @ 00755c3a:0x66c26
ves_icall_InternalInvoke @ 00755c3a:0xb44ff
ves_icall_InternalInvoke_raw @ 00755c3a:0xb3ffd
do_icall @ 00755c3a:0x10f8b1
do_icall_wrapper @ 00755c3a:0x50b6a
interp_exec_method @ 00755c3a:0x2588e
interp_runtime_invoke @ 00755c3a:0xf7391
mono_jit_runtime_invoke @ 00755c3a:0xddb3d
do_runtime_invoke @ 00755c3a:0x3ba85
mono_runtime_try_invoke @ 00755c3a:0xcfdb
mono_runtime_invoke @ 00755c3a:0x44b39
mono_wasm_invoke_method @ 00755c3a:0xca6a7
Module._mono_wasm_invoke_method @ dotnet.3.2.0.js:1
call_method @ dotnet.3.2.0.js:1
(anonymous) @ dotnet.3.2.0.js:1
beginInvokeDotNetFromJS @ blazor.webassembly.js:1
s @ blazor.webassembly.js:1
e.invokeMethodAsync @ blazor.webassembly.js:1
(anonymous) @ blazor.webassembly.js:1
(anonymous) @ blazor.webassembly.js:1
(anonymous) @ blazor.webassembly.js:1
(anonymous) @ blazor.webassembly.js:1
r @ blazor.webassembly.js:1
(anonymous) @ blazor.webassembly.js:1
(anonymous) @ blazor.webassembly.js:1
(anonymous) @ blazor.webassembly.js:1
(anonymous) @ blazor.webassembly.js:1
(anonymous) @ blazor.webassembly.js:1
r @ blazor.webassembly.js:1
d @ blazor.webassembly.js:1
f @ blazor.webassembly.js:1
(anonymous) @ blazor.webassembly.js:1
(anonymous) @ blazor.webassembly.js:1
e.onGlobalEvent @ blazor.webassembly.js:1
Show 43 more frames from Library code
blazor.webassembly.js:1调试热键:Shift+Alt+D(当应用程序有焦点时)
blazor.webassembly.js:1 blazor加载了6.51 MB的资源此应用程序是在禁用链接(树抖动)的情况下构建的。已发布的应用程序将大大减少。
dotnet.3.2.0.js:1>>mono\u wasm\u获取加载的\u文件
GRPCTest:1访问“获取”的权限https://localhost:5001/greet.Greeter/SayHello“起源”https://localhost:44326'已被CORS策略阻止:对飞行前请求的响应未通过访问控制检查:请求的资源上不存在'access control Allow Origin'标头。如果不透明响应满足您的需要,请将请求的模式设置为“no cors”,以获取禁用cors的资源。
:5001/greet.Greeter/SayHello:1未能加载资源:net::ERR\u失败
blazor.webassembly.js:1 crit:Microsoft.AspNetCore.Components.webassembly.Rendering.WebAssemblyRenderer[100]
未处理的异常呈现组件:状态(StatusCode=“Internal”,Detail=“启动gRPC调用时出错。JSException:TypeError:无法获取”,DebugException=“WebAssembly.JSException:TypeError:无法获取
0中的System.Net.Http.WebAssemblyHttpHandler.doFetch(System.Threading.Tasks.TaskCompletionSource`1[TResult]tcs、System.Net.Http.HttpRequestMessage请求、System.Threading.CancellationToken CancellationToken)处
位于0中的System.Net.Http.WebAssemblyHttpHandler.SendAsync(System.Net.Http.HttpRequestMessage请求,System.Threading.CancellationToken CancellationToken)处
位于/u/src/Grpc.Net.Client.Web.GrpcWebHandler.SendAsyncCore(System.Net.Http.HttpRequestMessage请求,System.Threading.CancellationToken CancellationToken)[0x00092]in/u/src/Grpc.Net.Client.Web/GrpcWebHandler.cs:137
在Grpc.Net.Client.Internal.GrpcCall`2[TRequest,treresponse].RunCall(System.Net.Http.HttpRequestMessage请求,System.Nullable`1[T]超时)[0x0020c]in/\uu/src/Grpc.Net.Client/Internal/GrpcCall.cs:477”)
Grpc.Core.RpcException:Status(StatusCode=“Internal”,Detail=“启动Grpc调用时出错。JSException:TypeError:Failed to fetch”,DebugException=“WebAssembly.JSException:TypeError:Failed to fetch
0中的System.Net.Http.WebAssemblyHttpHandler.doFetch(System.Threading.Tasks.TaskCompletionSource`1[TResult]tcs、System.Net.Http.HttpRequestMessage请求、System.Threading.CancellationToken CancellationToken)处
位于0中的System.Net.Http.WebAssemblyHttpHandler.SendAsync(System.Net.Http.HttpRequestMessage请求,System.Threading.CancellationToken CancellationToken)处
位于/u/src/Grpc.Net.Client.Web.GrpcWebHandler.SendAsyncCore(System.Net.Http.HttpRequestMessage请求,System.Threading.CancellationToken CancellationToken)[0x00092]in/u/src/Grpc.Net.Client.Web/GrpcWebHandler.cs:137
在Grpc.Net.Client.Internal.GrpcCall`2[TRequest,treresponse].RunCall(System.Net.Http.HttpRequestMessage请求,System.Nullable`1[T]超时)[0x0020c]in/\uu/src/Grpc.Net.Client/Internal/GrpcCall.cs:477”)
在C:\Users\sbaku\source\repos\GRPCBlazorApp\GRPCBlazorApp\Pages\GRPCBlazorApp\Pages\GRPCTest.OnInitializedAsync()[0x0005a]中
位于:0中的Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()处
位于:0中的Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(System.Threading.Tasks.Task taskToHandle)
f、 printErr@blazor.webassembly.js:1
f、 preRun.push.window.Blazor._internal.dotNetCriticalError@Blazor.webassembly.js:1
_mono_wasm_invoke_js_unmarshalled@dotnet.3.2.0.js:1
do__icall@00755c3a:0x10f924
do_icall_包装@00755c3a:0x50b6a
内部执行方法@00755c3a:0x2588e
interp_runtime_invoke@00755c3a:0xf7391
mono_jit_runtime_invoke@00755c3a:0xddb3d
运行时调用@00755c3a:0x3ba85
mono\u运行时\u尝试\u调用@00755c3a:0xcfdb
尝试调用执行等待回调@00755c3a:0xb62e0
worker_回调@00755c3a:0x9a7a3
火警报警@00755c3a:0x105481
mono_background_exec@00755c3a:0xeb5ee
模块._mono_background_exec@dotnet.3.2.0.js:1
pump_message@dotnet.3.2.0.js:1
设置超时(异步)
_schedule_background_exec@dotnet.3.2.0.js:1
单线程调度后台作业@00755c3a:0x14dbd
ves_icall_System_Threading_ThreadPool_RequestWorkerThread@00755c3a:0xa0025
ves_icall_System_Threading_ThreadPool_RequestWorkerThread_raw@00755c3a:0x9fecf
do__icall@00755c3a:0x10f790
do_icall_包装@00755c3a:0x50b6a
内部执行方法@00755c3a:0x2588e
interp_runtime_invoke@00755c3a:0xf7391
mono_jit_runtime_invoke@00755c3a:0xddb3d
运行时调用@00755c3a:0x3ba85
mono\u运行时\u尝试\u调用@00755c3a:0xcfdb
mono_运行时调用@00755c3a:0x44b39
mono_wasm_invoke_方法@00755c3a:0xca6a7
Module.\u mono\u wasm\u invoke\u method@dotnet.3.2.0.js:1
调用@dotnet.3.2.0.js的方法:1
set_task_failure@dotnet.3.2.0.js:1
(匿名)@dotnet.3.2.0.js:1
Promise.then(异步)
js_to_mono_obj@dotnet.3.2.0.js:1
_mono_wasm_使用参数@dotnet.3.2.0.js调用_js_:1
do__icall@00755c3a:0x10f8b1
do_icall_包装@00755c3a:0x50b6a
内部执行方法@00755c3a:0x2588e
interp_runtime_invoke@00755c3a:0xf7391
mono_jit_runtime_invoke@00755c3a:0xddb3d
运行时调用@00755c3a:0x3ba85
在00755c3a:0x96a2处检查单声道运行时调用
mono\u运行时\u尝试\u调用\u数组@00755c3a:0x66c26
ves_icall_InternalInvoke@00755c3a:0xb44ff
ves_icall_InternalInvoke_raw@00755c3a:0xb3ffd
do__icall@00755c3a:0x10f8b1
do_icall_包装@00755c3a:0x50b6a
内部执行方法@00755c3a:0x2588e
interp_runtime_invoke@00755c3a:0xf7391
mono_jit_runtime_invoke@00755c3a:0xddb3d
运行时调用@00755c3a:0x3ba85
mono\u运行时\u尝试\u调用@00755c3a:0xcfdb
mono_运行时调用@00755c3a:0x44b39
mono_wasm_invoke_方法@00755c3a:0xca6a7
模块。\u mono\u wasm\u invoke\m
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
//using GrpcWebService.Services;

namespace GrpcWebService
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddGrpc();
            services.AddAuthorization();
            services.AddCors(options =>
            {
                options.AddPolicy("AllowAllHeaders",
                      builder =>
                      {
                          builder.AllowAnyOrigin()
                                 .AllowAnyHeader()
                                 .AllowAnyMethod();
                      });
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGrpcService<GreeterService>()
                    .RequireCors(cors => cors.AllowAnyHeader().AllowAnyMethod());
                //endpoints.MapGrpcService<WeatherForecastsService>().EnableGrpcWeb()
                //    .RequireCors(cors => cors.AllowAnyHeader().AllowAnyMethod()
                //        .WithOrigins("http://localhost:54070", "https://localhost:44316"));

                endpoints.MapGet("/", async context =>
                {
                    await context.Response.WriteAsync("Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909");
                });
            });
        }
    }
}
using System;
using System.Net.Http;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Text;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Grpc.Net.Client.Web;
using Grpc.Net.Client;

namespace GRPCBlazorApp
{
    public class Program
    {
        public static async Task Main(string[] args)
        {
            var builder = WebAssemblyHostBuilder.CreateDefault(args);
            builder.RootComponents.Add<App>("app");

            builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

            builder.Services.AddSingleton(services =>
            {
                // Get the service address from appsettings.json
                //var config = services.GetRequiredService<IConfiguration>();
                //var backendUrl = config["BackendUrl"];
                var backendUrl = "https://localhost:5001";

                // Create a channel with a GrpcWebHandler that is addressed to the backend server.
                //
                // GrpcWebText is used because server streaming requires it. If server streaming is not used in your app
                // then GrpcWeb is recommended because it produces smaller messages.
                var httpHandler = new GrpcWebHandler(GrpcWebMode.GrpcWebText, new HttpClientHandler());

                return GrpcChannel.ForAddress(backendUrl, new GrpcChannelOptions { HttpHandler = httpHandler });
            });

            await builder.Build().RunAsync();
        }
    }
}