gRPC (service) in ASP.NET Project

I want to run the gRPC service “inside” the asp.net application, the same way as I can configure signalR/WebSocket to run in process with asp.net rest api.

Reason I want to do this because I want to send signalR notifications to connected clients when I receive a gRPC request.

Here is my Startup.cs (asp.net web api + signalR + gRPC).

    public void ConfigureServices(IServiceCollection services)
    {

        services.AddControllers();

        services.AddCors(options => options.AddPolicy("AllowLocalHost", corsPolicyBuilder =>
        {
            corsPolicyBuilder
                .AllowAnyMethod()
                .AllowAnyHeader()
                .WithOrigins("http://localhost", "http://localhost:4200")
                .AllowCredentials();
        }));

        services.AddSignalR();

        services.AddGrpc();
        services.AddGrpcSwagger();

        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new OpenApiInfo { Title = "MyBackend", Version = "v1", Description = "My proof of concept", });

        });
    }

    // 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.UseSwagger();
            app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "MyBackend v1"));
        }

        app.UseCors("AllowLocalHost");

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();

            //gRPC service
            endpoints.MapGrpcService();
            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");
            });
            endpoints.MapHub("/hub");
        });
    }

note: currently using .net 8

Is this possible and how?

Read more here: Source link