mirror of https://github.com/dapr/dotnet-sdk.git
moving aspnetcore sample to its own folder (#95)
* moving aspnetcore sample to its own app * Fixing path to project refs
This commit is contained in:
parent
a9e3f78f1b
commit
5c46ac34da
|
|
@ -14,6 +14,7 @@
|
|||
|
||||
# launchSettings.json for samples and tests
|
||||
/samples/**/Properties/launchSettings.json
|
||||
/samples/**/appSettings.Development.json
|
||||
/test/**/Properties/launchSettings.json
|
||||
|
||||
# MSBuild's log file
|
||||
|
|
|
|||
4
code.sln
4
code.sln
|
|
@ -17,9 +17,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Dapr.AspNetCore", "src\Dapr
|
|||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{B2DB41EE-45F5-447B-95E8-38E1E8B70C4E}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ControllerSample", "samples\ControllerSample\ControllerSample.csproj", "{2B015C86-32FB-4178-8F8A-26749B6FCE11}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ControllerSample", "samples\AspNetCore\ControllerSample\ControllerSample.csproj", "{2B015C86-32FB-4178-8F8A-26749B6FCE11}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RoutingSample", "samples\RoutingSample\RoutingSample.csproj", "{1FBDE4EF-D8B0-4BFE-B393-3CA4FE1944A2}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RoutingSample", "samples\AspNetCore\RoutingSample\RoutingSample.csproj", "{1FBDE4EF-D8B0-4BFE-B393-3CA4FE1944A2}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{DD020B34-460F-455F-8D17-CF4A949F100B}"
|
||||
EndProject
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\Dapr.AspNetCore\Dapr.AspNetCore.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\src\Dapr.AspNetCore\Dapr.AspNetCore.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
@ -1,73 +1,73 @@
|
|||
// ------------------------------------------------------------
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
// ------------------------------------------------------------
|
||||
|
||||
namespace ControllerSample.Controllers
|
||||
{
|
||||
using System.Threading.Tasks;
|
||||
using Dapr;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
/// <summary>
|
||||
/// Sample showing Dapr integration with controller.
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
public class SampleController : ControllerBase
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the account information as specified by the id.
|
||||
/// </summary>
|
||||
/// <param name="account">Account information for the id from Dapr state store.</param>
|
||||
/// <returns>Account information.</returns>
|
||||
[HttpGet("{account}")]
|
||||
public ActionResult<Account> Get(StateEntry<Account> account)
|
||||
{
|
||||
if (account.Value is null)
|
||||
{
|
||||
return this.NotFound();
|
||||
}
|
||||
|
||||
return account.Value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Method for depositing to account as psecified in transaction.
|
||||
/// </summary>
|
||||
/// <param name="transaction">Transaction info.</param>
|
||||
/// <param name="stateClient">State client to interact with dapr runtime.</param>
|
||||
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>
|
||||
[Topic("deposit")]
|
||||
[HttpPost("deposit")]
|
||||
public async Task<ActionResult<Account>> Deposit(Transaction transaction, [FromServices] StateClient stateClient)
|
||||
{
|
||||
var state = await stateClient.GetStateEntryAsync<Account>(transaction.Id);
|
||||
state.Value ??= new Account() { Id = transaction.Id, };
|
||||
state.Value.Balance += transaction.Amount;
|
||||
await state.SaveAsync();
|
||||
return state.Value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Method for withdrawing from account as specified in transaction.
|
||||
/// </summary>
|
||||
/// <param name="transaction">Transaction info.</param>
|
||||
/// <param name="stateClient">State client to interact with dapr runtime.</param>
|
||||
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>
|
||||
[Topic("withdraw")]
|
||||
[HttpPost("withdraw")]
|
||||
public async Task<ActionResult<Account>> Withdraw(Transaction transaction, [FromServices] StateClient stateClient)
|
||||
{
|
||||
var state = await stateClient.GetStateEntryAsync<Account>(transaction.Id);
|
||||
|
||||
if (state.Value == null)
|
||||
{
|
||||
return this.NotFound();
|
||||
}
|
||||
|
||||
state.Value.Balance -= transaction.Amount;
|
||||
await state.SaveAsync();
|
||||
return state.Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
// ------------------------------------------------------------
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
// ------------------------------------------------------------
|
||||
|
||||
namespace ControllerSample.Controllers
|
||||
{
|
||||
using System.Threading.Tasks;
|
||||
using Dapr;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
/// <summary>
|
||||
/// Sample showing Dapr integration with controller.
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
public class SampleController : ControllerBase
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the account information as specified by the id.
|
||||
/// </summary>
|
||||
/// <param name="account">Account information for the id from Dapr state store.</param>
|
||||
/// <returns>Account information.</returns>
|
||||
[HttpGet("{account}")]
|
||||
public ActionResult<Account> Get(StateEntry<Account> account)
|
||||
{
|
||||
if (account.Value is null)
|
||||
{
|
||||
return this.NotFound();
|
||||
}
|
||||
|
||||
return account.Value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Method for depositing to account as psecified in transaction.
|
||||
/// </summary>
|
||||
/// <param name="transaction">Transaction info.</param>
|
||||
/// <param name="stateClient">State client to interact with dapr runtime.</param>
|
||||
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>
|
||||
[Topic("deposit")]
|
||||
[HttpPost("deposit")]
|
||||
public async Task<ActionResult<Account>> Deposit(Transaction transaction, [FromServices] StateClient stateClient)
|
||||
{
|
||||
var state = await stateClient.GetStateEntryAsync<Account>(transaction.Id);
|
||||
state.Value ??= new Account() { Id = transaction.Id, };
|
||||
state.Value.Balance += transaction.Amount;
|
||||
await state.SaveAsync();
|
||||
return state.Value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Method for withdrawing from account as specified in transaction.
|
||||
/// </summary>
|
||||
/// <param name="transaction">Transaction info.</param>
|
||||
/// <param name="stateClient">State client to interact with dapr runtime.</param>
|
||||
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>
|
||||
[Topic("withdraw")]
|
||||
[HttpPost("withdraw")]
|
||||
public async Task<ActionResult<Account>> Withdraw(Transaction transaction, [FromServices] StateClient stateClient)
|
||||
{
|
||||
var state = await stateClient.GetStateEntryAsync<Account>(transaction.Id);
|
||||
|
||||
if (state.Value == null)
|
||||
{
|
||||
return this.NotFound();
|
||||
}
|
||||
|
||||
state.Value.Balance -= transaction.Amount;
|
||||
await state.SaveAsync();
|
||||
return state.Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,43 +1,43 @@
|
|||
// ------------------------------------------------------------
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
// ------------------------------------------------------------
|
||||
|
||||
namespace ControllerSample
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
/// <summary>
|
||||
/// Controller Sample.
|
||||
/// </summary>
|
||||
public class Program
|
||||
{
|
||||
/// <summary>
|
||||
/// Main for Controller Sample.
|
||||
/// </summary>
|
||||
/// <param name="args">Arguments.</param>
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
CreateHostBuilder(args).Build().Run();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates WebHost Builder.
|
||||
/// </summary>
|
||||
/// <param name="args">Arguments.</param>
|
||||
/// <returns>Returns IHostbuilder.</returns>
|
||||
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
||||
Host.CreateDefaultBuilder(args)
|
||||
.ConfigureWebHostDefaults(webBuilder =>
|
||||
{
|
||||
webBuilder.UseStartup<Startup>();
|
||||
});
|
||||
}
|
||||
}
|
||||
// ------------------------------------------------------------
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
// ------------------------------------------------------------
|
||||
|
||||
namespace ControllerSample
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
/// <summary>
|
||||
/// Controller Sample.
|
||||
/// </summary>
|
||||
public class Program
|
||||
{
|
||||
/// <summary>
|
||||
/// Main for Controller Sample.
|
||||
/// </summary>
|
||||
/// <param name="args">Arguments.</param>
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
CreateHostBuilder(args).Build().Run();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates WebHost Builder.
|
||||
/// </summary>
|
||||
/// <param name="args">Arguments.</param>
|
||||
/// <returns>Returns IHostbuilder.</returns>
|
||||
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
||||
Host.CreateDefaultBuilder(args)
|
||||
.ConfigureWebHostDefaults(webBuilder =>
|
||||
{
|
||||
webBuilder.UseStartup<Startup>();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -1,74 +1,74 @@
|
|||
// ------------------------------------------------------------
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
// ------------------------------------------------------------
|
||||
|
||||
namespace ControllerSample
|
||||
{
|
||||
using System.Text.Json;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
/// <summary>
|
||||
/// Startup class.
|
||||
/// </summary>
|
||||
public class Startup
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Startup"/> class.
|
||||
/// </summary>
|
||||
/// <param name="configuration">Configuration.</param>
|
||||
public Startup(IConfiguration configuration)
|
||||
{
|
||||
this.Configuration = configuration;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the configuration.
|
||||
/// </summary>
|
||||
public IConfiguration Configuration { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Configures Services.
|
||||
/// </summary>
|
||||
/// <param name="services">Service Collection.</param>
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddControllers().AddDapr();
|
||||
|
||||
services.AddSingleton(new JsonSerializerOptions()
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
PropertyNameCaseInsensitive = true,
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures Application Builder and WebHost environment.
|
||||
/// </summary>
|
||||
/// <param name="app">Application builder.</param>
|
||||
/// <param name="env">Webhost environment.</param>
|
||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||
{
|
||||
if (env.IsDevelopment())
|
||||
{
|
||||
app.UseDeveloperExceptionPage();
|
||||
}
|
||||
|
||||
app.UseRouting();
|
||||
|
||||
app.UseCloudEvents();
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
app.UseEndpoints(endpoints =>
|
||||
{
|
||||
endpoints.MapSubscribeHandler();
|
||||
endpoints.MapControllers();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
// ------------------------------------------------------------
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
// ------------------------------------------------------------
|
||||
|
||||
namespace ControllerSample
|
||||
{
|
||||
using System.Text.Json;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
/// <summary>
|
||||
/// Startup class.
|
||||
/// </summary>
|
||||
public class Startup
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Startup"/> class.
|
||||
/// </summary>
|
||||
/// <param name="configuration">Configuration.</param>
|
||||
public Startup(IConfiguration configuration)
|
||||
{
|
||||
this.Configuration = configuration;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the configuration.
|
||||
/// </summary>
|
||||
public IConfiguration Configuration { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Configures Services.
|
||||
/// </summary>
|
||||
/// <param name="services">Service Collection.</param>
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddControllers().AddDapr();
|
||||
|
||||
services.AddSingleton(new JsonSerializerOptions()
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
PropertyNameCaseInsensitive = true,
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures Application Builder and WebHost environment.
|
||||
/// </summary>
|
||||
/// <param name="app">Application builder.</param>
|
||||
/// <param name="env">Webhost environment.</param>
|
||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||
{
|
||||
if (env.IsDevelopment())
|
||||
{
|
||||
app.UseDeveloperExceptionPage();
|
||||
}
|
||||
|
||||
app.UseRouting();
|
||||
|
||||
app.UseCloudEvents();
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
app.UseEndpoints(endpoints =>
|
||||
{
|
||||
endpoints.MapSubscribeHandler();
|
||||
endpoints.MapControllers();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,10 +1,10 @@
|
|||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft": "Warning",
|
||||
"Microsoft.Hosting.Lifetime": "Information"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft": "Warning",
|
||||
"Microsoft.Hosting.Lifetime": "Information"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
|
|
@ -1,43 +1,43 @@
|
|||
// ------------------------------------------------------------
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
// ------------------------------------------------------------
|
||||
|
||||
namespace RoutingSample
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
/// <summary>
|
||||
/// Controller Sample.
|
||||
/// </summary>
|
||||
public class Program
|
||||
{
|
||||
/// <summary>
|
||||
/// Main for Controller Sample.
|
||||
/// </summary>
|
||||
/// <param name="args">Arguments.</param>
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
CreateHostBuilder(args).Build().Run();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates WebHost Builder.
|
||||
/// </summary>
|
||||
/// <param name="args">Arguments.</param>
|
||||
/// <returns>Returns IHostbuilder.</returns>
|
||||
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
||||
Host.CreateDefaultBuilder(args)
|
||||
.ConfigureWebHostDefaults(webBuilder =>
|
||||
{
|
||||
webBuilder.UseStartup<Startup>();
|
||||
});
|
||||
}
|
||||
}
|
||||
// ------------------------------------------------------------
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
// ------------------------------------------------------------
|
||||
|
||||
namespace RoutingSample
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
/// <summary>
|
||||
/// Controller Sample.
|
||||
/// </summary>
|
||||
public class Program
|
||||
{
|
||||
/// <summary>
|
||||
/// Main for Controller Sample.
|
||||
/// </summary>
|
||||
/// <param name="args">Arguments.</param>
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
CreateHostBuilder(args).Build().Run();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates WebHost Builder.
|
||||
/// </summary>
|
||||
/// <param name="args">Arguments.</param>
|
||||
/// <returns>Returns IHostbuilder.</returns>
|
||||
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
||||
Host.CreateDefaultBuilder(args)
|
||||
.ConfigureWebHostDefaults(webBuilder =>
|
||||
{
|
||||
webBuilder.UseStartup<Startup>();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -1,11 +1,11 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\Dapr.AspNetCore\Dapr.AspNetCore.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\src\Dapr.AspNetCore\Dapr.AspNetCore.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
@ -1,144 +1,144 @@
|
|||
// ------------------------------------------------------------
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
// ------------------------------------------------------------
|
||||
|
||||
namespace RoutingSample
|
||||
{
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using Dapr;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
/// <summary>
|
||||
/// Startup class.
|
||||
/// </summary>
|
||||
public class Startup
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Startup"/> class.
|
||||
/// </summary>
|
||||
/// <param name="configuration">Configuration.</param>
|
||||
public Startup(IConfiguration configuration)
|
||||
{
|
||||
this.Configuration = configuration;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the configuration.
|
||||
/// </summary>
|
||||
public IConfiguration Configuration { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Configures Services.
|
||||
/// </summary>
|
||||
/// <param name="services">Service Collection.</param>
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddDaprClient();
|
||||
|
||||
services.AddSingleton(new JsonSerializerOptions()
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
PropertyNameCaseInsensitive = true,
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures Application Builder and WebHost environment.
|
||||
/// </summary>
|
||||
/// <param name="app">Application builder.</param>
|
||||
/// <param name="env">Webhost environment.</param>
|
||||
/// <param name="serializerOptions">Options for json serialization.</param>
|
||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, JsonSerializerOptions serializerOptions)
|
||||
{
|
||||
if (env.IsDevelopment())
|
||||
{
|
||||
app.UseDeveloperExceptionPage();
|
||||
}
|
||||
|
||||
app.UseRouting();
|
||||
|
||||
app.UseCloudEvents();
|
||||
|
||||
app.UseEndpoints(endpoints =>
|
||||
{
|
||||
endpoints.MapSubscribeHandler();
|
||||
|
||||
endpoints.MapGet("{id}", Balance);
|
||||
endpoints.MapPost("deposit", Deposit).WithTopic("deposit");
|
||||
endpoints.MapPost("withdraw", Withdraw).WithTopic("withdraw");
|
||||
});
|
||||
|
||||
async Task Balance(HttpContext context)
|
||||
{
|
||||
var client = context.RequestServices.GetRequiredService<StateClient>();
|
||||
|
||||
var id = (string)context.Request.RouteValues["id"];
|
||||
var account = await client.GetStateAsync<Account>(id);
|
||||
if (account == null)
|
||||
{
|
||||
context.Response.StatusCode = 404;
|
||||
return;
|
||||
}
|
||||
|
||||
context.Response.ContentType = "application/json";
|
||||
await JsonSerializer.SerializeAsync(context.Response.Body, account, serializerOptions);
|
||||
}
|
||||
|
||||
async Task Deposit(HttpContext context)
|
||||
{
|
||||
var client = context.RequestServices.GetRequiredService<StateClient>();
|
||||
|
||||
var transaction = await JsonSerializer.DeserializeAsync<Transaction>(context.Request.Body, serializerOptions);
|
||||
var account = await client.GetStateAsync<Account>(transaction.Id);
|
||||
if (account == null)
|
||||
{
|
||||
account = new Account() { Id = transaction.Id, };
|
||||
}
|
||||
|
||||
if (transaction.Amount < 0m)
|
||||
{
|
||||
context.Response.StatusCode = 400;
|
||||
return;
|
||||
}
|
||||
|
||||
account.Balance += transaction.Amount;
|
||||
await client.SaveStateAsync(transaction.Id, account);
|
||||
|
||||
context.Response.ContentType = "application/json";
|
||||
await JsonSerializer.SerializeAsync(context.Response.Body, account, serializerOptions);
|
||||
}
|
||||
|
||||
async Task Withdraw(HttpContext context)
|
||||
{
|
||||
var client = context.RequestServices.GetRequiredService<StateClient>();
|
||||
|
||||
var transaction = await JsonSerializer.DeserializeAsync<Transaction>(context.Request.Body, serializerOptions);
|
||||
var account = await client.GetStateAsync<Account>(transaction.Id);
|
||||
if (account == null)
|
||||
{
|
||||
context.Response.StatusCode = 404;
|
||||
return;
|
||||
}
|
||||
|
||||
if (transaction.Amount < 0m)
|
||||
{
|
||||
context.Response.StatusCode = 400;
|
||||
return;
|
||||
}
|
||||
|
||||
account.Balance -= transaction.Amount;
|
||||
await client.SaveStateAsync(transaction.Id, account);
|
||||
|
||||
context.Response.ContentType = "application/json";
|
||||
await JsonSerializer.SerializeAsync(context.Response.Body, account, serializerOptions);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// ------------------------------------------------------------
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
// ------------------------------------------------------------
|
||||
|
||||
namespace RoutingSample
|
||||
{
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using Dapr;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
/// <summary>
|
||||
/// Startup class.
|
||||
/// </summary>
|
||||
public class Startup
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Startup"/> class.
|
||||
/// </summary>
|
||||
/// <param name="configuration">Configuration.</param>
|
||||
public Startup(IConfiguration configuration)
|
||||
{
|
||||
this.Configuration = configuration;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the configuration.
|
||||
/// </summary>
|
||||
public IConfiguration Configuration { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Configures Services.
|
||||
/// </summary>
|
||||
/// <param name="services">Service Collection.</param>
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddDaprClient();
|
||||
|
||||
services.AddSingleton(new JsonSerializerOptions()
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
PropertyNameCaseInsensitive = true,
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures Application Builder and WebHost environment.
|
||||
/// </summary>
|
||||
/// <param name="app">Application builder.</param>
|
||||
/// <param name="env">Webhost environment.</param>
|
||||
/// <param name="serializerOptions">Options for json serialization.</param>
|
||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, JsonSerializerOptions serializerOptions)
|
||||
{
|
||||
if (env.IsDevelopment())
|
||||
{
|
||||
app.UseDeveloperExceptionPage();
|
||||
}
|
||||
|
||||
app.UseRouting();
|
||||
|
||||
app.UseCloudEvents();
|
||||
|
||||
app.UseEndpoints(endpoints =>
|
||||
{
|
||||
endpoints.MapSubscribeHandler();
|
||||
|
||||
endpoints.MapGet("{id}", Balance);
|
||||
endpoints.MapPost("deposit", Deposit).WithTopic("deposit");
|
||||
endpoints.MapPost("withdraw", Withdraw).WithTopic("withdraw");
|
||||
});
|
||||
|
||||
async Task Balance(HttpContext context)
|
||||
{
|
||||
var client = context.RequestServices.GetRequiredService<StateClient>();
|
||||
|
||||
var id = (string)context.Request.RouteValues["id"];
|
||||
var account = await client.GetStateAsync<Account>(id);
|
||||
if (account == null)
|
||||
{
|
||||
context.Response.StatusCode = 404;
|
||||
return;
|
||||
}
|
||||
|
||||
context.Response.ContentType = "application/json";
|
||||
await JsonSerializer.SerializeAsync(context.Response.Body, account, serializerOptions);
|
||||
}
|
||||
|
||||
async Task Deposit(HttpContext context)
|
||||
{
|
||||
var client = context.RequestServices.GetRequiredService<StateClient>();
|
||||
|
||||
var transaction = await JsonSerializer.DeserializeAsync<Transaction>(context.Request.Body, serializerOptions);
|
||||
var account = await client.GetStateAsync<Account>(transaction.Id);
|
||||
if (account == null)
|
||||
{
|
||||
account = new Account() { Id = transaction.Id, };
|
||||
}
|
||||
|
||||
if (transaction.Amount < 0m)
|
||||
{
|
||||
context.Response.StatusCode = 400;
|
||||
return;
|
||||
}
|
||||
|
||||
account.Balance += transaction.Amount;
|
||||
await client.SaveStateAsync(transaction.Id, account);
|
||||
|
||||
context.Response.ContentType = "application/json";
|
||||
await JsonSerializer.SerializeAsync(context.Response.Body, account, serializerOptions);
|
||||
}
|
||||
|
||||
async Task Withdraw(HttpContext context)
|
||||
{
|
||||
var client = context.RequestServices.GetRequiredService<StateClient>();
|
||||
|
||||
var transaction = await JsonSerializer.DeserializeAsync<Transaction>(context.Request.Body, serializerOptions);
|
||||
var account = await client.GetStateAsync<Account>(transaction.Id);
|
||||
if (account == null)
|
||||
{
|
||||
context.Response.StatusCode = 404;
|
||||
return;
|
||||
}
|
||||
|
||||
if (transaction.Amount < 0m)
|
||||
{
|
||||
context.Response.StatusCode = 400;
|
||||
return;
|
||||
}
|
||||
|
||||
account.Balance -= transaction.Amount;
|
||||
await client.SaveStateAsync(transaction.Id, account);
|
||||
|
||||
context.Response.ContentType = "application/json";
|
||||
await JsonSerializer.SerializeAsync(context.Response.Body, account, serializerOptions);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,10 +1,10 @@
|
|||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft": "Warning",
|
||||
"Microsoft.Hosting.Lifetime": "Information"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft": "Warning",
|
||||
"Microsoft.Hosting.Lifetime": "Information"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
|
|
@ -1,30 +0,0 @@
|
|||
{
|
||||
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:15298",
|
||||
"sslPort": 44311
|
||||
}
|
||||
},
|
||||
"profiles": {
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "api/values",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"ControllerSample": {
|
||||
"commandName": "Project",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "api/values",
|
||||
"applicationUrl": "https://localhost:5001;http://localhost:5000",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Debug",
|
||||
"System": "Information",
|
||||
"Microsoft": "Information"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,30 +0,0 @@
|
|||
{
|
||||
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:38427",
|
||||
"sslPort": 44370
|
||||
}
|
||||
},
|
||||
"profiles": {
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "api/values",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"RoutingSample": {
|
||||
"commandName": "Project",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "api/values",
|
||||
"applicationUrl": "https://localhost:5001;http://localhost:5000",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Debug",
|
||||
"System": "Information",
|
||||
"Microsoft": "Information"
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue