Skip to content

Commit 191dfbb

Browse files
another try
1 parent c111bdb commit 191dfbb

File tree

4 files changed

+71
-20
lines changed

4 files changed

+71
-20
lines changed

ConsoleAppGRPC/ConsoleAppGRPC.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
<Protobuf Include="..\DemoAspNetCore3\Protos\country.proto" GrpcServices="Client">
3232
<Link>Protos\country.proto</Link>
3333
</Protobuf>
34+
<Protobuf Include="..\DemoAspNetCore3\Protos\greet.proto" GrpcServices="Client">
35+
<Link>Protos\greet.proto</Link>
36+
</Protobuf>
3437
</ItemGroup>
3538

3639
</Project>

ConsoleAppGRPC/Program.cs

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,15 @@ static async Task Main(string[] args)
5353
var client = provider.GetRequiredService<CountryServiceClient>();
5454
*/
5555

56-
5756
// gRPC-Web
58-
var handler = new GrpcWebHandler(GrpcWebMode.GrpcWebText, HttpVersion.Version11, new HttpClientHandler());
59-
var channel = GrpcChannel.ForAddress("https://grpcwebdemo.azurewebsites.net", new GrpcChannelOptions
60-
{
61-
HttpClient = new HttpClient(handler),
62-
LoggerFactory = loggerFactory
63-
});
64-
var clientWeb = new CountryServiceClient(channel);
65-
57+
//var handler = new GrpcWebHandler(GrpcWebMode.GrpcWebText, HttpVersion.Version11, new HttpClientHandler());
58+
//var channel = GrpcChannel.ForAddress("https://grpcwebdemo.azurewebsites.net:8080", new GrpcChannelOptions
59+
//{
60+
// HttpClient = new HttpClient(handler),
61+
// LoggerFactory = loggerFactory
62+
//});
63+
//var clientWeb = new CountryServiceClient(channel);
64+
6665
try
6766
{
6867
/*
@@ -116,18 +115,29 @@ static async Task Main(string[] args)
116115
countries.ForEach(x => Console.WriteLine($"Found country {x.CountryName} ({x.CountryId}) {x.Description}"));
117116
*/
118117

119-
118+
120119
// Get all gRPC-web
121-
var countriesweb = (await clientWeb.GetAllAsync(new EmptyRequest())).Countries.Select(x => new Country
120+
//var countriesweb = (await clientWeb.GetAllAsync(new EmptyRequest())).Countries.Select(x => new Country
121+
//{
122+
// CountryId = x.Id,
123+
// Description = x.Description,
124+
// CountryName = x.Name
125+
//}).ToList();
126+
127+
//Console.WriteLine("Found countries with gRPC-Web");
128+
//countriesweb.ForEach(x => Console.WriteLine($"Found country with gRPC-Web: {x.CountryName} ({x.CountryId}) {x.Description}"));
129+
130+
131+
var handler2 = new GrpcWebHandler(GrpcWebMode.GrpcWebText, new HttpClientHandler());
132+
var channel2 = GrpcChannel.ForAddress("https://grpcweb.azurewebsites.net", new GrpcChannelOptions
122133
{
123-
CountryId = x.Id,
124-
Description = x.Description,
125-
CountryName = x.Name
126-
}).ToList();
134+
HttpClient = new HttpClient(handler2),
135+
LoggerFactory = loggerFactory
136+
});
137+
var clientweb2 = new Greeter.GreeterClient(channel2);
138+
var response = await clientweb2.SayHelloAsync(new HelloRequest { Name = ".NET" });
127139

128-
Console.WriteLine("Found countries with gRPC-Web");
129-
countriesweb.ForEach(x => Console.WriteLine($"Found country with gRPC-Web: {x.CountryName} ({x.CountryId}) {x.Description}"));
130-
140+
Console.WriteLine(response.Message);
131141

132142
}
133143
catch (RpcException e)

DemoAspNetCore3/Protos/greet.proto

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
syntax = "proto3";
2+
3+
option csharp_namespace = "DemoGrpc.Protobufs";
4+
5+
// The greeting service definition.
6+
service Greeter {
7+
// Sends a greeting
8+
rpc SayHello (HelloRequest) returns (HelloReply);
9+
rpc SayHelloWithHttpContextAccessor (HelloRequest) returns (HelloReply);
10+
rpc SayHelloSendLargeReply (HelloRequest) returns (HelloReply);
11+
rpc SayHellos (HelloRequest) returns (stream HelloReply);
12+
rpc SayHellosSendHeadersFirst (HelloRequest) returns (stream HelloReply);
13+
}
14+
15+
// Test multiple services in one package
16+
service SecondGreeter {
17+
rpc SayHello (HelloRequest) returns (HelloReply) {}
18+
}
19+
20+
// The request message containing the user's name.
21+
message HelloRequest {
22+
string name = 1;
23+
}
24+
25+
// The response message containing the greetings
26+
message HelloReply {
27+
string message = 1;
28+
}

DemoAspNetCore3/Startup.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,20 @@ public void ConfigureServices(IServiceCollection services)
108108
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
109109
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
110110
{
111-
111+
if (env.IsDevelopment())
112+
{
113+
app.UseDeveloperExceptionPage();
114+
}
115+
else
116+
{
117+
app.UseHsts();
118+
}
119+
120+
app.UseHttpsRedirection();
121+
112122
app.UseRouting();
113123

114-
app.UseCors();
124+
app.UseCors("MyPolicy");
115125

116126
app.UseGrpcWeb();
117127

0 commit comments

Comments
 (0)