-
Notifications
You must be signed in to change notification settings - Fork 321
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[c# grpc] Skip compilation for --structs=false
* The MSBuild codegen targets now detect --structs=false and skip automatic compilation of the non-existent `_types.cs` files. * Added a gRPC example that show how to use a shared type assembly between a client and service.
- Loading branch information
Showing
14 changed files
with
459 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Using a shared types assembly | ||
|
||
Sometimes you want to compile the types from a .bond file separately from | ||
the gRPC services and clients. This example shows how to split the types | ||
into their own assembly. | ||
|
||
The [`gbc`](https://microsoft.github.io/bond/manual/compiler.html) options | ||
`--structs` and `--grpc` can be used to control whether codegen is performed | ||
for structs and services. | ||
|
||
In the `types` directory, codegen is performed with out the `--grpc` switch, | ||
so just types are generated and compiled. | ||
|
||
In the `client` and `service` directories, however, codegen is performed | ||
with the `--grpc` switch. Also, `--structs=false` is passed to disable the | ||
default behavior of generating C# code for the types. If `--structs=false` | ||
were not specified, there would be duplicate types in different assemblies, | ||
resulting in a conflict that would need to be resolved via | ||
[`extern alias`](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/extern-alias). |
32 changes: 32 additions & 0 deletions
32
examples/cs/grpc/shared-types-assembly/client/StaClient.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace Examples.SharedTypes | ||
{ | ||
using System; | ||
using Grpc.Core; | ||
|
||
public static class Client | ||
{ | ||
const string Address = "127.0.0.1"; | ||
const int Port = 50051; | ||
|
||
public static void Main() | ||
{ | ||
var channel = new Channel(Address, Port, ChannelCredentials.Insecure); | ||
var client = new Calc.CalcClient(channel); | ||
|
||
var request = new Request | ||
{ | ||
Num1 = 40, | ||
Num2 = 2 | ||
}; | ||
|
||
var response = client.AddAsync(request).GetAwaiter().GetResult(); | ||
|
||
Console.WriteLine($"Addition result: {response.Payload.Deserialize().Result}"); | ||
|
||
channel.ShutdownAsync().GetAwaiter().GetResult(); | ||
} | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
examples/cs/grpc/shared-types-assembly/client/grpc_sta-client.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> | ||
<Import Project="..\..\..\..\..\cs\build\nuget\Bond.CSharp.props" /> | ||
<PropertyGroup> | ||
<ProjectGuid>{9DCEAD87-C61F-40D5-989E-0E44F56E01BE}</ProjectGuid> | ||
<OutputType>Exe</OutputType> | ||
<RootNamespace>Examples.SharedTypes</RootNamespace> | ||
<AssemblyName>grpc_sta-client</AssemblyName> | ||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
<PlatformTarget>AnyCPU</PlatformTarget> | ||
<DebugSymbols>true</DebugSymbols> | ||
<DebugType>full</DebugType> | ||
<Optimize>false</Optimize> | ||
<OutputPath>bin\Debug\</OutputPath> | ||
<DefineConstants>DEBUG;TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
<PlatformTarget>AnyCPU</PlatformTarget> | ||
<DebugType>pdbonly</DebugType> | ||
<Optimize>true</Optimize> | ||
<OutputPath>bin\Release\</OutputPath> | ||
<DefineConstants>TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Reference Include="System" /> | ||
<Reference Include="Grpc.Core"> | ||
<HintPath>..\..\..\..\..\cs\packages\Grpc.Core.1.3.0\lib\net45\Grpc.Core.dll</HintPath> | ||
</Reference> | ||
<Reference Include="System.Interactive.Async"> | ||
<HintPath>..\..\..\..\..\cs\packages\System.Interactive.Async.3.1.1\lib\net45\System.Interactive.Async.dll</HintPath> | ||
</Reference> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Compile Include="StaClient.cs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<!-- This import is not needed when consuming Bond via NuGet packages. | ||
Due to the different layout of the Bond repository, we manually | ||
specify it here. --> | ||
<BondCodegen Include="..\sta.bond"> | ||
<!-- We could also use BondOptions property to configure this for all | ||
files. Using BondOptions results in more efficient codegen and | ||
should be preferred if it can be used. For this example, we'll | ||
the per-item metadata. --> | ||
<Options>--grpc --structs=false</Options> | ||
</BondCodegen> | ||
<!-- Resharper Workaround --> | ||
<Compile Include="$(IntermediateOutputPath)\sta_grpc.cs" Condition="False" /> | ||
<!-- End Resharper Workaround --> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Reference Include="Attributes"> | ||
<HintPath>$(BOND_BINARY_PATH)\net45\Bond.Attributes.dll</HintPath> | ||
</Reference> | ||
<Reference Include="Bond"> | ||
<HintPath>$(BOND_BINARY_PATH)\net45\Bond.dll</HintPath> | ||
</Reference> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<None Include="packages.config" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\..\..\cs\src\grpc\grpc.csproj"> | ||
<Project>{AF03BAE6-2470-4E1B-A683-3EBDCDC595FA}</Project> | ||
<Name>grpc</Name> | ||
</ProjectReference> | ||
<ProjectReference Include="..\types\grpc_sta-types.csproj"> | ||
<Project>{723baa6b-b309-48cf-b2a9-ecddc91909d1}</Project> | ||
<Name>grpc_sta-types</Name> | ||
</ProjectReference> | ||
</ItemGroup> | ||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||
<Import Project="$(BOND_PATH)\build\nuget\Bond.CSharp.targets" /> | ||
<Import Project="..\..\..\..\..\cs\packages\Grpc.Core.1.3.0\build\net45\Grpc.Core.targets" Condition="Exists('..\..\..\..\..\cs\packages\Grpc.Core.1.3.0\build\net45\Grpc.Core.targets')" /> | ||
</Project> |
Oops, something went wrong.