Skip to content

Commit

Permalink
File scoped namespace for examples and leftovers (#609)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kielek authored Aug 25, 2022
1 parent 58cfd20 commit 54f22bb
Show file tree
Hide file tree
Showing 34 changed files with 4,361 additions and 4,401 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,38 +20,37 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace Examples.GrpcCore.AspNetCore.Controllers
namespace Examples.GrpcCore.AspNetCore.Controllers;

[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
private static readonly string[] Summaries = new[]
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching",
};
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching",
};

private readonly Echo.EchoClient echoClient;
private readonly Echo.EchoClient echoClient;

public WeatherForecastController(Echo.EchoClient echoClient)
{
this.echoClient = echoClient;
}
public WeatherForecastController(Echo.EchoClient echoClient)
{
this.echoClient = echoClient;
}

[HttpGet]
public async Task<IEnumerable<WeatherForecast>> Get()
{
var echoCall = this.echoClient.EchoAsync(new EchoRequest { Message = "Hello" });
var echoResponse = await echoCall.ResponseAsync.ConfigureAwait(false);
[HttpGet]
public async Task<IEnumerable<WeatherForecast>> Get()
{
var echoCall = this.echoClient.EchoAsync(new EchoRequest { Message = "Hello" });
var echoResponse = await echoCall.ResponseAsync.ConfigureAwait(false);

var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)],
})
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)],
})
.ToArray();
}
}
}
19 changes: 9 additions & 10 deletions examples/grpc.core/Examples.GrpcCore.AspNetCore/EchoService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,16 @@
using System.Threading.Tasks;
using Grpc.Core;

namespace Examples.GrpcCore.AspNetCore
namespace Examples.GrpcCore.AspNetCore;

/// <summary>
/// Simple implementation of the echo service.
/// </summary>
internal class EchoService : Echo.EchoBase
{
/// <summary>
/// Simple implementation of the echo service.
/// </summary>
internal class EchoService : Echo.EchoBase
/// <inheritdoc/>
public override Task<EchoResponse> Echo(EchoRequest request, ServerCallContext context)
{
/// <inheritdoc/>
public override Task<EchoResponse> Echo(EchoRequest request, ServerCallContext context)
{
return Task.FromResult(new EchoResponse { Message = request.Message });
}
return Task.FromResult(new EchoResponse { Message = request.Message });
}
}
33 changes: 16 additions & 17 deletions examples/grpc.core/Examples.GrpcCore.AspNetCore/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,23 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;

namespace Examples.GrpcCore.AspNetCore
{
public class Program
{
internal const int Port = 5000;
internal const int GrpcServicePort = 5001;
namespace Examples.GrpcCore.AspNetCore;

public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public class Program
{
internal const int Port = 5000;
internal const int GrpcServicePort = 5001;

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseUrls($"http://+:{Port}");
webBuilder.UseStartup<Startup>();
});
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseUrls($"http://+:{Port}");
webBuilder.UseStartup<Startup>();
});
}
129 changes: 64 additions & 65 deletions examples/grpc.core/Examples.GrpcCore.AspNetCore/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,92 +26,91 @@
using OpenTelemetry.Instrumentation.GrpcCore;
using OpenTelemetry.Trace;

namespace Examples.GrpcCore.AspNetCore
namespace Examples.GrpcCore.AspNetCore;

public class Startup
{
public class Startup
public Startup(IConfiguration configuration)
{
public Startup(IConfiguration configuration)
{
this.Configuration = configuration;
}
this.Configuration = configuration;
}

public IConfiguration Configuration { get; }
public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();

// Wire in otel
services.AddOpenTelemetryTracing(
(builder) => builder
.AddAspNetCoreInstrumentation()
.AddGrpcCoreInstrumentation()
.AddConsoleExporter());
// Wire in otel
services.AddOpenTelemetryTracing(
(builder) => builder
.AddAspNetCoreInstrumentation()
.AddGrpcCoreInstrumentation()
.AddConsoleExporter());

// We are running an in-process gRPC Core service.
services.AddHostedService<EchoGrpcHostedService>();
// We are running an in-process gRPC Core service.
services.AddHostedService<EchoGrpcHostedService>();

// Add a singleton for the gRPC client to our local service.
services.AddSingleton(provider =>
{
var channel = new Channel($"dns:localhost:{Program.GrpcServicePort}", ChannelCredentials.Insecure);
// Add a singleton for the gRPC client to our local service.
services.AddSingleton(provider =>
{
var channel = new Channel($"dns:localhost:{Program.GrpcServicePort}", ChannelCredentials.Insecure);

var callInvoker = channel.CreateCallInvoker()
.Intercept(new ClientTracingInterceptor(new ClientTracingInterceptorOptions()));
var callInvoker = channel.CreateCallInvoker()
.Intercept(new ClientTracingInterceptor(new ClientTracingInterceptorOptions()));

return new Echo.EchoClient(callInvoker);
});
}
return new Echo.EchoClient(callInvoker);
});
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseDeveloperExceptionPage();
}

app.UseRouting();
app.UseRouting();

app.UseAuthorization();
app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}

/// <summary>
/// A hosted service wrapper for an in-process gRPC Core service.
/// This gRPC service is instrumented using the server interceptor.
/// </summary>
private sealed class EchoGrpcHostedService : BackgroundService
/// <summary>
/// A hosted service wrapper for an in-process gRPC Core service.
/// This gRPC service is instrumented using the server interceptor.
/// </summary>
private sealed class EchoGrpcHostedService : BackgroundService
{
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
var serviceDefinition = Echo.BindService(new EchoService())
.Intercept(new ServerTracingInterceptor(new ServerTracingInterceptorOptions()));
var serviceDefinition = Echo.BindService(new EchoService())
.Intercept(new ServerTracingInterceptor(new ServerTracingInterceptorOptions()));

var server = new Server
{
Ports = { new ServerPort("localhost", Program.GrpcServicePort, ServerCredentials.Insecure) },
Services = { serviceDefinition },
};
var server = new Server
{
Ports = { new ServerPort("localhost", Program.GrpcServicePort, ServerCredentials.Insecure) },
Services = { serviceDefinition },
};

server.Start();
server.Start();

var tcs = new TaskCompletionSource<bool>();
var tcs = new TaskCompletionSource<bool>();

var tokenRegistration = stoppingToken.Register(
async () =>
{
await server.ShutdownAsync().ConfigureAwait(false);
tcs.SetResult(true);
});
var tokenRegistration = stoppingToken.Register(
async () =>
{
await server.ShutdownAsync().ConfigureAwait(false);
tcs.SetResult(true);
});

return tcs.Task.ContinueWith(antecedent => tokenRegistration.Dispose());
}
return tcs.Task.ContinueWith(antecedent => tokenRegistration.Dispose());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@

using System;

namespace Examples.GrpcCore.AspNetCore
namespace Examples.GrpcCore.AspNetCore;

public class WeatherForecast
{
public class WeatherForecast
{
public DateTime Date { get; set; }
public DateTime Date { get; set; }

public int TemperatureC { get; set; }
public int TemperatureC { get; set; }

public int TemperatureF => 32 + (int)(this.TemperatureC / 0.5556);
public int TemperatureF => 32 + (int)(this.TemperatureC / 0.5556);

public string Summary { get; set; }
}
public string Summary { get; set; }
}
14 changes: 6 additions & 8 deletions examples/owin/Controllers/TestController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,15 @@
// limitations under the License.
// </copyright>

using System;
using System.Web.Http;

namespace Examples.Owin.Controllers
namespace Examples.Owin.Controllers;

public class TestController : ApiController
{
public class TestController : ApiController
// GET api/test/{id}
public string Get(string id = null)
{
// GET api/test/{id}
public string Get(string id = null)
{
return $"id:{id}";
}
return $"id:{id}";
}
}
Loading

0 comments on commit 54f22bb

Please sign in to comment.