Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gRPC and usage of dependency injection with Simpleinjector #709

Open
MyPierre opened this issue May 12, 2019 · 4 comments

Comments

Projects
None yet
2 participants
@MyPierre
Copy link

commented May 12, 2019

Hi,
I have some difficulties to see how to configure a container for the gRPC framework.
Please could advise is there anyway/example to implement it ?
Thank you for your help.

@MyPierre MyPierre added the question label May 12, 2019

@dotnetjunkie

This comment has been minimized.

Copy link
Collaborator

commented May 12, 2019

I'm unfamiliar with the gRPC framework. Can you show me what you tried and what examples you found for other DI Containers?

@MyPierre

This comment has been minimized.

Copy link
Author

commented May 12, 2019

I found a discussion related to DI and gRPC proposing an idea here :
grpc/grpc#12478
the example on which it is based to develop a solution idea comes from the grpc documentation: https://grpc.io/docs/quickstart/csharp/
I don't see what is a correct way to do it using simplinjector.
If you have an idea, it will be valuable to me.
Thank you for your help.

@dotnetjunkie

This comment has been minimized.

Copy link
Collaborator

commented May 13, 2019

The solution with Simple Injector would be similar to the example given here. In other words, you turn your gRPC class into a Humble Object:

class GreeterImpl : Greeter.GreeterBase
{
    private Container container;

    public GreeterImpl(Container container) => this.container = container;

    public override async Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
    {
        using (AsyncScopedLifestyle.BeginScope(this.container))
        {
            // NOTE: Moved all logic to the GreeterService implementation
            var service = container.GetInstance<GreeterService>();
            return await service.SayHello();
        }
    }
}
Server server = new Server
{
    Services = { Greeter.BindService(new GreeterImpl(container)) },
}
@MyPierre

This comment has been minimized.

Copy link
Author

commented May 14, 2019

Thank you dotnetjunkie for this enlightening solution but also the fast response time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.