【问题标题】:Protoc 3.9 Compiled Output for C# FailsC# 的 Protoc 3.9 编译输出失败
【发布时间】:2019-07-13 01:45:12
【问题描述】:

运行时环境:.net core 2.2,.net framework 4.6.1 同样的问题

尝试为 Google Assistant SDK 的 C# 创建 gRPC 绑定时,在 EmbeddedAssistant.g.cs 中出现编译时错误 “无法从 System.Type 转换为 Google.Type”

可供下载的项目

https://drive.google.com/file/d/1Pbmg0Zk-ozXHkWT6a6FMmBt79MTHn7_t/view?usp=sharing

我在 Google.Protobuf 上也遇到了错误

“Type”是一个命名空间,但使用起来像一个类型。

详情

我关注了instructions to integrate google assistant for C# (Other languages: gRPC)

  1. clone Google APIs on local disk
  2. 我下载了 Protoc Compiler latest release
  3. 按照说明生成 EmbeddedAssistant.g.cs 文件
  4. 早些时候它抛出错误,Google.Protobuf.Collections 和 Google.Protobuf.Reflection 不可用,所以我从 # 的链接下载了 protobuf-csharp-3.9.0.zip 2 ,将 Google.Protobuf 文件夹复制到我的项目文件夹中,该错误消失了。

  5. 对于 Google.API 和 Google.Type 错误,我关注了这个 Stackoverflow article 并得到了解决。

错误所在的编译输出:

   descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData,
          new pbr::FileDescriptor[] { global::Google.Api.AnnotationsReflection.Descriptor, global::Google.Type.LatlngReflection.Descriptor, },
          new pbr::GeneratedClrTypeInfo(null, null, new pbr::GeneratedClrTypeInfo[] {
            new pbr::GeneratedClrTypeInfo(typeof(global::Google.Assistant.Embedded.V1Alpha2.AssistRequest), global::Google.Assistant.Embedded.V1Alpha2.AssistRequest.Parser, new[]{ "Config", "AudioIn" }, new[]{ "Type" }, null, null, null),
            new pbr::GeneratedClrTypeInfo(typeof(global::Google.Assistant.Embedded.V1Alpha2.AssistResponse), global::Google.Assistant.Embedded.V1Alpha2.AssistResponse.Parser, new[]{ "EventType", "AudioOut", "ScreenOut", "DeviceAction", "SpeechResults", "DialogStateOut", "DebugInfo" }, null, new[]{ typeof(global::Google.Assistant.Embedded.V1Alpha2.AssistResponse.Types.EventType) }, null, null),
            new pbr::GeneratedClrTypeInfo(typeof(global::Google.Assistant.Embedded.V1Alpha2.DebugInfo), global::Google.Assistant.Embedded.V1Alpha2.DebugInfo.Parser, new[]{ "AogAgentToAssistantJson" }, null, null, null, null),
            new pbr::GeneratedClrTypeInfo(typeof(global::Google.Assistant.Embedded.V1Alpha2.AssistConfig), global::Google.Assistant.Embedded.V1Alpha2.AssistConfig.Parser, new[]{ "AudioInConfig", "TextQuery", "AudioOutConfig", "ScreenOutConfig", "DialogStateIn", "DeviceConfig", "DebugConfig" }, new[]{ "Type" }, null, null, null),
            new pbr::GeneratedClrTypeInfo(typeof(global::Google.Assistant.Embedded.V1Alpha2.AudioInConfig), global::Google.Assistant.Embedded.V1Alpha2.AudioInConfig.Parser, new[]{ "Encoding", "SampleRateHertz" }, null, new[]{ typeof(global::Google.Assistant.Embedded.V1Alpha2.AudioInConfig.Types.Encoding) }, null, null),
            new pbr::GeneratedClrTypeInfo(typeof(global::Google.Assistant.Embedded.V1Alpha2.AudioOutConfig), global::Google.Assistant.Embedded.V1Alpha2.AudioOutConfig.Parser, new[]{ "Encoding", "SampleRateHertz", "VolumePercentage" }, null, new[]{ typeof(global::Google.Assistant.Embedded.V1Alpha2.AudioOutConfig.Types.Encoding) }, null, null),
            new pbr::GeneratedClrTypeInfo(typeof(global::Google.Assistant.Embedded.V1Alpha2.ScreenOutConfig), global::Google.Assistant.Embedded.V1Alpha2.ScreenOutConfig.Parser, new[]{ "ScreenMode" }, null, new[]{ typeof(global::Google.Assistant.Embedded.V1Alpha2.ScreenOutConfig.Types.ScreenMode) }, null, null),
            new pbr::GeneratedClrTypeInfo(typeof(global::Google.Assistant.Embedded.V1Alpha2.DialogStateIn), global::Google.Assistant.Embedded.V1Alpha2.DialogStateIn.Parser, new[]{ "ConversationState", "LanguageCode", "DeviceLocation", "IsNewConversation" }, null, null, null, null),
            new pbr::GeneratedClrTypeInfo(typeof(global::Google.Assistant.Embedded.V1Alpha2.DeviceConfig), global::Google.Assistant.Embedded.V1Alpha2.DeviceConfig.Parser, new[]{ "DeviceId", "DeviceModelId" }, null, null, null, null),
            new pbr::GeneratedClrTypeInfo(typeof(global::Google.Assistant.Embedded.V1Alpha2.AudioOut), global::Google.Assistant.Embedded.V1Alpha2.AudioOut.Parser, new[]{ "AudioData" }, null, null, null, null),
            new pbr::GeneratedClrTypeInfo(typeof(global::Google.Assistant.Embedded.V1Alpha2.ScreenOut), global::Google.Assistant.Embedded.V1Alpha2.ScreenOut.Parser, new[]{ "Format", "Data" }, null, new[]{ typeof(global::Google.Assistant.Embedded.V1Alpha2.ScreenOut.Types.Format) }, null, null),
            new pbr::GeneratedClrTypeInfo(typeof(global::Google.Assistant.Embedded.V1Alpha2.DeviceAction), global::Google.Assistant.Embedded.V1Alpha2.DeviceAction.Parser, new[]{ "DeviceRequestJson" }, null, null, null, null),
            new pbr::GeneratedClrTypeInfo(typeof(global::Google.Assistant.Embedded.V1Alpha2.SpeechRecognitionResult), global::Google.Assistant.Embedded.V1Alpha2.SpeechRecognitionResult.Parser, new[]{ "Transcript", "Stability" }, null, null, null, null),
            new pbr::GeneratedClrTypeInfo(typeof(global::Google.Assistant.Embedded.V1Alpha2.DialogStateOut), global::Google.Assistant.Embedded.V1Alpha2.DialogStateOut.Parser, new[]{ "SupplementalDisplayText", "ConversationState", "MicrophoneMode", "VolumePercentage" }, null, new[]{ typeof(global::Google.Assistant.Embedded.V1Alpha2.DialogStateOut.Types.MicrophoneMode) }, null, null),
            new pbr::GeneratedClrTypeInfo(typeof(global::Google.Assistant.Embedded.V1Alpha2.DebugConfig), global::Google.Assistant.Embedded.V1Alpha2.DebugConfig.Parser, new[]{ "ReturnDebugInfo" }, null, null, null, null),
            new pbr::GeneratedClrTypeInfo(typeof(global::Google.Assistant.Embedded.V1Alpha2.DeviceLocation), global::Google.Assistant.Embedded.V1Alpha2.DeviceLocation.Parser, new[]{ "Coordinates" }, new[]{ "Type" }, null, null, null)
          }));
    }

EmbeddedAssistant.g.cs 第 101 到 116 行的编译输出出现编译器错误

Google.Protobuf ReflectionUtil.cs 上的编译器错误类型 Type 是一个命名空间,但用作类型。

 internal static readonly Type[] EmptyTypes = new Type[0];

        /// <summary>
        /// Creates a delegate which will cast the argument to the type that declares the method,
        /// call the method on it, then convert the result to object.
        /// </summary>
        /// <param name="method">The method to create a delegate for, which must be declared in an IMessage
        /// implementation.</param>
        internal static Func<IMessage, object> CreateFuncIMessageObject(MethodInfo method) =>
            GetReflectionHelper(method.DeclaringType, method.ReturnType).CreateFuncIMessageObject(method);

C# Project Structure. Files under Google folder /API , /Assistant and /Type were generated by Protoc 3.9.0 compiler

Errors look like this

【问题讨论】:

  • Error1 GeneratedClrTypeInfo 的第一个参数抛出错误typeof(global::Google.Assistant.Embedded.V1Alpha2.AssistRequest) 错误:无法从 System.Type 转换为 Google.Type 错误 2 : ** Google.Protobuf.Reflection.ReflectionUtil.cs 第 75 行及以后 ** 错误:类型是命名空间,但用作类型
  • 问题我发布在协议缓冲区 github repo github.com/protocolbuffers/protobuf/issues/6388

标签: c# google-assistant-sdk protoc


【解决方案1】:

问题已解决

我使用了这个链接 https://github.com/grpc/grpc/blob/master/src/csharp/BUILD-INTEGRATION.md

基本上从 googleapis 添加了最少必要的 .proto 文件,以使 google 助手工作。附上图片

Screenshot of what project looks like

然后添加 Grpc ,Grpc.Tools nuget 包,按照上面文章中的说明更改 .csproj 并构建它。

我刚刚在我的 csproj 中添加了以下内容

<Protobuf Include="**/*.proto" OutputDir="%(RelativePath)"/>

它生成了如图所示的输出

构建失败,但生成了 CS 文件。 获取生成的 CS 文件,将它们添加到您的实际项目中并添加 Google.Protobuf nuget 包,构建,一切顺利。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-22
    • 1970-01-01
    • 1970-01-01
    • 2022-10-14
    • 1970-01-01
    相关资源
    最近更新 更多