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

File scoped namespace for examples and leftovers #609

Merged
merged 1 commit into from
Aug 25, 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
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