Skip to content

Commit

Permalink
Revamp Orleans shopping cart example | .NET 8 + Azure Cosmos DB for N…
Browse files Browse the repository at this point in the history
…oSQL (#6213)

* Floating package version numbers and .NET 8

* Format projects

* Fixed deprecation warnings for Mud icons

* Removed Startup.cs

* Revamped Program.cs to match Orleans quickstart

* Fix build error

* Remove container tools dependency

* Add permanent Dockerfile

* Updated readme

* Fix Markdownlint issues

* Switch to `8.*` package version

Co-authored-by: David Pine <[email protected]>

---------

Co-authored-by: David Pine <[email protected]>
  • Loading branch information
seesharprun and IEvangelist authored Jan 5, 2024
1 parent c3043d9 commit c04cd19
Show file tree
Hide file tree
Showing 31 changed files with 182 additions and 134 deletions.
25 changes: 25 additions & 0 deletions orleans/ShoppingCart/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
3 changes: 3 additions & 0 deletions orleans/ShoppingCart/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
##
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore

# Publishing package
*.zip

# User-specific files
*.suo
*.user
Expand Down
4 changes: 2 additions & 2 deletions orleans/ShoppingCart/Abstractions/CartItem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT License.

namespace Orleans.ShoppingCart.Abstractions;
Expand All @@ -12,4 +12,4 @@ public sealed record class CartItem(
[JsonIgnore]
public decimal TotalPrice =>
Math.Round(Quantity * Product.UnitPrice, 2);
}
}
2 changes: 1 addition & 1 deletion orleans/ShoppingCart/Abstractions/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT License.

global using System.Text.Json.Serialization;
global using Orleans.Concurrency;
global using System.Text.Json.Serialization;
6 changes: 3 additions & 3 deletions orleans/ShoppingCart/Abstractions/IInventoryGrain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
namespace Orleans.ShoppingCart.Abstractions;

public interface IInventoryGrain : IGrainWithStringKey
{
{
Task<HashSet<ProductDetails>> GetAllProductsAsync();

Task AddOrUpdateProductAsync(ProductDetails productDetails);

Task RemoveProductAsync(string productId);
}
4 changes: 2 additions & 2 deletions orleans/ShoppingCart/Abstractions/IShoppingCartGrain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public interface IShoppingCartGrain : IGrainWithStringKey
/// Removes the given <paramref name="product" /> from the shopping cart.
/// </summary>
Task RemoveItemAsync(ProductDetails product);

/// <summary>
/// Gets all the items in the shopping cart.
/// </summary>
Expand All @@ -30,4 +30,4 @@ public interface IShoppingCartGrain : IGrainWithStringKey
/// Removes all items from the shopping cart.
/// </summary>
Task EmptyCartAsync();
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Orleans.Sdk" Version="7.0.0" />
<PackageReference Include="Microsoft.Orleans.Sdk" Version="8.*" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion orleans/ShoppingCart/Abstractions/ProductDetails.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT License.

namespace Orleans.ShoppingCart.Abstractions;
Expand Down
2 changes: 1 addition & 1 deletion orleans/ShoppingCart/Grains/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

global using Orleans.Concurrency;
global using Orleans.Runtime;
global using Orleans.ShoppingCart.Abstractions;
global using Orleans.ShoppingCart.Abstractions;
2 changes: 1 addition & 1 deletion orleans/ShoppingCart/Grains/InventoryGrain.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT License.

using Orleans.Runtime;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Orleans.Runtime" Version="7.0.0" />
<PackageReference Include="Microsoft.Orleans.Sdk" Version="7.0.0" />
<PackageReference Include="Microsoft.Orleans.Runtime" Version="8.*" />
<PackageReference Include="Microsoft.Orleans.Sdk" Version="8.*" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions orleans/ShoppingCart/Grains/ProductGrain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ public ProductGrain(
storageName: "shopping-cart")]
IPersistentState<ProductDetails> product) => _product = product;

Task<int> IProductGrain.GetProductAvailabilityAsync() =>
Task<int> IProductGrain.GetProductAvailabilityAsync() =>
Task.FromResult(_product.State.Quantity);

Task<ProductDetails> IProductGrain.GetProductDetailsAsync() =>
Task<ProductDetails> IProductGrain.GetProductDetailsAsync() =>
Task.FromResult(_product.State);

Task IProductGrain.ReturnProductAsync(int quantity) =>
Expand Down
2 changes: 1 addition & 1 deletion orleans/ShoppingCart/Grains/ShoppingCartGrain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public ShoppingCartGrain(
async Task<bool> IShoppingCartGrain.AddOrUpdateItemAsync(int quantity, ProductDetails product)
{
var products = GrainFactory.GetGrain<IProductGrain>(product.Id);

int? adjustedQuantity = null;
if (_cart.State.TryGetValue(product.Id, out var existingItem))
{
Expand Down
15 changes: 15 additions & 0 deletions orleans/ShoppingCart/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@ The app is architected as follows:
1. `cd orleans-on-app-service`
1. `dotnet run --project Silo\Orleans.ShoppingCart.Silo.csproj`

### Deploying to Azure

Before deploying to Azure, make sure you complete the following steps:

1. Create an Azure Cosmos DB for NoSQL account.
1. Create a database named `Orleans`.
1. Within the `Orleans` database, create a container named `OrleansStorage` with a partition key path of `/PartitionKey`.
1. Create another container named `OrleansCluster` within the `Orleans` database. Ensure this container has a partition key path of `/ClusterId`.
1. Get the connection string.
1. Create an Azure Container App as the target of your deployment.
1. Ensure that the target ingress port is `8080`. For more information, see [default ASP.NET Core port changed to 8080](https://learn.microsoft.com/dotnet/core/compatibility/containers/8.0/aspnet-port).
1. Create a secret in the Container App for the Azure Cosmos DB for NoSQL account's connection string.
1. Create an environment variable in the Container App's container named `ORLEANS_AZURE_COSMOS_DB_CONNECTION_STRING`. Reference the secret you just created.
1. Deploy the application to the Azure Container App service. For more information, see [Azure Container Apps deployment options](https://learn.microsoft.com/azure/container-apps/code-to-cloud-options).

### Acknowledgements

The Orleans.ShoppingCart.Silo project uses the following open3rd party-source projects:
Expand Down
2 changes: 1 addition & 1 deletion orleans/ShoppingCart/Silo/App.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Router AppAssembly="@typeof(Startup).Assembly">
<Router AppAssembly="@typeof(App).Assembly">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
</Found>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
Size=Size.Large Disabled=@_isSaving>Bogus</MudButton>
<MudButton OnClick=@Close Color=Color.Secondary Variant=Variant.Filled
Size=Size.Large Disabled=@_isSaving>Cancel</MudButton>
<MudButton StartIcon=@Icons.Filled.Save
<MudButton StartIcon=@Icons.Material.Filled.Save
Color=Color.Primary Size=Size.Large Variant=Variant.Filled
OnClick=@Save Disabled=@_isSaving>
@if (_isSaving)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<RowTemplate>
<MudTd>
<MudFab Disabled=@(IsInCart?.Invoke(product) ?? false) Size=Size.Small
Color=Color.Primary Icon=@Icons.Filled.AddShoppingCart
Color=Color.Primary Icon=@Icons.Material.Filled.AddShoppingCart
OnClick=@(async _ => await AddToCartAsync(product.Id))/>
</MudTd>
<MudTd DataLabel="Name">@product.Name</MudTd>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<MudPaper Outlined="true" Class="flex-grow-1 px-4 pt-2 pb-4">
<MudStack Row="true" Justify="Justify.SpaceBetween" AlignItems="AlignItems.Center">
<MudText Typo="Typo.h6">Cart Summary</MudText>
<MudIconButton Icon="@Icons.Filled.AttachMoney" Color="Color.Default" Edge="Edge.End" />
<MudIconButton Icon="@Icons.Material.Filled.AttachMoney" Color="Color.Default" Edge="Edge.End" />
</MudStack>
<MudStack Spacing="5">
@if (Items is null or { Count: 0 })
Expand Down
26 changes: 26 additions & 0 deletions orleans/ShoppingCart/Silo/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER app
WORKDIR /app
EXPOSE 8080
EXPOSE 8081

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["Silo/Orleans.ShoppingCart.Silo.csproj", "Silo/"]
COPY ["Abstractions/Orleans.ShoppingCart.Abstractions.csproj", "Abstractions/"]
COPY ["Grains/Orleans.ShoppingCart.Grains.csproj", "Grains/"]
RUN dotnet restore "Silo/Orleans.ShoppingCart.Silo.csproj"
COPY . .
WORKDIR "/src/Silo"
RUN dotnet build "Orleans.ShoppingCart.Silo.csproj" -c $BUILD_CONFIGURATION -o /app/build

FROM build AS publish
RUN dotnet publish "Orleans.ShoppingCart.Silo.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Orleans.ShoppingCart.Silo.dll"]
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT License.

namespace Orleans.ShoppingCart.Silo.Extensions;
Expand Down
6 changes: 3 additions & 3 deletions orleans/ShoppingCart/Silo/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft. All rights reserved.
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT License.

global using System.Net;
global using System.Security.Claims;
global using Bogus;
global using Microsoft.AspNetCore.Components;
global using MudBlazor;
Expand All @@ -13,5 +15,3 @@
global using Orleans.ShoppingCart.Silo.Extensions;
global using Orleans.ShoppingCart.Silo.Services;
global using Orleans.ShoppingCart.Silo.StartupTasks;
global using System.Net;
global using System.Security.Claims;
21 changes: 12 additions & 9 deletions orleans/ShoppingCart/Silo/Orleans.ShoppingCart.Silo.csproj
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<IsTransformWebConfigDisabled>false</IsTransformWebConfigDisabled>
<UserSecretsId>98f9767c-ea86-409e-bde7-f6d352a55cce</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.10.2" />
<PackageReference Include="Blazor.Serialization" Version="2.0.11" />
<PackageReference Include="Blazor.LocalStorage" Version="2.0.11" />
<PackageReference Include="Bogus" Version="34.0.2" />
<PackageReference Include="Microsoft.Orleans.Clustering.AzureStorage" Version="7.0.0" />
<PackageReference Include="Microsoft.Orleans.Persistence.AzureStorage" Version="7.0.0" />
<PackageReference Include="Microsoft.Orleans.Server" Version="7.0.0" />
<PackageReference Include="MudBlazor" Version="6.0.18" />
<PackageReference Include="Azure.Identity" Version="1.*" />
<PackageReference Include="Blazor.Serialization" Version="2.*" />
<PackageReference Include="Blazor.LocalStorage" Version="2.*" />
<PackageReference Include="Bogus" Version="34.*" />
<PackageReference Include="Microsoft.Orleans.Clustering.Cosmos" Version="8.*" />
<PackageReference Include="Microsoft.Orleans.Persistence.Cosmos" Version="8.*" />
<PackageReference Include="Microsoft.Orleans.Server" Version="8.*" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.*" />
<PackageReference Include="MudBlazor" Version="6.*" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion orleans/ShoppingCart/Silo/Pages/Cart.razor
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<MudToolBar DisableGutters="true">
<MudText Typo="Typo.h4">Shopping Cart</MudText>
<MudSpacer />
<MudButton Variant="Variant.Outlined" StartIcon=@Icons.Filled.RemoveShoppingCart
<MudButton Variant="Variant.Outlined" StartIcon=@Icons.Material.Filled.RemoveShoppingCart
Disabled=@(_cartItems?.Any() is false) Size=@Size.Large
Color="Color.Secondary" OnClick=@EmptyCartAsync>
Empty Cart
Expand Down
4 changes: 2 additions & 2 deletions orleans/ShoppingCart/Silo/Pages/Products.razor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT License.

using Orleans.ShoppingCart.Silo.Components;
Expand All @@ -20,7 +20,7 @@ public sealed partial class Products
public ProductService ProductService { get; set; } = null!;

[Inject]
public IDialogService DialogService { get; set; } = null!;
public IDialogService DialogService { get; set; } = null!;

protected override async Task OnInitializedAsync() =>
_products = await InventoryService.GetAllProductsAsync();
Expand Down
2 changes: 1 addition & 1 deletion orleans/ShoppingCart/Silo/Pages/Shop.razor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT License.

namespace Orleans.ShoppingCart.Silo.Pages;
Expand Down
Loading

0 comments on commit c04cd19

Please sign in to comment.