Skip to content

MapGrpcService non static alternative #1628

@jakubbukaj

Description

@jakubbukaj

Hello,
I am migrating server from Grpc.Core to Grpc.AspNetCore.Server.
In Grpc.Core I was using:

Grpc.Core.Server server = new Grpc.Core.Server()
{
   Services = { _instanceOfMyServiceImplementation.GetServiceDefinition() },
   Ports =
   {
        new ServerPort("0.0.0.0", 5555, Grpc.Core.ServerCredentials.Insecure)
   }
};

In my service was following registration method and handler as:

public ServerServiceDefinition GetServiceDefinition()
        {
            return ServerServiceDefinition.CreateBuilder()
                .AddMethod(_duplexCommunicationMethod, DuplexCommunication).Build();
        }

for binding incommning calls to specific nonstatic processing method and handler.

I am now migrating to Grpc.AspNetCore.Server. I would like to avoid static classes, methods if possible. I was able to create my service as singleton. But I am not able to register processing _duplexCommunicationMethod method and handler from my instanced service implementation. I would need endpoints.MapGrpcService alternative mapping from specific instance not from Type and using static methods. Something like this:

var instanceOfMyServiceImplementation = <Got from different DI container>
var builder = Host.CreateDefaultBuilder().ConfigureWebHostDefaults(webBuilder =>
{
    webBuilder.UseKestrel();
    webBuilder.ConfigureServices(services =>
    {
        services.AddSingleton(communicationService);
        services.AddGrpc();
    });

    webBuilder.Configure((context, app) =>
    {
        app.UseRouting();
        app.UseEndpoints(endpoints =>
        {
            endpoints.BindGrpcService(instanceOfMyServiceImplementation.BindService());
        });
    });
});

with BindService like this:

public ServiceBinderBase BindService ()
{
    ServiceBinderBase sbb = new ServiceBinderBase();
    sbb.AddMethod(_duplexCommunicationMethod, DuplexCommunication);
    return sbb;
}

Is it possible? Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions