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

Use struct as parameters and return value. #27

Open
Ilchert opened this issue Oct 16, 2019 · 8 comments
Open

Use struct as parameters and return value. #27

Ilchert opened this issue Oct 16, 2019 · 8 comments

Comments

@Ilchert
Copy link

Ilchert commented Oct 16, 2019

I am trying to create simple method with int parameter and it fails with System.Security.VerificationException: 'Method ProtoBuf.Grpc.Configuration.ServerBinder.AddMethod: type argument 'System.Int32' violates the constraint of type parameter 'TRequest'.' is it expected behavior? Should I wrap it to some request class? Are there any limitations to use struct as parameters? Will it be allowed in the future?

@mgravell
Copy link
Member

Yes, I'm aware of the struct limitation, and yes the library should hide it from you automatically. So: yes, it is a bug and yes, I'll fix it.

This is a requirement in the underlying gRPC API, but there's no need for me not to hide it.

It was actually already on my list because protobuf-net v3 adds value-type implementations of Empty, Duration, Timestamp, etc. But: great to have it listed here to make sure I get it done.

@mgravell
Copy link
Member

Just as an implementation note: what it'll actually do here will depend on the type; if the value-type being sent is a scalar, it'll treat it as though it were a message with a single field with field number 1, of the supplied type and value; if the value-type being sent is a message, it'll send the value as the root message, but in a way that makes gRPC happy.

@Ilchert
Copy link
Author

Ilchert commented Oct 16, 2019

Nice, thanks for the clarification!

@Sossenbinder
Copy link

I was just wondering about the progress on this issue - Did you manage to find time to do this as of yet?

@nilmas
Copy link

nilmas commented Mar 12, 2020

This enhancement would be very welcome.

@mgravell
Copy link
Member

mgravell commented Apr 7, 2020

I'm wondering if we can fix this at the core API instead ^^^

@LMSDev
Copy link

LMSDev commented Feb 15, 2021

@mgravell I recently came across your library (fantastic work, btw!) and encountered this type constraint. Additionally, I found gRPC's lack of support for multiple parameters discouraging, especially for simple method signatures. I understand the reasoning behind it, but it would still be nice to use them.

As a PoC I wrote a dynamic proxy that intercepts all calls with more than 1 argument (and where the second argument is not CancellationToken) and generates a new type with fields based on the arguments. This type is then serialized to byte[] using protobuf-net and passed to a more generic gRPC call along with the function name. The server uses the function name to locate the function signature and is able to also generate a type suitable for deserialization. The field values are then extracted from the deserialized type and passed to the original function (with multiple parameters), which happily does its work. Returning a value works exactly the same way.

Now I know that this would only work for protobuf-net.Grpc <--> protobuf-net.Grpc but I am wondering if you are interested in implementing a similar scheme. There is definitely a performance hit (a few ms, but did not optimize anything), so perhaps not well suited for high-performance scenarios, but for my use case the ability to call services with multiple parameters and no type constraints easily makes up for it.

@mgravell
Copy link
Member

mgravell commented Feb 15, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants