简体   繁体   English

使用 Grpc.Tools 和 Protoc 插件生成额外的 C# 文件

[英]Using Grpc.Tools with Protoc plug-in to generate additional C# files

I am using Grpc.Tools (2.38.1) to generate C# types and gRPC stubs from a Test.proto file containing some service definitions.我使用Grpc.Tools(2.38.1)从生成C#类型和GRPC存根Test.proto包含一些服务定义文件。

To do this I have the following in my project's .csproj file:为此,我在项目的.csproj文件中有以下内容:

<ItemGroup>
    <Protobuf Include="**/*.proto" />
</ItemGroup>

This is all working fine: my Test.proto gets compiled to Test.cs and TestGrpc.cs in the obj/Debug folder of my project.这是所有工作的罚款:我Test.proto被编译到Test.csTestGrpc.cs在我的项目的OBJ / Debug文件夹中。 The types within them can be referenced from within other types in the project.它们中的类型可以从项目中的其他类型中引用。

But I need to create a WCF interface for the service too, so I thought I could generate this using a custom Protoc plug-in.但是我也需要为服务创建一个 WCF 接口,所以我想我可以使用自定义的 Protoc 插件来生成它。 So I wrote a simple Protoc plug-in that writes out a TestWcf.cs file containing an interface.于是我写了一个简单的TestWcf.cs插件,写出一个包含接口的TestWcf.cs文件。 I then placed this plug-in executable on my path named protoc-gen-blah.exe and updated the entry in the .csproj file to this:然后我将此插件可执行文件放在名为protoc-gen-blah.exe路径上,并将.csproj文件中的条目更新为:

<ItemGroup>
    <Protobuf Include="**/*.proto" AdditionalProtocArguments="--blah_out=obj\Debug" />
</ItemGroup>

This correctly creates the C# file, TestWcf.cs , with my interface in: fantastic.这正确地创建了 C# 文件TestWcf.cs ,我的界面在:太棒了。

The problem is that my interface within TestWcf.cs cannot be referenced from other types in the project unless I manually include the generated file in the project: something I do not have to do with the other generated files.问题是我在TestWcf.cs接口不能被项目中的其他类型引用,除非我手动将生成的文件包含在项目中:我不需要处理其他生成的文件。

Whilst none of the files are included in the project by default―I have to enable 'Show All Files' to see them― Test.cs and TestGrpc.cs have arrows beside them in the Solution Explorer that allow them to be expanded to reveal the types inside.虽然默认情况下没有任何文件包含在项目中——我必须启用“显示所有文件”才能看到它们——在解决方案资源管理器中, Test.csTestGrpc.cs旁边有箭头,允许展开它们以显示里面的类型。 TestWcf.cs does not have this arrow. TestWcf.cs没有这个箭头。 So Visual Studio is somehow aware that Test.cs and TestGrpc.cs are source code files.所以 Visual Studio 以某种方式知道Test.csTestGrpc.cs是源代码文件。

Does anyone know what I need to do for my generated file to be automatically recognised by Visual Studio like the other two files are?有谁知道我需要做什么才能让 Visual Studio 像其他两个文件一样自动识别生成的文件?

I suspect it has something to do with this part of the Grpc.Tools build target , as I noticed my TestWcf.cs file is not included in the files deleted by the Grpc.Tools clean either, but I can't see why it does not consider my generated file to be C#.我怀疑它与Grpc.Tools 构建目标的这一部分有关,因为我注意到我的TestWcf.cs文件也不包含在 Grpc.Tools clean 删除的文件中,但我不明白为什么会这样不要认为我生成的文件是 C#。

When I build, this is the Protoc call:当我构建时,这是 Protoc 调用:

D:\...\Src\packages\Grpc.Tools.2.38.1\tools\windows_x86\protoc.exe --csharp_out=obj\Debug ⤶
--plugin=protoc-gen-grpc=D:\...\Src\packages\Grpc.Tools.2.38.1\tools\windows_x86\grpc_csharp_plugin.exe ⤶
--grpc_out=obj\Debug --proto_path=D:\...\Src\packages\Grpc.Tools.2.38.1\build\native\include ⤶
--proto_path=. --dependency_out=obj\Debug\xxxx_Test.protodep --error_format=msvs --blah_out=obj\Debug ⤶
Test.proto

The dependency file looks like this:依赖文件如下所示:

obj\Debug/Test.cs \
obj\Debug/TestGrpc.cs \
obj\Debug/TestWcf.cs: Test.proto

Thanks.谢谢。

I believe the problem is caused some logic in Grpc.Tools that informs MSBuild of the files that have been generated:我相信这个问题是由 Grpc.Tools 中的一些逻辑引起的, 这些逻辑通知 MSBuild 已生成的文件:

public override string[] GetPossibleOutputs(ITaskItem protoItem)
{
    ...     
    var outputs = new string[doGrpc ? 2 : 1];
    ...
    outputs[0] = Path.Combine(outdir, filename) + ".cs";

    if (doGrpc)
    {
        ...
        outputs[1] = Path.Combine(grpcdir, filename) + "Grpc.cs";
    }
    return outputs;
}

This code only caters for two files being generated from a Protocol Buffer source ( name.proto ): the Protocol Buffers code generation ( name.cs ) and the gRPC code generation ( nameGrpc.cs ).用于从协定缓冲源(产生两个文件此代码仅迎合name.proto的协议缓冲器代码生成(:) name.cs )和GRPC代码生成( nameGrpc.cs )。 It is not picking up the additional file and informing MSBuild that it exists, hence Visual Studio does not consider it to be code.它没有选择附加文件并通知 MSBuild 它存在,因此 Visual Studio 不认为它是代码。

There is no away around this short of changing the Grpc.Tools code.除了更改 Grpc.Tools 代码之外,没有其他办法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM