Skip to content

Commit 0ab5e2c

Browse files
authored
Change examples to use WebApplication V3 (#1878)
1 parent 9950fd7 commit 0ab5e2c

File tree

83 files changed

+394
-41402
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+394
-41402
lines changed

Diff for: examples/Locator/Client/Program.cs

+35-46
Original file line numberDiff line numberDiff line change
@@ -16,62 +16,51 @@
1616

1717
#endregion
1818

19-
using System;
2019
using System.Diagnostics;
21-
using System.Threading.Tasks;
2220
using Greet;
2321
using Grpc.Core;
2422
using Grpc.Net.Client;
2523

26-
namespace Client
24+
await MakeInternalCall("https://localhost:5001");
25+
try
2726
{
28-
public class Program
29-
{
30-
static async Task Main(string[] args)
31-
{
32-
await MakeInternalCall("https://localhost:5001");
33-
try
34-
{
35-
await MakeInternalCall("http://localhost:5000");
36-
Debug.Fail("Expected error.");
37-
}
38-
catch (RpcException ex)
39-
{
40-
Console.WriteLine(ex.Status.StatusCode);
41-
}
27+
await MakeInternalCall("http://localhost:5000");
28+
Debug.Fail("Expected error.");
29+
}
30+
catch (RpcException ex)
31+
{
32+
Console.WriteLine(ex.Status.StatusCode);
33+
}
4234

43-
await MakeExternalCall("http://localhost:5000");
44-
try
45-
{
46-
await MakeExternalCall("https://localhost:5001");
47-
Debug.Fail("Expected error.");
48-
}
49-
catch (RpcException ex)
50-
{
51-
Console.WriteLine(ex.Status.StatusCode);
52-
}
35+
await MakeExternalCall("http://localhost:5000");
36+
try
37+
{
38+
await MakeExternalCall("https://localhost:5001");
39+
Debug.Fail("Expected error.");
40+
}
41+
catch (RpcException ex)
42+
{
43+
Console.WriteLine(ex.Status.StatusCode);
44+
}
5345

54-
Console.WriteLine("Shutting down");
55-
Console.WriteLine("Press any key to exit...");
56-
Console.ReadKey();
57-
}
46+
Console.WriteLine("Shutting down");
47+
Console.WriteLine("Press any key to exit...");
48+
Console.ReadKey();
5849

59-
private static async Task MakeInternalCall(string address)
60-
{
61-
var channel = GrpcChannel.ForAddress(address);
62-
var client = new Internal.InternalClient(channel);
50+
static async Task MakeInternalCall(string address)
51+
{
52+
var channel = GrpcChannel.ForAddress(address);
53+
var client = new Internal.InternalClient(channel);
6354

64-
var reply = await client.SayHelloAsync(new InternalRequest { Name = "InternalClient" });
65-
Console.WriteLine("Greeting: " + reply.Message);
66-
}
55+
var reply = await client.SayHelloAsync(new InternalRequest { Name = "InternalClient" });
56+
Console.WriteLine("Greeting: " + reply.Message);
57+
}
6758

68-
private static async Task MakeExternalCall(string address)
69-
{
70-
var channel = GrpcChannel.ForAddress(address);
71-
var client = new External.ExternalClient(channel);
59+
static async Task MakeExternalCall(string address)
60+
{
61+
var channel = GrpcChannel.ForAddress(address);
62+
var client = new External.ExternalClient(channel);
7263

73-
var reply = await client.SayHelloAsync(new ExternalRequest { Name = "ExternalClient" });
74-
Console.WriteLine("Greeting: " + reply.Message);
75-
}
76-
}
64+
var reply = await client.SayHelloAsync(new ExternalRequest { Name = "ExternalClient" });
65+
Console.WriteLine("Greeting: " + reply.Message);
7766
}

Diff for: examples/Locator/Server/Program.cs

+12-22
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,18 @@
1616

1717
#endregion
1818

19-
using Microsoft.AspNetCore.Hosting;
20-
using Microsoft.Extensions.Hosting;
19+
using Server;
2120

22-
namespace Server
21+
var builder = WebApplication.CreateBuilder(args);
22+
builder.Services.AddGrpc();
23+
builder.WebHost.ConfigureKestrel(options =>
2324
{
24-
public class Program
25-
{
26-
public static void Main(string[] args)
27-
{
28-
CreateHostBuilder(args).Build().Run();
29-
}
25+
options.ListenLocalhost(5000);
26+
options.ListenLocalhost(5001, listener => listener.UseHttps());
27+
});
3028

31-
public static IHostBuilder CreateHostBuilder(string[] args) =>
32-
Host.CreateDefaultBuilder(args)
33-
.ConfigureWebHostDefaults(webBuilder =>
34-
{
35-
webBuilder.UseStartup<Startup>();
36-
webBuilder.ConfigureKestrel(options =>
37-
{
38-
options.ListenLocalhost(5000);
39-
options.ListenLocalhost(5001, listener => listener.UseHttps());
40-
});
41-
});
42-
}
43-
}
29+
var app = builder.Build();
30+
app.MapGrpcService<InternalService>().RequireHost("*:5001");
31+
app.MapGrpcService<ExternalService>().RequireHost("*:5000");
32+
33+
app.Run();

Diff for: examples/Locator/Server/Startup.cs

-49
This file was deleted.

Diff for: examples/Mailer/Client/Program.cs

+43-54
Original file line numberDiff line numberDiff line change
@@ -16,72 +16,61 @@
1616

1717
#endregion
1818

19-
using System;
20-
using System.Threading.Tasks;
2119
using Grpc.Core;
2220
using Grpc.Net.Client;
2321
using Mail;
2422

25-
namespace Client
26-
{
27-
public class Program
28-
{
29-
static async Task Main(string[] args)
30-
{
31-
var mailboxName = GetMailboxName(args);
23+
var mailboxName = GetMailboxName(args);
3224

33-
Console.WriteLine($"Creating client to mailbox '{mailboxName}'");
34-
Console.WriteLine();
35-
36-
var channel = GrpcChannel.ForAddress("https://localhost:5001");
37-
var client = new Mailer.MailerClient(channel);
25+
Console.WriteLine($"Creating client to mailbox '{mailboxName}'");
26+
Console.WriteLine();
3827

39-
Console.WriteLine("Client created");
40-
Console.WriteLine("Press escape to disconnect. Press any other key to forward mail.");
28+
var channel = GrpcChannel.ForAddress("https://localhost:5001");
29+
var client = new Mailer.MailerClient(channel);
4130

42-
using (var call = client.Mailbox(headers: new Metadata { new Metadata.Entry("mailbox-name", mailboxName) }))
43-
{
44-
var responseTask = Task.Run(async () =>
45-
{
46-
await foreach (var message in call.ResponseStream.ReadAllAsync())
47-
{
48-
Console.ForegroundColor = message.Reason == MailboxMessage.Types.Reason.Received ? ConsoleColor.White : ConsoleColor.Green;
49-
Console.WriteLine();
50-
Console.WriteLine(message.Reason == MailboxMessage.Types.Reason.Received ? "Mail received" : "Mail forwarded");
51-
Console.WriteLine($"New mail: {message.New}, Forwarded mail: {message.Forwarded}");
52-
Console.ResetColor();
53-
}
54-
});
31+
Console.WriteLine("Client created");
32+
Console.WriteLine("Press escape to disconnect. Press any other key to forward mail.");
5533

56-
while (true)
57-
{
58-
var result = Console.ReadKey(intercept: true);
59-
if (result.Key == ConsoleKey.Escape)
60-
{
61-
break;
62-
}
34+
using (var call = client.Mailbox(headers: new Metadata { new Metadata.Entry("mailbox-name", mailboxName) }))
35+
{
36+
var responseTask = Task.Run(async () =>
37+
{
38+
await foreach (var message in call.ResponseStream.ReadAllAsync())
39+
{
40+
Console.ForegroundColor = message.Reason == MailboxMessage.Types.Reason.Received ? ConsoleColor.White : ConsoleColor.Green;
41+
Console.WriteLine();
42+
Console.WriteLine(message.Reason == MailboxMessage.Types.Reason.Received ? "Mail received" : "Mail forwarded");
43+
Console.WriteLine($"New mail: {message.New}, Forwarded mail: {message.Forwarded}");
44+
Console.ResetColor();
45+
}
46+
});
6347

64-
await call.RequestStream.WriteAsync(new ForwardMailMessage());
65-
}
48+
while (true)
49+
{
50+
var result = Console.ReadKey(intercept: true);
51+
if (result.Key == ConsoleKey.Escape)
52+
{
53+
break;
54+
}
6655

67-
Console.WriteLine("Disconnecting");
68-
await call.RequestStream.CompleteAsync();
69-
await responseTask;
70-
}
56+
await call.RequestStream.WriteAsync(new ForwardMailMessage());
57+
}
7158

72-
Console.WriteLine("Disconnected. Press any key to exit.");
73-
Console.ReadKey();
74-
}
59+
Console.WriteLine("Disconnecting");
60+
await call.RequestStream.CompleteAsync();
61+
await responseTask;
62+
}
7563

76-
private static string GetMailboxName(string[] args)
77-
{
78-
if (args.Length < 1)
79-
{
80-
Console.WriteLine("No mailbox name provided. Using default name. Usage: dotnet run <name>.");
81-
return "DefaultMailbox";
82-
}
64+
Console.WriteLine("Disconnected. Press any key to exit.");
65+
Console.ReadKey();
8366

84-
return args[0];
85-
}
67+
static string GetMailboxName(string[] args)
68+
{
69+
if (args.Length < 1)
70+
{
71+
Console.WriteLine("No mailbox name provided. Using default name. Usage: dotnet run <name>.");
72+
return "DefaultMailbox";
8673
}
74+
75+
return args[0];
8776
}

Diff for: examples/Mailer/Server/Program.cs

+8-18
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,13 @@
1616

1717
#endregion
1818

19-
using Microsoft.AspNetCore.Hosting;
20-
using Microsoft.Extensions.Hosting;
19+
using Server;
2120

22-
namespace Server
23-
{
24-
public class Program
25-
{
26-
public static void Main(string[] args)
27-
{
28-
CreateHostBuilder(args).Build().Run();
29-
}
21+
var builder = WebApplication.CreateBuilder(args);
22+
builder.Services.AddGrpc();
23+
builder.Services.AddSingleton<MailQueueRepository>();
3024

31-
public static IHostBuilder CreateHostBuilder(string[] args) =>
32-
Host.CreateDefaultBuilder(args)
33-
.ConfigureWebHostDefaults(webBuilder =>
34-
{
35-
webBuilder.UseStartup<Startup>();
36-
});
37-
}
38-
}
25+
var app = builder.Build();
26+
app.MapGrpcService<MailerService>();
27+
28+
app.Run();

Diff for: examples/Mailer/Server/Startup.cs

-49
This file was deleted.

0 commit comments

Comments
 (0)