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

Add remote profile example based on httpclient #344

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
16 changes: 15 additions & 1 deletion MiniProfiler.sln
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Samples.Mvc5.EFCore", "samp
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MiniProfiler.Providers.MongoDB", "src\MiniProfiler.Providers.MongoDB\MiniProfiler.Providers.MongoDB.csproj", "{001EFFB2-E56C-4A63-9954-79D7AEB052DB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Misc", "samples\Misc\Misc.csproj", "{44075C61-13E9-4C78-B340-6045EA115433}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Misc", "samples\Misc\Misc.csproj", "{44075C61-13E9-4C78-B340-6045EA115433}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Samples.Remote.Api", "samples\Samples.Remote\Samples.Remote.Api\Samples.Remote.Api.csproj", "{3254616D-DF5E-4954-87C4-6AE37175D633}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Samples.Remote.Mvc", "samples\Samples.Remote\Samples.Remote.Mvc\Samples.Remote.Mvc.csproj", "{EEBA0F76-FA7B-4EA3-A264-7E71EC525A2F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -182,6 +186,14 @@ Global
{44075C61-13E9-4C78-B340-6045EA115433}.Debug|Any CPU.Build.0 = Debug|Any CPU
{44075C61-13E9-4C78-B340-6045EA115433}.Release|Any CPU.ActiveCfg = Release|Any CPU
{44075C61-13E9-4C78-B340-6045EA115433}.Release|Any CPU.Build.0 = Release|Any CPU
{3254616D-DF5E-4954-87C4-6AE37175D633}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3254616D-DF5E-4954-87C4-6AE37175D633}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3254616D-DF5E-4954-87C4-6AE37175D633}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3254616D-DF5E-4954-87C4-6AE37175D633}.Release|Any CPU.Build.0 = Release|Any CPU
{EEBA0F76-FA7B-4EA3-A264-7E71EC525A2F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EEBA0F76-FA7B-4EA3-A264-7E71EC525A2F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EEBA0F76-FA7B-4EA3-A264-7E71EC525A2F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EEBA0F76-FA7B-4EA3-A264-7E71EC525A2F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -211,6 +223,8 @@ Global
{44F5C82B-412E-485D-B349-BA9A008BD5AC} = {E0DA4035-4D64-4BB8-8EA1-42197DE62519}
{001EFFB2-E56C-4A63-9954-79D7AEB052DB} = {6A510DBF-E85F-4D2C-B8F7-006DA31B3418}
{44075C61-13E9-4C78-B340-6045EA115433} = {E0DA4035-4D64-4BB8-8EA1-42197DE62519}
{3254616D-DF5E-4954-87C4-6AE37175D633} = {E0DA4035-4D64-4BB8-8EA1-42197DE62519}
{EEBA0F76-FA7B-4EA3-A264-7E71EC525A2F} = {E0DA4035-4D64-4BB8-8EA1-42197DE62519}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9373F37A-A996-4545-A251-1902C8886E3F}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Samples.Remote.Api.Data;

namespace Samples.Remote.Api.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class SamplesController : ControllerBase
{
private readonly SampleContext _context;

public SamplesController(SampleContext context)
{
_context = context;
}

[HttpGet]
public async Task<ActionResult<IEnumerable<Sample>>> GetAll()
{
return Ok(await _context.Samples.ToListAsync());
}

[HttpGet("odd")]
public async Task<ActionResult<IEnumerable<Sample>>> GetOdd()
{
return Ok(await _context.Samples.Where(s => s.Id % 2 != 0).ToListAsync());
}
}
}
20 changes: 20 additions & 0 deletions samples/Samples.Remote/Samples.Remote.Api/Data/SampleContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Microsoft.EntityFrameworkCore;

namespace Samples.Remote.Api.Data
{
public class SampleContext : DbContext
{
public SampleContext(DbContextOptions<SampleContext> options)
: base(options)
{ }

public DbSet<Sample> Samples { get; set; }
}

public class Sample
{
public int Id { get; set; }

public string Name { get; set; }
}
}
17 changes: 17 additions & 0 deletions samples/Samples.Remote/Samples.Remote.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;

namespace Samples.Remote.Api
{
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:63227",
"sslPort": 44321
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "api/values",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Samples.Remote.Api": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "api/values",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\MiniProfiler.AspNetCore.Mvc\MiniProfiler.AspNetCore.Mvc.csproj" />
<ProjectReference Include="..\..\..\src\MiniProfiler.EntityFrameworkCore\MiniProfiler.EntityFrameworkCore.csproj" />
</ItemGroup>

</Project>
68 changes: 68 additions & 0 deletions samples/Samples.Remote/Samples.Remote.Api/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System.Linq;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Samples.Remote.Api.Data;
using StackExchange.Profiling;

namespace Samples.Remote.Api
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

// Register MiniProfiler
services.AddMiniProfiler()
.AddEntityFramework();

var connection = new SqliteConnection("DataSource=:memory:");
// The Sqlite in memory dB is alive till the connection is open, so for the sake of simplicity we're opening
// the connection here and keep it open forever, this should not be done in real world scenario
connection.Open();

services.AddDbContext<SampleContext>(options =>
{
options.UseSqlite(connection);
});
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}

app.UseMiniProfiler();
app.Use((ctx, next) =>
{
// Add the ID of the current miniprofiler session to the response headers
ctx.Response.Headers.Add("MiniProfiler-Remote-Id", MiniProfiler.Current.Id.ToString());

return next();
});

app.UseHttpsRedirection();
app.UseMvc();

// Initialize the dB - don try this at home!
using (var scope = app.ApplicationServices.CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService<SampleContext>();
context.Database.EnsureCreated();
context.Samples.AddRange(
Enumerable.Range(1, 10).Select(index => new Sample { Name = $"Smaple-{index}" }));
context.SaveChanges();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
8 changes: 8 additions & 0 deletions samples/Samples.Remote/Samples.Remote.Api/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*"
}
Loading