【问题标题】:c# models to protobuf messages (gRPC project)c#模型到protobuf消息(gRPC项目)
【发布时间】:2022-06-30 06:17:45
【问题描述】:

我可以找到很多关于如何从 protobuf 消息生成 C# 模型的资源(它甚至内置在 Grpc.AspNetCore 包中),但反之则不行。

我创建了一个类似于此的 blazor webassembly 客户端/服务器项目:https://github.com/stevejgordon/gRPCBasicSample/

我有一个域层,其中包含许多用于应用程序的 C# 模型。我想将这些模型转换为消息回复“视图模型”,而不是全部编写(我什至不确定 c# 和 protobuf 之间的正确转换)

示例: C# 模型:

    /// <summary>
    /// The area.
    /// </summary>
    public class Area
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Area"/> class.
        /// </summary>
        /// <param name="name">Name of the region.</param>
        /// <param name="areaType">What type of area is the region.</param>
        /// <param name="numberOfSupplies">number of supplies the region has.</param>
        /// <param name="numberOfMusterCrows">number of crowns the region has.</param>
        public Area(string name, AreaType areaType, int numberOfSupplies, int numberOfMusterCrows)
        {
            this.Name = name;
            this.AreaType = areaType;
            this.NumberOfSupplies = numberOfSupplies;
            this.NumberOfMusterCrowns = numberOfMusterCrows;
            this.CurrentArmy = new Collection<Unit>();
            this.AdjacentAreas = new Collection<Area>();
        }

        /// <summary>
        /// Gets the name of the region.
        /// </summary>
        public string Name { get; }

        /// <summary>
        /// Gets or sets which house the area is controlled by.
        /// </summary>
        public House ControlledBy { get; set; }

        /// <summary>
        /// Gets the type of area the region is.
        /// </summary>
        public AreaType AreaType { get; }

        /// <summary>
        /// Gets the adjacent regions the region has.
        /// </summary>
        public IEnumerable<Area> AdjacentAreas { get; internal set; }

        /// <summary>
        /// Gets the number of supplies (barrels) the region has.
        /// </summary>
        public int NumberOfSupplies { get; }

        /// <summary>
        /// Gets the number of crowns the region has.
        /// </summary>
        public int NumberOfMusterCrowns { get; }

        /// <summary>
        /// Gets or sets a value indicating whether the region is occupied by troops.
        /// </summary>
        public bool IsOccupied { get; set; }

        /// <summary>
        /// Gets a value indicating whether the region has a stronghold.
        /// </summary>
        public bool HasStronghold { get; }

        /// <summary>
        /// Gets a value indicating whether the region has a castle.
        /// </summary>
        public bool HasCastle { get; }

        /// <summary>
        /// Gets or sets the current armies occupying the region.
        /// </summary>
        public ICollection<Unit> CurrentArmy { get; set; }

        /// <summary>
        /// Gets or sets the current order token placed.
        /// </summary>
        public OrderToken? PlacedOrderToken { get; set; }
    }

应该变成这样的东西(简化):

message AreaReply {
  string name = 1;
  enum AreaType {
    Land = 0;
    Water = 1;
  }
  AreaType areaType = 4;
  House controlledBy = 5;
}
message House {
    google.protobuf.StringValue name = 1;
}

真的没有这样的发电机吗? :)

谢谢

【问题讨论】:

    标签: c# grpc protobuf-net


    【解决方案1】:

    在你的类定义上使用这个注解:

    [ProtoContract(ImplicitFields = ImplicitFields.AllPublic)]

    见:Protobuf-net serialization without annotation

    【讨论】:

    • 谢谢,但这看起来像是另一种需要在运行时序列化/反序列化的方法?我将如何使用这样的方法从前端发送和接收数据:public async override Task&lt;AreaReply&gt; GetArea(AreaRequest request, ServerCallContext context) 请求和回复类是强类型的。我可能正在寻找一种自动生成类的 nswag 解决方案。
    【解决方案2】:

    您是否曾找到解决此问题的方法?我遇到了同样的情况,使用 protobuf-net 似乎与使用 gRPC 不直观。 (我可能不完全理解解决方案,但是当我看到序列化和反序列化时,我继续前进)

    【讨论】:

      猜你喜欢
      • 2019-07-07
      • 2019-01-06
      • 1970-01-01
      • 2021-03-17
      • 1970-01-01
      • 1970-01-01
      • 2019-12-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多