Skip to content

feat: add csharp starter template #134

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ When utilizing the `nitric new` command to initiate a new project, the available
| [url-shortener](./v1/url-shortener/) | URL Shortener with Pub/Sub | APIs, Topics, Key Value Stores |
| [websocket-app](./v1/websocket-app/) | A basic websockets example | APIs, WebSockets, Key Value Stores |

### C#

| [dotnet-starter](./v1/dotnet-starter/) | .NET REST API Starter | APIs |

### Websites

| Name | Description | Features |
Expand Down
4 changes: 4 additions & 0 deletions cli-templates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ templates:
label: Go - Starter
desc: A simple REST API
path: ./v1/go-starter
- name: csharp-starter
label: C# - Starter
desc: A simple REST API
path: ./v1/dotnet-starter
6 changes: 6 additions & 0 deletions v1/dotnet-starter/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.nitric/
bin
obj
Nuget.config
.vs
nitric-spec.json
36 changes: 36 additions & 0 deletions v1/dotnet-starter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<p align="center"><a href="https://nitric.io" target="_blank"><img src="https://raw.githubusercontent.com/nitrictech/nitric/main/docs/assets/nitric-logo.svg" height="120"></a></p>

> The Nitric C# SDK is currently experimental and subject to change. We don't recommend using it for production systems, feedback is welcome.

## About Nitric

This is a [Nitric](https://nitric.io) C# project, but Nitric is a framework for rapid development of cloud-native and serverless applications in many languages.

Using Nitric you define your apps in terms of the resources they need, then write the code for serverless function based APIs, event subscribers and scheduled jobs.

Apps built with Nitric can be deployed to AWS, Azure or Google Cloud all from the same code base so you can focus on your products, not your cloud provider.

Nitric makes it easy to:

- Create smart [serverless functions and APIs](https://nitric.io/docs/apis)
- Build reliable distributed apps that use [events](https://nitric.io/docs/messaging/topics) and/or [queues](https://nitric.io/docs/messaging/queues)
- Securely store, retrieve and rotate [secrets](https://nitric.io/docs/secrets)
- Read and write files from [buckets](https://nitric.io/docs/storage)

## Learning Nitric

Nitric provides detailed and intuitive [documentation](https://nitric.io/docs) and [guides](https://nitric.io/docs/getting-started) to help you get started quickly.

If you'd rather chat with the maintainers or community, come and join our [Discord](https://discord.gg/Webemece5C) server, [GitHub Discussions](https://github.com/nitrictech/nitric/discussions) or find us on [Twitter](https://twitter.com/nitric_io).

## Running this project

To run this project you'll need the [Nitric CLI](https://nitric.io/docs/installation) installed, then you can use the CLI commands to run, build or deploy the project.

Start the Nitric services.

```bash
nitric start
```

You'll see your services connect in your `nitric start` terminal.
36 changes: 36 additions & 0 deletions v1/dotnet-starter/csharp-starter.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 25.0.1704.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "services", "services", "{D4817AD1-C4EE-4EE2-A381-CCEDC4B2BEB7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hello", "services\Hello\Hello.csproj", "{524526B1-D05C-4E73-BB52-1EC675E67072}"
EndProject
Global
GlobalSection(NestedProjects) = preSolution
{524526B1-D05C-4E73-BB52-1EC675E67072} = {D4817AD1-C4EE-4EE2-A381-CCEDC4B2BEB7}
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{524526B1-D05C-4E73-BB52-1EC675E67072}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{524526B1-D05C-4E73-BB52-1EC675E67072}.Debug|Any CPU.Build.0 = Debug|Any CPU
{524526B1-D05C-4E73-BB52-1EC675E67072}.Debug|x64.ActiveCfg = Debug|Any CPU
{524526B1-D05C-4E73-BB52-1EC675E67072}.Debug|x64.Build.0 = Debug|Any CPU
{524526B1-D05C-4E73-BB52-1EC675E67072}.Debug|x86.ActiveCfg = Debug|Any CPU
{524526B1-D05C-4E73-BB52-1EC675E67072}.Debug|x86.Build.0 = Debug|Any CPU
{524526B1-D05C-4E73-BB52-1EC675E67072}.Release|Any CPU.ActiveCfg = Release|Any CPU
{524526B1-D05C-4E73-BB52-1EC675E67072}.Release|Any CPU.Build.0 = Release|Any CPU
{524526B1-D05C-4E73-BB52-1EC675E67072}.Release|x64.ActiveCfg = Release|Any CPU
{524526B1-D05C-4E73-BB52-1EC675E67072}.Release|x64.Build.0 = Release|Any CPU
{524526B1-D05C-4E73-BB52-1EC675E67072}.Release|x86.ActiveCfg = Release|Any CPU
{524526B1-D05C-4E73-BB52-1EC675E67072}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
35 changes: 35 additions & 0 deletions v1/dotnet-starter/dotnet.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
ARG HANDLER

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build

# Set working directory
WORKDIR /app

# Copy the entire project directory
COPY . ./

# Restore dependencies
RUN dotnet restore

# Publish as a single file executable
RUN dotnet publish -c Release --self-contained -p:PublishSingleFile=true -p:EnableCompressionInSingleFile=true

# Use a runtime image
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime

ARG HANDLER

# Extract project name from Handler in the form "./services/Hello/Hello.csproj"
ENV PROJECT_NAME=${HANDLER%/*}
ENV PROJECT_NAME=${PROJECT_NAME##*/}

# Set working directory
WORKDIR /app

# Copy the built executable from the builder stage
COPY --from=build /app/out/linux-x64/publish/ ./

RUN ls

# Set entrypoint
ENTRYPOINT "./${PROJECT_NAME}"
9 changes: 9 additions & 0 deletions v1/dotnet-starter/dotnet.dockerfile.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.nitric
.git
.idea
.vscode
.github
*.dockerfile
*.dockerignore
.vs
nitric-spec.json
10 changes: 10 additions & 0 deletions v1/dotnet-starter/nitric.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: csharp-starter
services:
- match: services/**/*.csproj
start: dotnet watch --non-interactive --project $SERVICE_PATH
runtime: dotnet

runtimes:
dotnet:
dockerfile: ./dotnet.dockerfile
args: {}
14 changes: 14 additions & 0 deletions v1/dotnet-starter/services/Hello/Hello.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Application = Nitric.Sdk.Nitric;

var api = Application.Api("main");

api.Get("/hello/:name", async context =>
{
var name = context.Req.PathParams["name"];

context.Res.Text($"Hello {name}!");

return context;
});

Application.Run();
18 changes: 18 additions & 0 deletions v1/dotnet-starter/services/Hello/Hello.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Dotnet Starter</RootNamespace>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<OutputPath>../../out</OutputPath>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Nitric.Sdk" Version="1.0.0" />
</ItemGroup>
</Project>
10 changes: 10 additions & 0 deletions v1/dotnet-starter/services/Hello/Profile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Common;

public class Profile
{
public string Name { get; private set; }

public Profile(string name) {
this.Name = name;
}
}