在我们的项目中,我们成功使用Google Protobuf for C++.现在需要编译相同的*.proto文件以在C#代码中使用它.我下载了最近的Protobuf版本3.0.0-alpha-3.它为C#提供了proto2格式的支持,这对我来说已经足够了.我可以成功构建我的*.proto文件并获得*.cs文件.但是,当我将生成的*.cs文件添加到我的C#项目并尝试构建时,我会收到类似这样的编译器错误:"The type or namespace name 'Google' could not be found in the global namespace (are you missing an assembly reference?)"这是发生错误的地方:
// Generated by the protocol buffer compiler. DO NOT EDIT!
// source: DiagramExport.proto
#pragma warning disable 1591, 0612, 3021
#region Designer generated code
using pb = global::Google.ProtocolBuffers;
using pbc = global::Google.ProtocolBuffers.Collections;
using pbd = global::Google.ProtocolBuffers.Descriptors;
Run Code Online (Sandbox Code Playgroud)
现在我在项目页面上的发布ZIP中找不到任何DLL等,我可以在C#项目中作为参考包含它.只有protoc.exe和一些*.proto文件.我的简单问题是:我从哪里获得这些装配?
(备注:我尝试按照README文件中的说明从源代码构建项目protobuf-csharp-3.0.0-alpha-3,但未能使用Visual Studio 2013 Update 4"开箱即用"构建它;我得到了一些编译器错误.)
我有以下 protoc3 消息:
message LocalizedString {
map<string, string> translations = 1
}
Run Code Online (Sandbox Code Playgroud)
当编译成 C# 时,我得到以下自动生成的代码:
using pb = global::Google.Protobuf;
using pbc = global::Google.Protobuf.Collections;
using pbr = global::Google.Protobuf.Reflection;
using scg = global::System.Collections.Generic;
namespace PKIo {
/// <summary>Holder for reflection information generated from io/common/localization.proto</summary>
public static partial class LocalizationReflection {
#region Descriptor
/// <summary>File descriptor for io/common/localization.proto</summary>
public static pbr::FileDescriptor Descriptor {
get { return descriptor; }
}
private static pbr::FileDescriptor descriptor;
static LocalizationReflection() {
byte[] descriptorData = global::System.Convert.FromBase64String(
string.Concat(
"Chxpby9jb21tb24vbG9jYWxpemF0aW9uLnByb3RvEgJpbyKDAQoPTG9jYWxp",
"emVkU3RyaW5nEjsKDHRyYW5zbGF0aW9ucxgBIAMoCzIlLmlvLkxvY2FsaXpl", …Run Code Online (Sandbox Code Playgroud) 这是我的原型文件:
message MSG {
required MsgCodes MsgCode = 1;
optional int64 Serial = 2; // Unique ID number for this person.
required int32 From = 3;
required int32 To = 4;
//bla bla...
enum MsgCodes
{
MSG = 1;
FILE = 2;
APPROVE=4;
ACK=8;
ERROR_SENDING=16;
WORLD=32;
}
}
Run Code Online (Sandbox Code Playgroud)
在我的C#中我试图:
msg = msg.ToBuilder().SetMsgCode(msg.MsgCode | MSG.Types.MsgCodes.ACK | MSG.Types.MsgCodes.APPROVE).Build();
SendToJava(msg);
Run Code Online (Sandbox Code Playgroud)
但JAVA告诉我:缺少MsgCode(这是一个required)
删除组合 - 确实解决了它
但我需要指定组合
题
我该如何解决?
nb:
奇怪的是,如果我创建一个msg并设置多个枚举,然后再次在C#中读取它 - 它确实有效...... :-(
Visual Studio 2015
C#
NuGet Packages :
Google.Protobuf v3.0.0
Google.Protobuf.Tools v3.0.0
Run Code Online (Sandbox Code Playgroud)
消息类型 Quake
syntax = "proto3";
import "google/protobuf/timestamp.proto";
message Quake {
google.protobuf.Timestamp _timestamp = 1;
double magnitude = 2;
}
Run Code Online (Sandbox Code Playgroud)
timestamp.proto 包含在同一个文件夹中
protoc 3.0.2 命令行编译成功
但是VS右键单击proto文件并选择“运行自定义工具”失败,错误“自定义工具'ProtoBufTool'失败。”
从 Visual Studio 中生成 C# 类?
c# timestamp protocol-buffers protobuf-csharp-port protocol-buffers-3
我想将字符串对象转换为ByteString。我尝试使用ByteString.CopyFrom()函数进行转换,但是返回值始终为“ {Google.ProtocolBuffers.ByteString}”。为什么?我能怎么做?
我正在寻找替换ASF中RPC的默认序列化器。这涉及实现一些接口,其中一个接口在通过RPC进行通信的服务之间传递
public interface IServiceRemotingResponseMessageBody
{
void Set(object response);
object Get(Type paramType);
}
Run Code Online (Sandbox Code Playgroud)
由于实现需要可序列化,因此显而易见的ProtoBuf实现类似于
[ProtoContract]
public class ProtoBufRemotingResponseBody : IServiceRemotingResponseMessageBody
{
[ProtoMember(1)]
public object Value { get; set; }
public void Set(object response)
{
Value = response;
}
public object Get(Type paramType)
{
return Value;
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,这失败了
没有为类型定义序列化器:System.Object
这里有解决方法吗?System.Object的没有合同,但开箱即用的DataContract串行器可以,如可以MessagePack 这里,但这些都不是系统化它可以创建时的版本头痛可靠的集合。我试过使用常见的基本类型,但Value可以是IEnumerable<T>或T等。
有人可以帮忙吗?谢谢,KH
protobuf-net protobuf-csharp-port azure-service-fabric service-fabric-stateful
我有以下 protobuf 消息协议:
message TestMsg
{
int32 int = 1;
google.protobuf.Int32Value nullable_int = 2;
repeated google.protobuf.Int32Value nullable_int_array = 3; // Runtime fail
}
Run Code Online (Sandbox Code Playgroud)
protoc 可以很好地编译它,并且在 C# 中所有 Int32Value 都是 int?。但它在运行时失败,并出现空参数不允许异常。我可以理解repeated不允许空消息。但是Int32Value是 WellKnownType,因此编译器可以根据需要生成特殊的 NullValue 类型。
这是 protobuf 中的限制(不允许Int32Valuein repeated)还是 C# 代码生成和支持库中的限制/错误?
除了创建自己的消息和代码生成之外,在 protobuf 协议中执行可空 int 数组的选项还有哪些?
我们使用 protobuf v.3 通过 HTTP 将消息从 C# 客户端传输到 Java 服务器。
消息原型如下所示:
message CLIENT_MESSAGE {
string message = 1;
}
Run Code Online (Sandbox Code Playgroud)
客户端和服务器都对字符串使用 UTF-8 字符编码。
当我们使用像“abc”这样的短字符串值时一切都很好,但是当我们尝试传输包含 198 个字符的字符串时,我们捕获了一个异常:
com.google.protobuf.InvalidProtocolBufferException:
While parsing a protocol message, the input ended unexpectedly in the middle of a field. This could mean either that the input has been truncated or that an embedded message misreported its own length.
Run Code Online (Sandbox Code Playgroud)
我们甚至尝试比较包含 protobuf 数据的字节数组,但没有找到解决方案。对于“aaa”字符串字节数组以以下字节开头:
10 3 97 97 97
其中 10 是 protobuf 字段编号,3 是字符串长度,69 65 67 是“aaa”。
对于字符串
“aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa”
其中包含 …
我正在尝试对 protobuf 消息实施限制,这些消息可以在使用不同语言的不同客户端之间共享
除此之外,我想对字符串长度实施限制,例如最小或最大长度,或者 int32 var 的最小和最大值范围
有没有办法实现这样的要求?非常感谢你的帮助!
我从Google.Protobuf Git中了解到有一些关于.NET Core支持的评论但是当我从Git获得源代码的副本时,Google.Protobuf项目在.依赖关系中列出.NetFramework 4.5以及.NetStandard 1.0 .
在csharp文件夹中的Google.Protobuf Git Depot上,还有一些关于.NET Core支持的评论.
此外,通过通过问题跟踪器上连接到仓库去,我碰到这个线程从大约一年回有关获取Google.Protobuf发挥好与.NET的核心.
谁能给我一个关于Google.Protobuf是否支持.NET Core的明确答案?
protocol-buffers protobuf-net protobuf-csharp-port .net-core uwp
我想在两个类之间映射:
public class A {
public IEnumerable<C> someList
}
Run Code Online (Sandbox Code Playgroud)
和
public class B {
public RepeatedField<D> someList
}
Run Code Online (Sandbox Code Playgroud)
其中 RepeatedField 是来自 Google.Protobuf.Collections 的一个类,用于处理 gRPC 数据。
编辑:事实证明,gRPC 通过其原型创建类的方式与创建类 B 并不完全一样。请参阅我的答案。
我像这样创建了一个 Automapper MappingConfiguration
return new MapperConfiguration(cfg =>
{
cfg.CreateMap<C, D>().ReverseMap();
cfg.CreateMap<A, B>().ReverseMap();
});
Run Code Online (Sandbox Code Playgroud)
然后它通过 ASP.NET Startup 类注册。
如果我在另一堂课上做这样的事情
A instanceA; // assume A's list has values inside
var listofD = this.mapper.Map<List<D>>(A.someList)
Run Code Online (Sandbox Code Playgroud)
它正确返回一个包含值的列表。然而:
A instanceA; // assume A's list has values inside
B instanceB = this.mapper.Map<B>(A);
Run Code Online (Sandbox Code Playgroud)
返回 B 的一个实例,但 instanceB 中的列表为空。我该如何解决?
c# ×7
protobuf-net ×3
.net ×1
.net-core ×1
assemblies ×1
automapper ×1
enums ×1
grpc ×1
ienumerable ×1
java ×1
list ×1
protobuf-3 ×1
timestamp ×1
uwp ×1