ServiceModel.Grpc

Code-first for gRPC

View on GitHub

ServiceModel.Grpc compatibility with native gRPC

To make ServiceModel.Grpc calls compatible with native gRPC:

Demo app

Small app to demonstrate compatibility with Native gRPC. The application is configured to use protobuf-net for data serialization.

Service and method names

.proto Calculator

message SumRequest {
    int32 x = 1;
    int32 y = 2;
}

message SumResponse {
    int64 result = 1;
}

// service name: Calculator
service Calculator {
    // method name Sum
    rpc Sum (SumRequest) returns (SumResponse);
}

ServiceModel.Grpc contract

[ServiceContract(Name = "Calculator")] // service name: Calculator
public interface ICalculator
{
    [OperationContract(Name = "Sum")] // method name: Sum
    Task<long> SumAsync(int x, int y);
}

For additional information refer to service and operation names and service and operation bindings.

Serialization

.proto Person

message Person {
    string firstName = 1;
    string secondName = 2;
    int32 Age = 3;
}

c# Person decorated with data contract

[DataContract]
public class Person
{
    [DataMember(Order = 1)]
    public string FirstName { get; set; }

    [DataMember(Order = 2)]
    public string SecondName { get; set; }

    [DataMember(Order = 3)]
    public int Age { get; set; }
}

c# Person decorated with proto contract

[ProtoContract]
public class Person
{
    [ProtoMember(1)]
    public string FirstName { get; set; }

    [ProtoMember(2)]
    public string SecondName { get; set; }

    [ProtoMember(3)]
    public int Age { get; set; }
}

For additional information refer to protobuf-net.