Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change examples to use WebApplication V3 #1878

Merged
merged 2 commits into from
Sep 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 35 additions & 46 deletions examples/Locator/Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,62 +16,51 @@

#endregion

using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Greet;
using Grpc.Core;
using Grpc.Net.Client;

namespace Client
await MakeInternalCall("https://localhost:5001");
try
{
public class Program
{
static async Task Main(string[] args)
{
await MakeInternalCall("https://localhost:5001");
try
{
await MakeInternalCall("http://localhost:5000");
Debug.Fail("Expected error.");
}
catch (RpcException ex)
{
Console.WriteLine(ex.Status.StatusCode);
}
await MakeInternalCall("http://localhost:5000");
Debug.Fail("Expected error.");
}
catch (RpcException ex)
{
Console.WriteLine(ex.Status.StatusCode);
}

await MakeExternalCall("http://localhost:5000");
try
{
await MakeExternalCall("https://localhost:5001");
Debug.Fail("Expected error.");
}
catch (RpcException ex)
{
Console.WriteLine(ex.Status.StatusCode);
}
await MakeExternalCall("http://localhost:5000");
try
{
await MakeExternalCall("https://localhost:5001");
Debug.Fail("Expected error.");
}
catch (RpcException ex)
{
Console.WriteLine(ex.Status.StatusCode);
}

Console.WriteLine("Shutting down");
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
Console.WriteLine("Shutting down");
Console.WriteLine("Press any key to exit...");
Console.ReadKey();

private static async Task MakeInternalCall(string address)
{
var channel = GrpcChannel.ForAddress(address);
var client = new Internal.InternalClient(channel);
static async Task MakeInternalCall(string address)
{
var channel = GrpcChannel.ForAddress(address);
var client = new Internal.InternalClient(channel);

var reply = await client.SayHelloAsync(new InternalRequest { Name = "InternalClient" });
Console.WriteLine("Greeting: " + reply.Message);
}
var reply = await client.SayHelloAsync(new InternalRequest { Name = "InternalClient" });
Console.WriteLine("Greeting: " + reply.Message);
}

private static async Task MakeExternalCall(string address)
{
var channel = GrpcChannel.ForAddress(address);
var client = new External.ExternalClient(channel);
static async Task MakeExternalCall(string address)
{
var channel = GrpcChannel.ForAddress(address);
var client = new External.ExternalClient(channel);

var reply = await client.SayHelloAsync(new ExternalRequest { Name = "ExternalClient" });
Console.WriteLine("Greeting: " + reply.Message);
}
}
var reply = await client.SayHelloAsync(new ExternalRequest { Name = "ExternalClient" });
Console.WriteLine("Greeting: " + reply.Message);
}
34 changes: 12 additions & 22 deletions examples/Locator/Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,18 @@

#endregion

using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Server;

namespace Server
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddGrpc();
builder.WebHost.ConfigureKestrel(options =>
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
options.ListenLocalhost(5000);
options.ListenLocalhost(5001, listener => listener.UseHttps());
});

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
webBuilder.ConfigureKestrel(options =>
{
options.ListenLocalhost(5000);
options.ListenLocalhost(5001, listener => listener.UseHttps());
});
});
}
}
var app = builder.Build();
app.MapGrpcService<InternalService>().RequireHost("*:5001");
app.MapGrpcService<ExternalService>().RequireHost("*:5000");

app.Run();
49 changes: 0 additions & 49 deletions examples/Locator/Server/Startup.cs

This file was deleted.

97 changes: 43 additions & 54 deletions examples/Mailer/Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,72 +16,61 @@

#endregion

using System;
using System.Threading.Tasks;
using Grpc.Core;
using Grpc.Net.Client;
using Mail;

namespace Client
{
public class Program
{
static async Task Main(string[] args)
{
var mailboxName = GetMailboxName(args);
var mailboxName = GetMailboxName(args);

Console.WriteLine($"Creating client to mailbox '{mailboxName}'");
Console.WriteLine();

var channel = GrpcChannel.ForAddress("https://localhost:5001");
var client = new Mailer.MailerClient(channel);
Console.WriteLine($"Creating client to mailbox '{mailboxName}'");
Console.WriteLine();

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

using (var call = client.Mailbox(headers: new Metadata { new Metadata.Entry("mailbox-name", mailboxName) }))
{
var responseTask = Task.Run(async () =>
{
await foreach (var message in call.ResponseStream.ReadAllAsync())
{
Console.ForegroundColor = message.Reason == MailboxMessage.Types.Reason.Received ? ConsoleColor.White : ConsoleColor.Green;
Console.WriteLine();
Console.WriteLine(message.Reason == MailboxMessage.Types.Reason.Received ? "Mail received" : "Mail forwarded");
Console.WriteLine($"New mail: {message.New}, Forwarded mail: {message.Forwarded}");
Console.ResetColor();
}
});
Console.WriteLine("Client created");
Console.WriteLine("Press escape to disconnect. Press any other key to forward mail.");

while (true)
{
var result = Console.ReadKey(intercept: true);
if (result.Key == ConsoleKey.Escape)
{
break;
}
using (var call = client.Mailbox(headers: new Metadata { new Metadata.Entry("mailbox-name", mailboxName) }))
{
var responseTask = Task.Run(async () =>
{
await foreach (var message in call.ResponseStream.ReadAllAsync())
{
Console.ForegroundColor = message.Reason == MailboxMessage.Types.Reason.Received ? ConsoleColor.White : ConsoleColor.Green;
Console.WriteLine();
Console.WriteLine(message.Reason == MailboxMessage.Types.Reason.Received ? "Mail received" : "Mail forwarded");
Console.WriteLine($"New mail: {message.New}, Forwarded mail: {message.Forwarded}");
Console.ResetColor();
}
});

await call.RequestStream.WriteAsync(new ForwardMailMessage());
}
while (true)
{
var result = Console.ReadKey(intercept: true);
if (result.Key == ConsoleKey.Escape)
{
break;
}

Console.WriteLine("Disconnecting");
await call.RequestStream.CompleteAsync();
await responseTask;
}
await call.RequestStream.WriteAsync(new ForwardMailMessage());
}

Console.WriteLine("Disconnected. Press any key to exit.");
Console.ReadKey();
}
Console.WriteLine("Disconnecting");
await call.RequestStream.CompleteAsync();
await responseTask;
}

private static string GetMailboxName(string[] args)
{
if (args.Length < 1)
{
Console.WriteLine("No mailbox name provided. Using default name. Usage: dotnet run <name>.");
return "DefaultMailbox";
}
Console.WriteLine("Disconnected. Press any key to exit.");
Console.ReadKey();

return args[0];
}
static string GetMailboxName(string[] args)
{
if (args.Length < 1)
{
Console.WriteLine("No mailbox name provided. Using default name. Usage: dotnet run <name>.");
return "DefaultMailbox";
}

return args[0];
}
26 changes: 8 additions & 18 deletions examples/Mailer/Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,13 @@

#endregion

using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Server;

namespace Server
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddGrpc();
builder.Services.AddSingleton<MailQueueRepository>();

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
var app = builder.Build();
app.MapGrpcService<MailerService>();

app.Run();
49 changes: 0 additions & 49 deletions examples/Mailer/Server/Startup.cs

This file was deleted.

Loading